You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MethodAnnotationsTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.tests;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import org.aspectj.apache.bcel.classfile.JavaClass;
  16. import org.aspectj.apache.bcel.classfile.Method;
  17. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  18. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  19. import org.aspectj.apache.bcel.util.SyntheticRepository;
  20. public class MethodAnnotationsTest extends BcelTestCase {
  21. protected void setUp() throws Exception {
  22. super.setUp();
  23. }
  24. public void testMethodAnnotations() throws ClassNotFoundException {
  25. JavaClass clazz = getClassFromJar("AnnotatedMethods");
  26. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  27. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  28. }
  29. public void testMethodAnnotationsReadWrite() throws ClassNotFoundException,IOException {
  30. JavaClass clazz = getClassFromJar("AnnotatedMethods");
  31. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  32. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  33. // Write it out
  34. File tfile = createTestdataFile("AnnotatedMethods.class");
  35. clazz.dump(tfile);
  36. SyntheticRepository repos2 = createRepos(".");
  37. JavaClass clazz2 = repos2.loadClass("AnnotatedMethods");
  38. checkAnnotatedMethod(clazz,"method1","SimpleAnnotation","id","1");
  39. checkAnnotatedMethod(clazz,"method2","SimpleAnnotation","id","2");
  40. assertTrue(tfile.delete());
  41. }
  42. // helper methods
  43. public void checkAnnotatedMethod(JavaClass clazz,String methodname,
  44. String annotationName,String annotationElementName,String annotationElementValue) {
  45. Method[] methods = clazz.getMethods();
  46. for (Method m : methods) {
  47. AnnotationGen[] methodAnnotations = m.getAnnotations();
  48. if (m.getName().equals(methodname)) {
  49. checkAnnotation(methodAnnotations[0], annotationName, annotationElementName, annotationElementValue);
  50. }
  51. }
  52. }
  53. private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
  54. assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
  55. a.getTypeName().equals(name));
  56. assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
  57. NameValuePair envp = a.getValues().get(0);
  58. assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
  59. elementname.equals(envp.getNameString()));
  60. assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
  61. elementvalue.equals(envp.getValue().stringifyValue()));
  62. }
  63. // helper methods
  64. public void checkValue(AnnotationGen a,String name,String tostring) {
  65. for (NameValuePair element : a.getValues()) {
  66. if (element.getNameString().equals(name)) {
  67. if (!element.getValue().stringifyValue().equals(tostring)) {
  68. fail("Expected element " + name + " to have value " + tostring + " but it had value " + element.getValue().stringifyValue());
  69. }
  70. return;
  71. }
  72. }
  73. fail("Didnt find named element "+name);
  74. }
  75. protected void tearDown() throws Exception {
  76. super.tearDown();
  77. }
  78. }