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

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