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.

RuntimeVisibleAnnotationAttributeTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 java.util.ArrayList;
  16. import java.util.List;
  17. import org.aspectj.apache.bcel.classfile.Attribute;
  18. import org.aspectj.apache.bcel.classfile.ConstantPool;
  19. import org.aspectj.apache.bcel.classfile.JavaClass;
  20. import org.aspectj.apache.bcel.classfile.Utility;
  21. import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue;
  22. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  23. import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue;
  24. import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue;
  25. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  26. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  27. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  28. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos;
  29. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  30. import org.aspectj.apache.bcel.generic.ClassGen;
  31. import org.aspectj.apache.bcel.util.SyntheticRepository;
  32. public class RuntimeVisibleAnnotationAttributeTest extends BcelTestCase {
  33. protected void setUp() throws Exception {
  34. super.setUp();
  35. }
  36. public void testSeeAnnotationsAsAttribute() throws ClassNotFoundException {
  37. SyntheticRepository repos = createRepos("testcode.jar");
  38. JavaClass clazz = repos.loadClass("SimpleAnnotatedClass");
  39. ConstantPool pool = clazz.getConstantPool();
  40. Attribute[] rvaAttr = findAttribute("RuntimeVisibleAnnotations",clazz);
  41. assertTrue("Expected a RuntimeVisibleAnnotations attribute but found none",
  42. rvaAttr.length==1);
  43. }
  44. public void testAnnotationsAttributeContainsRightData() throws ClassNotFoundException {
  45. SyntheticRepository repos = createRepos("testcode.jar");
  46. JavaClass clazz = repos.loadClass("SimpleAnnotatedClass");
  47. ConstantPool pool = clazz.getConstantPool();
  48. Attribute[] rvaAttr = findAttribute("RuntimeVisibleAnnotations",clazz);
  49. RuntimeVisAnnos rva = (RuntimeVisAnnos) rvaAttr[0];
  50. List<AnnotationGen> anns = rva.getAnnotations();
  51. assertTrue("Should be one annotation but found "+anns.size(),
  52. anns.size()==1);
  53. AnnotationGen ann = anns.get(0);
  54. assertTrue("Should be called 'SimpleAnnotation' but was called "+ann.getTypeName(),
  55. ann.getTypeName().equals("SimpleAnnotation"));
  56. List<NameValuePair> l = ann.getValues();
  57. assertTrue("Should be one value for annotation 'SimpleAnnotation' but found "+l.size(),
  58. l.size()==1);
  59. NameValuePair envp = l.get(0);
  60. assertTrue("Name of element in SimpleAnnotation should be 'id' but it is "+envp.getNameString(),
  61. envp.getNameString().equals("id"));
  62. SimpleElementValue evalue = (SimpleElementValue)envp.getValue();
  63. assertTrue("'id' should be of type int, but it is "+evalue.getElementValueType(),evalue.getElementValueType()==SimpleElementValue.PRIMITIVE_INT);
  64. assertTrue("'id' should have value 4 but it is "+evalue.getValueInt(),
  65. evalue.getValueInt()==4);
  66. }
  67. public void testAccessingAnnotationsOnClazz() throws ClassNotFoundException {
  68. SyntheticRepository repos = createRepos("testcode.jar");
  69. JavaClass clazz = repos.loadClass("SimpleAnnotatedClass");
  70. ConstantPool pool = clazz.getConstantPool();
  71. AnnotationGen[] anns = clazz.getAnnotations();
  72. assertTrue("Expected one annotation on SimpleAnnotatedClass class but got "+anns.length,
  73. anns.length==1);
  74. }
  75. public void testReadingWritingAnnotations() throws ClassNotFoundException, IOException {
  76. SyntheticRepository repos = createRepos("testcode.jar");
  77. JavaClass clazz = repos.loadClass("SimpleAnnotatedClass");
  78. ConstantPool pool = clazz.getConstantPool();
  79. AnnotationGen[] anns = clazz.getAnnotations();
  80. assertTrue("Expected one annotation on SimpleAnnotatedClass class but got "+anns.length,
  81. anns.length==1);
  82. // Write it out
  83. File tfile = createTestdataFile("SimpleAnnotatedClass.class");
  84. clazz.dump(tfile);
  85. SyntheticRepository repos2 = createRepos(".");
  86. JavaClass clazz2 = repos.loadClass("SimpleAnnotatedClass");
  87. ConstantPool pool2 = clazz2.getConstantPool();
  88. AnnotationGen[] anns2 = clazz2.getAnnotations();
  89. assertTrue("Expected one annotation on SimpleAnnotatedClass class but got "+anns2.length,
  90. anns2.length==1);
  91. assertTrue(tfile.delete());
  92. }
  93. ////
  94. // Test for annotations containing string elements
  95. public void testAnnotationStringElement() throws ClassNotFoundException {
  96. SyntheticRepository repos = createRepos("testcode.jar");
  97. JavaClass clazz = repos.loadClass("AnnotatedClass");
  98. verifyAnnotationStringElement(clazz);
  99. }
  100. public void testAnnotationStringElementReadWrite() throws ClassNotFoundException, IOException {
  101. SyntheticRepository repos = createRepos("testcode.jar");
  102. JavaClass clazz = repos.loadClass("AnnotatedClass");
  103. verifyAnnotationStringElement(clazz);
  104. // Write it out
  105. File tfile = createTestdataFile("AnnotatedClass.class");
  106. clazz.dump(tfile);
  107. SyntheticRepository repos2 = createRepos(".");
  108. JavaClass clazz2 = repos2.loadClass("AnnotatedClass");
  109. verifyAnnotationStringElement(clazz2);
  110. assertTrue(tfile.delete());
  111. }
  112. private void verifyAnnotationStringElement(JavaClass clazz) {
  113. AnnotationGen[] anns = clazz.getAnnotations();
  114. assertTrue("should be one annotation but found "+anns.length,anns.length==1);
  115. AnnotationGen ann = anns[0];
  116. assertTrue("should be called 'AnnotationStringElement' but was called "+ann.getTypeName(),
  117. ann.getTypeName().equals("AnnotationStringElement"));
  118. List<NameValuePair> l = ann.getValues();
  119. assertTrue("Should be one value but there were "+l.size(),l.size()==1);
  120. NameValuePair nvp = l.get(0);
  121. assertTrue("Name of element should be 'stringval' but was "+nvp.getNameString(),
  122. nvp.getNameString().equals("stringval"));
  123. SimpleElementValue ev = (SimpleElementValue)nvp.getValue();
  124. assertTrue("String value should be 'hello' but was '"+ev.getValueString()+"'",
  125. ev.getValueString().equals("hello"));
  126. }
  127. ////
  128. // Test for complex annotation that includes all primitives
  129. public void testComplexAnnotation() throws ClassNotFoundException {
  130. SyntheticRepository repos = createRepos("testcode.jar");
  131. JavaClass clazz = repos.loadClass("ComplexAnnotatedClass");
  132. verifyComplexAnnotation(clazz);
  133. }
  134. public void testComplexAnnotationsReadWrite() throws ClassNotFoundException, IOException {
  135. SyntheticRepository repos = createRepos("testcode.jar");
  136. JavaClass clazz = repos.loadClass("ComplexAnnotatedClass");
  137. verifyComplexAnnotation(clazz);
  138. // Write it out
  139. File tfile = createTestdataFile("ComplexAnnotatedClass.class");
  140. clazz.dump(tfile);
  141. SyntheticRepository repos2 = createRepos(".");
  142. JavaClass clazz2 = repos.loadClass("ComplexAnnotatedClass");
  143. verifyComplexAnnotation(clazz2);
  144. assertTrue(tfile.delete());
  145. }
  146. private void verifyComplexAnnotation(JavaClass clazz) {
  147. AnnotationGen[] anns = clazz.getAnnotations();
  148. assertTrue("Should be one annotation but found "+anns.length,anns.length==1);
  149. AnnotationGen ann = anns[0];
  150. assertTrue("Should be called 'ComplexAnnotation' but was called "+ann.getTypeName(),
  151. ann.getTypeName().equals("ComplexAnnotation"));
  152. List<NameValuePair> l = ann.getValues();
  153. assertTrue("Should be eight values for annotation 'ComplexAnnotation' but found "+l.size(),
  154. l.size()==8);
  155. List<String> names = RuntimeVisibleAnnotationAttributeTest.getListOfAnnotationNames(ann);
  156. assertTrue("Cant find expected element ",names.contains("ival"));
  157. assertTrue("Cant find expected element ",names.contains("dval"));
  158. assertTrue("Cant find expected element ",names.contains("zval"));
  159. assertTrue("Cant find expected element ",names.contains("fval"));
  160. assertTrue("Cant find expected element ",names.contains("jval"));
  161. assertTrue("Cant find expected element ",names.contains("sval"));
  162. assertTrue("Cant find expected element ",names.contains("bval"));
  163. assertTrue("Cant find expected element ",names.contains("cval"));
  164. checkValue(ann,"ival","4");
  165. checkValue(ann,"jval","56");
  166. checkValue(ann,"fval","3.0");
  167. checkValue(ann,"dval","33.4");
  168. checkValue(ann,"sval","99");
  169. checkValue(ann,"bval","2");
  170. checkValue(ann,"cval", Character.toString('5'));
  171. checkValue(ann,"zval","false");
  172. }
  173. private void checkValue(AnnotationGen a,String name,String tostring) {
  174. for (NameValuePair element : a.getValues()) {
  175. if (element.getNameString().equals(name)) {
  176. if (!element.getValue().stringifyValue().equals(tostring)) {
  177. fail("Expected element " + name + " to have value " + tostring + " but it had value " + element.getValue().stringifyValue());
  178. }
  179. return;
  180. }
  181. }
  182. fail("Didnt find named element "+name);
  183. }
  184. ////
  185. // Test an annotation containing a 'Class' element
  186. public void testAnnotationClassElement() throws ClassNotFoundException {
  187. SyntheticRepository repos = createRepos("testcode.jar");
  188. JavaClass clazz = repos.loadClass("AnnotatedWithClassClass");
  189. verifyClassAnnotation(clazz);
  190. }
  191. public void testAnnotationClassElementCopying() throws ClassNotFoundException {
  192. SyntheticRepository repos = createRepos("testcode.jar");
  193. JavaClass clazz = repos.loadClass("AnnotatedWithClassClass");
  194. AnnotationGen[] anns = clazz.getAnnotations();
  195. ClassGen cg = new ClassGen(clazz);
  196. // Checks we can copy class values in an annotation
  197. new AnnotationGen(anns[0],cg.getConstantPool(),true);
  198. new AnnotationGen(anns[0],cg.getConstantPool(),false);
  199. }
  200. public void testAnnotationClassElementReadWrite() throws ClassNotFoundException,IOException {
  201. SyntheticRepository repos = createRepos("testcode.jar");
  202. JavaClass clazz = repos.loadClass("AnnotatedWithClassClass");
  203. verifyClassAnnotation(clazz);
  204. // Write it out
  205. File tfile = createTestdataFile("AnnotatedWithClassClass.class");
  206. clazz.dump(tfile);
  207. SyntheticRepository repos2 = createRepos(".");
  208. JavaClass clazz2 = repos2.loadClass("AnnotatedWithClassClass");
  209. verifyClassAnnotation(clazz2);
  210. assertTrue(wipe("AnnotatedWithClassClass.class"));
  211. }
  212. private void verifyClassAnnotation(JavaClass clazz) {
  213. AnnotationGen[] anns = clazz.getAnnotations();
  214. assertTrue("should be one annotation but found "+anns.length,anns.length==1);
  215. AnnotationGen ann = anns[0];
  216. assertTrue("should be called 'AnnotationClassElement' but was called "+ann.getTypeName(),
  217. ann.getTypeName().equals("AnnotationClassElement"));
  218. List<NameValuePair> l = ann.getValues();
  219. assertTrue("Should be one value but there were "+l.size(),l.size()==1);
  220. NameValuePair nvp = l.get(0);
  221. assertTrue("Name of element should be 'clz' but was "+nvp.getNameString(),
  222. nvp.getNameString().equals("clz"));
  223. ClassElementValue ev = (ClassElementValue)nvp.getValue();
  224. assertTrue("String value should be 'Ljava/lang/Integer;' but was '"+ev.getClassString()+"'",
  225. ev.getClassString().equals("Ljava/lang/Integer;"));
  226. }
  227. ////
  228. // Test an annotation containing an enum element
  229. public void testAnnotationEnumElement() throws ClassNotFoundException {
  230. SyntheticRepository repos = createRepos("testcode.jar");
  231. JavaClass clazz = repos.loadClass("AnnotatedWithEnumClass");
  232. verifyAnnotationEnumElement(clazz);
  233. }
  234. public void testAnnotationEnumElementReadWrite() throws ClassNotFoundException, IOException {
  235. SyntheticRepository repos = createRepos("testcode.jar");
  236. JavaClass clazz = repos.loadClass("AnnotatedWithEnumClass");
  237. verifyAnnotationEnumElement(clazz);
  238. // Write it out
  239. File tfile = createTestdataFile("AnnotatedWithEnumClass.class");
  240. clazz.dump(tfile);
  241. SyntheticRepository repos2 = createRepos(".");
  242. JavaClass clazz2 = repos2.loadClass("AnnotatedWithEnumClass");
  243. verifyAnnotationEnumElement(clazz2);
  244. assertTrue(tfile.delete());
  245. }
  246. public void verifyAnnotationEnumElement(JavaClass clazz) {
  247. AnnotationGen[] anns = clazz.getAnnotations();
  248. assertTrue("should be one annotation but found "+anns.length,anns.length==1);
  249. AnnotationGen ann = anns[0];
  250. assertTrue("should be called 'AnnotationEnumElement' but was called "+ann.getTypeName(),
  251. ann.getTypeName().equals("AnnotationEnumElement"));
  252. List<NameValuePair> l = ann.getValues();
  253. assertTrue("Should be one value but there were "+l.size(),l.size()==1);
  254. NameValuePair nvp = l.get(0);
  255. assertTrue("Name of element should be 'enumval' but was "+nvp.getNameString(),
  256. nvp.getNameString().equals("enumval"));
  257. ElementValue ev = nvp.getValue();
  258. assertTrue("Should be of type EnumElementValue but is "+ev,ev instanceof EnumElementValue);
  259. EnumElementValue eev = (EnumElementValue)ev;
  260. assertTrue("Should be an enum type value but is "+eev.getElementValueType(),eev.getElementValueType()==SimpleElementValue.ENUM_CONSTANT);
  261. assertTrue("Enum type for annotation should be 'SimpleEnum' but is "+Utility.signatureToString(eev.getEnumTypeString()),Utility.signatureToString(eev.getEnumTypeString()).equals("SimpleEnum"));
  262. assertTrue("String value should be 'Red' but was '"+eev.getEnumValueString()+"'",
  263. eev.getEnumValueString().equals("Red"));
  264. }
  265. ////
  266. // Test an annotation with an array element
  267. public void testAnnotationArraysOfAnnotations() throws ClassNotFoundException {
  268. SyntheticRepository repos = createRepos("testcode.jar");
  269. JavaClass clazz = repos.loadClass("AnnotatedWithCombinedAnnotation");
  270. AnnotationGen[] anns = clazz.getAnnotations();
  271. assertTrue("should be one annotation but found "+anns.length,anns.length==1);
  272. checkCombinedAnnotation(anns[0]);
  273. }
  274. public void testAnnotationArraysOfAnnotationsReadWrite() throws ClassNotFoundException, IOException {
  275. SyntheticRepository repos = createRepos("testcode.jar");
  276. JavaClass clazz = repos.loadClass("AnnotatedWithCombinedAnnotation");
  277. AnnotationGen[] anns = clazz.getAnnotations();
  278. assertTrue("should be one annotation but found "+anns.length,anns.length==1);
  279. checkCombinedAnnotation(anns[0]);
  280. // Write it out
  281. File tfile = createTestdataFile("AnnotatedWithCombinedAnnotation.class");
  282. clazz.dump(tfile);
  283. SyntheticRepository repos2 = createRepos(".");
  284. JavaClass clazz2 = repos2.loadClass("AnnotatedWithCombinedAnnotation");
  285. AnnotationGen[] anns2 = clazz2.getAnnotations();
  286. assertTrue("should be one annotation but found "+anns2.length,anns2.length==1);
  287. checkCombinedAnnotation(anns2[0]);
  288. assertTrue(tfile.delete());
  289. }
  290. private void checkCombinedAnnotation(AnnotationGen ann) {
  291. assertTrue("should be called 'CombinedAnnotation' but was called "+ann.getTypeName(),
  292. ann.getTypeName().equals("CombinedAnnotation"));
  293. List<NameValuePair> l = ann.getValues();
  294. assertTrue("Should be one value but there were "+l.size(),l.size()==1);
  295. NameValuePair nvp = l.get(0);
  296. assertTrue("Name of element should be 'value' but was "+nvp.getNameString(),
  297. nvp.getNameString().equals("value"));
  298. ElementValue ev = nvp.getValue();
  299. assertTrue("Should be of type ArrayElementValue but is "+ev,ev instanceof ArrayElementValue);
  300. ArrayElementValue aev = (ArrayElementValue)ev;
  301. assertTrue("Array element value should be of size 1 but is "+aev.getElementValuesArraySize(),
  302. aev.getElementValuesArraySize()==1);
  303. ElementValue[] evs = aev.getElementValuesArray();
  304. assertTrue("Entry in the array should be AnnotationElementValue but is "+evs[0],
  305. evs[0] instanceof AnnotationElementValue);
  306. AnnotationElementValue inner_ev = (AnnotationElementValue)evs[0];
  307. AnnotationGen a = inner_ev.getAnnotation();
  308. assertTrue("Should be SimpleAnnotation but is "+a.getTypeName(),a.getTypeName().equals("SimpleAnnotation"));
  309. List<NameValuePair> envps = a.getValues();
  310. assertTrue("Should be one name value pair but found "+envps.size(),envps.size()==1);
  311. NameValuePair envp = envps.get(0);
  312. assertTrue("Name should be 'id' but it is "+envp.getNameString(),envp.getNameString().equals("id"));
  313. assertTrue("Value of 'id' should be 4 but it is "+envp.getValue().stringifyValue(),
  314. envp.getValue().stringifyValue().equals("4"));
  315. }
  316. protected void tearDown() throws Exception {
  317. super.tearDown();
  318. }
  319. public static List<String> getListOfAnnotationNames(AnnotationGen a) {
  320. List<NameValuePair> l = a.getValues();
  321. List<String> names = new ArrayList<>();
  322. for (NameValuePair element : l) {
  323. names.add(element.getNameString());
  324. }
  325. return names;
  326. }
  327. }