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

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