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.

AnnotationBinding.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andy Clement - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc150;
  12. import java.io.File;
  13. import java.util.List;
  14. import junit.framework.Test;
  15. import org.aspectj.asm.AsmManager;
  16. import org.aspectj.asm.IHierarchy;
  17. import org.aspectj.asm.IProgramElement;
  18. import org.aspectj.asm.IRelationship;
  19. import org.aspectj.asm.internal.Relationship;
  20. import org.aspectj.testing.XMLBasedAjcTestCase;
  21. public class AnnotationBinding extends XMLBasedAjcTestCase {
  22. public static Test suite() {
  23. return XMLBasedAjcTestCase.loadSuite(AnnotationBinding.class);
  24. }
  25. protected java.net.URL getSpecFile() {
  26. return getClassResource("ajc150.xml");
  27. }
  28. // /////////////////////////////////// @ANNOTATION and CALL
  29. // Very simple annotation binding for 'call() && @annotation()'
  30. public void testCallAnnotationBinding1() {
  31. runTest("call annotation binding 1");
  32. }
  33. // 'call() && @annotation()' when the called method has multiple arguments
  34. public void testCallAnnotationBinding2() {
  35. runTest("call annotation binding 2");
  36. }
  37. // 'call() && @annotation()' when the called method takes primitive arguments (YUCK!)
  38. public void testCallAnnotationBinding3() {
  39. runTest("call annotation binding 3");
  40. }
  41. // 'call() && @annotation()' when runtime type will exhibit different annotation (due to interface implementing)
  42. public void testCallAnnotationBinding4() {
  43. runTest("call annotation binding 4");
  44. }
  45. // 'call() && @annotation()' when target doesnt have an annotation !
  46. public void testCallAnnotationBinding5() {
  47. runTest("call annotation binding 5");
  48. }
  49. // 'call() && @annotation()' when runtime type will exhibit different annotation (due to subclassing)
  50. public void testCallAnnotationBinding6() {
  51. runTest("call annotation binding 6");
  52. }
  53. // 'call() && @annotation()' using named pointcut
  54. public void testCallAnnotationBinding7() {
  55. runTest("call annotation binding 7");
  56. }
  57. // /////////////////////////////////// @TARGET
  58. // 'call() && @target()'
  59. public void testAtTargetAnnotationBinding1() {
  60. runTest("@target annotation binding 1");
  61. }
  62. // 'call() && @target() && @target'
  63. public void testAtTargetAnnotationBinding2() {
  64. runTest("@target annotation binding 2");
  65. }
  66. // 'call() && @target()' - using a type hierarchy where some levels are missing annotations
  67. public void testAtTargetAnnotationBinding3() {
  68. runTest("@target annotation binding 3");
  69. }
  70. // 'call() && @target()' - using a type hierarchy where some levels are missing annotations
  71. // but the annotation is inherited
  72. public void testAtTargetAnnotationBinding4() {
  73. runTest("@target annotation binding 4");
  74. }
  75. // @target() with an annotation in a package
  76. public void testAtTargetAnnotationBinding5() {
  77. runTest("@target annotation binding 5");
  78. }
  79. // /////////////////////////////////// @THIS
  80. // 'call() && @this()'
  81. public void testAtThisAnnotationBinding1() {
  82. runTest("@this annotation binding 1");
  83. }
  84. // 'call() && @this() && @this'
  85. public void testAtThisAnnotationBinding2() {
  86. runTest("@this annotation binding 2");
  87. }
  88. // 'call() && @this()' - using a type hierarchy where some levels are missing annotations
  89. public void testAtThisAnnotationBinding3() {
  90. runTest("@this annotation binding 3");
  91. }
  92. // 'call() && @this()' - using a type hierarchy where some levels are missing annotations
  93. // but the annotation is inherited
  94. public void testAtThisAnnotationBinding4() {
  95. runTest("@this annotation binding 4");
  96. }
  97. // '@this() and @target()' used together
  98. public void testAtThisAtTargetAnnotationBinding() {
  99. runTest("@this annotation binding 5");
  100. }
  101. // /////////////////////////////////// @ARGS
  102. // complex case when there are 3 parameters
  103. public void testAtArgs1() {
  104. runTest("@args annotation binding 1");
  105. }
  106. // simple case when there is only one parameter
  107. public void testAtArgs2() {
  108. runTest("@args annotation binding 2");
  109. }
  110. // simple case when there is only one parameter and no binding
  111. public void testAtArgs3() {
  112. runTest("@args annotation binding 3");
  113. }
  114. // complex case binding different annotation kinds
  115. public void testAtArgs4() {
  116. runTest("@args annotation binding 4");
  117. }
  118. // check @args and execution()
  119. public void testAtArgs5() {
  120. runTest("@args annotation binding 5");
  121. }
  122. // /////////////////////////////////// @ANNOTATION and EXECUTION
  123. // 'execution() && @annotation()'
  124. public void testExecutionAnnotationBinding1() {
  125. runTest("execution and @annotation");
  126. }
  127. // /////////////////////////////////// @ANNOTATION and SET
  128. // 'set() && @annotation()'
  129. public void testFieldAnnotationBinding1() {
  130. runTest("set and @annotation");
  131. }
  132. // 'get() && @annotation()'
  133. public void testFieldAnnotationBinding2() {
  134. runTest("get and @annotation");
  135. }
  136. // 'get() && @annotation()' when using array fields
  137. public void testFieldAnnotationBinding3() {
  138. runTest("get and @annotation with arrays");
  139. }
  140. // /////////////////////////////////// @ANNOTATION and CTOR-CALL
  141. // 'ctor-call(new) && @annotation()'
  142. public void testCtorCallAnnotationBinding1() {
  143. runTest("cons call and @annotation");
  144. }
  145. // /////////////////////////////////// @ANNOTATION and CTOR-CALL
  146. // 'ctor-execution() && @annotation()'
  147. public void testCtorExecAnnotationBinding1() {
  148. runTest("cons exe and @annotation");
  149. }
  150. // /////////////////////////////////// @ANNOTATION and STATICINITIALIZATION
  151. // 'staticinitialization() && @annotation()'
  152. public void testStaticInitAnnotationBinding1() {
  153. runTest("staticinit and @annotation");
  154. }
  155. // /////////////////////////////////// @ANNOTATION and PREINITIALIZATION
  156. // 'preinitialization() && @annotation()'
  157. public void testPreInitAnnotationBinding1() {
  158. runTest("preinit and @annotation");
  159. }
  160. // /////////////////////////////////// @ANNOTATION and INITIALIZATION
  161. // 'initialization() && @annotation()'
  162. public void testInitAnnotationBinding1() {
  163. runTest("init and @annotation");
  164. }
  165. // /////////////////////////////////// @ANNOTATION and ADVICEEXECUTION
  166. // 'adviceexecution() && @annotation()'
  167. public void testAdviceExecAnnotationBinding1() {
  168. runTest("adviceexecution and @annotation");
  169. }
  170. // /////////////////////////////////// @ANNOTATION and HANDLER
  171. // 'handler() && @annotation()'
  172. public void testHandlerAnnotationBinding1() {
  173. runTest("handler and @annotation");
  174. }
  175. // /////////////////////////////////// @WITHIN
  176. // '@within()'
  177. public void testWithinBinding1() {
  178. runTest("@within");
  179. }
  180. // '@within()' but multiple types around (some annotated)
  181. public void testWithinBinding2() {
  182. runTest("@within - multiple types");
  183. }
  184. // /////////////////////////////////// @WITHINCODE
  185. // '@withincode() && call(* println(..))'
  186. public void testWithinCodeBinding1() {
  187. runTest("@withincode() and call(* println(..))");
  188. }
  189. // /////////////////////////////////// @ANNOTATION complex tests
  190. // Using package names for the types (including the annotation) - NO BINDING
  191. public void testPackageNamedTypesNoBinding() {
  192. runTest("packages and no binding");
  193. }
  194. // Using package names for the types (including the annotation) - INCLUDES BINDING
  195. public void testPackageNamedTypesWithBinding() {
  196. runTest("packages and binding");
  197. }
  198. // declare parents: @Color * implements Serializable
  199. public void testDeclareParentsWithAnnotatedAnyPattern() {
  200. runTest("annotated any pattern");
  201. }
  202. // Should error (in a nice way!) on usage of an annotation that isnt imported
  203. public void testAnnotationUsedButNotImported() {
  204. runTest("annotation not imported");
  205. }
  206. // Binding with calls/executions of static methods
  207. public void testCallsAndExecutionsOfStaticMethods() {
  208. runTest("binding with static methods");
  209. }
  210. // ///////////////////////////////////////////////////////////////////////////////
  211. // annotation binding with ITDs
  212. public void testAnnotationBindingAndITDs1() {
  213. runTest("simple binding annotation values where itd method is annotated");
  214. }
  215. public void testAnnotationBindingAndITDs2() {
  216. runTest("simple binding annotation values where itd field is annotated");
  217. }
  218. public void testAnnotationBindingAndITDs3() {
  219. runTest("simple binding annotation values where itd ctor is annotated");
  220. }
  221. public void testAnnotationBindingAndITDs4() {
  222. runTest("simple binding annotation values where itd method is annotated via declare");
  223. }
  224. public void testAnnotationBindingAndITDs5() {
  225. runTest("simple binding annotation values where itd field is annotated via declare");
  226. }
  227. public void testAnnotationBindingAndITDs6() {
  228. runTest("simple binding annotation values where itd field is annotated multiple times via declare");
  229. }
  230. public void testAnnotationBindingAndITDs7() {
  231. runTest("simple binding annotation values where itd ctor is annotated via declare");
  232. }
  233. public void testAnnotationBindingAndITDs4_asmtest() {
  234. // AsmManager.setReporting("c:/debug.txt",true,true,true,true);
  235. runTest("simple binding annotation values where itd method is annotated via declare");
  236. if (getCurrentTest().canRunOnThisVM()) {
  237. AsmManager asm = AsmManager.lastActiveStructureModel;
  238. IHierarchy top = asm.getHierarchy();
  239. IProgramElement ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_METHOD,
  240. "declare @method: int A.m() : @Fruit(\"orange\")");
  241. assertTrue("Couldn't find 'declare @method' element in the tree", ipe != null);
  242. List<IRelationship> l = asm.getRelationshipMap().get(ipe);
  243. assertTrue("Should have a relationship but does not ", l.size() > 0);
  244. ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_METHOD,
  245. "declare @method: int A.n() : @Fruit(\"banana\")");
  246. assertTrue("Couldn't find 'declare @method element in the tree", ipe != null);
  247. l = asm.getRelationshipMap().get(ipe);
  248. assertTrue("Should have a relationship but does not ", l.size() > 0);
  249. Relationship rel = (Relationship) l.get(0);
  250. assertTrue("Should have 1 target but has " + rel.getTargets().size(), rel.getTargets().size() == 1);
  251. String tgt = (String) rel.getTargets().get(0);
  252. int lineNumber = asm.getHandleProvider().getLineNumberForHandle(tgt);
  253. assertTrue("Should point to line 10 but doesnt: " + lineNumber, lineNumber == 10);
  254. }
  255. }
  256. public void testAnnotationBindingAndITDs5_asmtest() {
  257. // AsmManager.setReporting("c:/debug.txt",true,true,true,true);
  258. runTest("simple binding annotation values where itd field is annotated via declare");
  259. if (getCurrentTest().canRunOnThisVM()) {
  260. AsmManager asm = AsmManager.lastActiveStructureModel;
  261. IHierarchy top = asm.getHierarchy();
  262. IProgramElement ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_FIELD,
  263. "declare @field: int A.i : @Fruit(\"orange\")");
  264. assertTrue("Couldn't find 'declare @type' element in the tree", ipe != null);
  265. List<IRelationship> l = asm.getRelationshipMap().get(ipe);
  266. assertTrue("Should have a relationship but does not ", l.size() > 0);
  267. ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_FIELD,
  268. "declare @field: java.lang.String A.j : @Fruit(\"banana\")");
  269. assertTrue("Couldn't find 'declare @field element in the tree", ipe != null);
  270. l = asm.getRelationshipMap().get(ipe);
  271. assertTrue("Should have a relationship but does not ", l.size() > 0);
  272. Relationship rel = (Relationship) l.get(0);
  273. assertTrue("Should have 1 target but has " + rel.getTargets().size(), rel.getTargets().size() == 1);
  274. String tgt = (String) rel.getTargets().get(0);
  275. int lineNumber = asm.getHandleProvider().getLineNumberForHandle(tgt);
  276. assertTrue("Should point to line 10 but doesnt: " + lineNumber, lineNumber == 10);
  277. }
  278. }
  279. public void testAnnotationBindingAndITDs7_asmtest() {
  280. // AsmManager.setReporting("c:/debug.txt",true,true,true,true);
  281. runTest("simple binding annotation values where itd ctor is annotated via declare");
  282. if (getCurrentTest().canRunOnThisVM()) {
  283. AsmManager asm = AsmManager.lastActiveStructureModel;
  284. IHierarchy top = asm.getHierarchy();
  285. IProgramElement ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_CONSTRUCTOR,
  286. "declare @constructor: A.new(java.lang.String) : @Fruit(\"pear\")");
  287. assertTrue("Couldn't find 'declare @constructor' element in the tree", ipe != null);
  288. List<IRelationship> l = asm.getRelationshipMap().get(ipe);
  289. assertTrue("Should have a relationship but does not ", l.size() > 0);
  290. ipe = top.findElementForLabel(top.getRoot(), IProgramElement.Kind.DECLARE_ANNOTATION_AT_CONSTRUCTOR,
  291. "declare @constructor: A.new(int) : @Fruit(\"orange\")");
  292. assertTrue("Couldn't find 'declare @constructor element in the tree", ipe != null);
  293. l = asm.getRelationshipMap().get(ipe);
  294. assertTrue("Should have a relationship but does not ", l.size() > 0);
  295. Relationship rel = (Relationship) l.get(0);
  296. assertTrue("Should have 1 target but has " + rel.getTargets().size(), rel.getTargets().size() == 1);
  297. String tgt = (String) rel.getTargets().get(0);
  298. int lineNumber = asm.getHandleProvider().getLineNumberForHandle(tgt);
  299. assertTrue("Should point to line 10 but doesnt: " + lineNumber, lineNumber == 10);
  300. }
  301. }
  302. public void testAnnotationBindingArgsVerifyError_pr92053() {
  303. runTest("AtArgs causes a VerifyError: Unable to pop operand off an empty stack");
  304. }
  305. }