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.3KB

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