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.

FieldAnnotationsTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Field;
  16. import org.aspectj.apache.bcel.classfile.JavaClass;
  17. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  18. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  19. import org.aspectj.apache.bcel.generic.ClassGen;
  20. import org.aspectj.apache.bcel.generic.FieldGen;
  21. import org.aspectj.apache.bcel.util.SyntheticRepository;
  22. public class FieldAnnotationsTest extends BcelTestCase {
  23. protected void setUp() throws Exception {
  24. super.setUp();
  25. }
  26. /**
  27. * Check field annotations are retrievable.
  28. */
  29. public void testFieldAnnotations() throws ClassNotFoundException {
  30. JavaClass clazz = getClassFromJar("AnnotatedFields");
  31. checkAnnotatedField(clazz,"i","SimpleAnnotation","id","1");
  32. checkAnnotatedField(clazz,"s","SimpleAnnotation","id","2");
  33. }
  34. /**
  35. * Check field annotations (de)serialize ok.
  36. */
  37. public void testFieldAnnotationsReadWrite() throws ClassNotFoundException,IOException {
  38. JavaClass clazz = getClassFromJar("AnnotatedFields");
  39. checkAnnotatedField(clazz,"i","SimpleAnnotation","id","1");
  40. checkAnnotatedField(clazz,"s","SimpleAnnotation","id","2");
  41. // Write it out
  42. File tfile = createTestdataFile("AnnotatedFields.class");
  43. clazz.dump(tfile);
  44. SyntheticRepository repos2 = createRepos(".");
  45. JavaClass clazz2 = repos2.loadClass("AnnotatedFields");
  46. checkAnnotatedField(clazz,"i","SimpleAnnotation","id","1");
  47. checkAnnotatedField(clazz,"s","SimpleAnnotation","id","2");
  48. assertTrue(tfile.delete());
  49. }
  50. /**
  51. * Check we can load in a class, modify its field annotations, save it, reload it and
  52. * everything is correct.
  53. */
  54. public void testFieldAnnotationsModification() throws ClassNotFoundException, IOException {
  55. boolean dbg = false;
  56. JavaClass clazz = getClassFromJar("AnnotatedFields");
  57. ClassGen clg = new ClassGen(clazz);
  58. Field f = clg.getFields()[0];
  59. if (dbg) System.err.println("Field in freshly constructed class is: "+f);
  60. if (dbg) System.err.println("Annotations on field are: "+dumpAnnotations(f.getAnnotations()));
  61. AnnotationGen fruitBasedAnnotation = createFruitAnnotation(clg.getConstantPool(),"Tomato",false);
  62. FieldGen fg = new FieldGen(f,clg.getConstantPool());
  63. if (dbg) System.err.println("Adding annotation to the field");
  64. fg.addAnnotation(fruitBasedAnnotation);
  65. if (dbg) System.err.println("FieldGen (mutable field) is "+fg);
  66. if (dbg) System.err.println("with annotations: "+dumpAnnotations(fg.getAnnotations()));
  67. if (dbg) System.err.println("Replacing original field with new field that has extra annotation");
  68. clg.removeField(f);
  69. clg.addField(fg.getField());
  70. f = clg.getFields()[1]; // there are two fields in the class, removing and readding has changed the order
  71. // so this time index [1] is the 'int i' field
  72. if (dbg) System.err.println("Field now looks like this: "+f);
  73. if (dbg) System.err.println("With annotations: "+dumpAnnotations(f.getAnnotations()));
  74. assertTrue("Should be 2 annotations on this field, but there are "+f.getAnnotations().length,f.getAnnotations().length==2);
  75. }
  76. // helper methods
  77. public void checkAnnotatedField(JavaClass clazz,String fieldname,
  78. String annotationName,String annotationElementName,String annotationElementValue) {
  79. Field[] fields = clazz.getFields();
  80. for (Field f : fields) {
  81. AnnotationGen[] fieldAnnotations = f.getAnnotations();
  82. if (f.getName().equals(fieldname)) {
  83. checkAnnotation(fieldAnnotations[0], annotationName, annotationElementName, annotationElementValue);
  84. }
  85. }
  86. }
  87. private void checkAnnotation(AnnotationGen a,String name,String elementname,String elementvalue) {
  88. assertTrue("Expected annotation to have name "+name+" but it had name "+a.getTypeName(),
  89. a.getTypeName().equals(name));
  90. assertTrue("Expected annotation to have one element but it had "+a.getValues().size(),a.getValues().size()==1);
  91. NameValuePair envp = a.getValues().get(0);
  92. assertTrue("Expected element name "+elementname+" but was "+envp.getNameString(),
  93. elementname.equals(envp.getNameString()));
  94. assertTrue("Expected element value "+elementvalue+" but was "+envp.getValue().stringifyValue(),
  95. elementvalue.equals(envp.getValue().stringifyValue()));
  96. }
  97. // helper methods
  98. public void checkValue(AnnotationGen a,String name,String tostring) {
  99. for (NameValuePair element : a.getValues()) {
  100. if (element.getNameString().equals(name)) {
  101. if (!element.getValue().stringifyValue().equals(tostring)) {
  102. fail("Expected element " + name + " to have value " + tostring + " but it had value " + element.getValue().stringifyValue());
  103. }
  104. return;
  105. }
  106. }
  107. fail("Didnt find named element "+name);
  108. }
  109. protected void tearDown() throws Exception {
  110. super.tearDown();
  111. }
  112. }