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.

Ajc164Tests.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*******************************************************************************
  2. * Copyright (c) 2008 Contributors
  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.ajc164;
  12. import java.io.File;
  13. import java.io.PrintWriter;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import org.aspectj.apache.bcel.classfile.LocalVariable;
  17. import org.aspectj.apache.bcel.classfile.LocalVariableTable;
  18. import org.aspectj.apache.bcel.classfile.Method;
  19. import org.aspectj.asm.AsmManager;
  20. import org.aspectj.asm.IHierarchy;
  21. import org.aspectj.asm.IProgramElement;
  22. import org.aspectj.asm.IRelationship;
  23. import org.aspectj.testing.XMLBasedAjcTestCase;
  24. import junit.framework.Test;
  25. public class Ajc164Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
  26. public void testGenericAspectsNpe_pr268689() {
  27. runTest("generics aspects npe");
  28. }
  29. public void testGenericAspectsNpe_pr268710() {
  30. runTest("generics aspects incorrect error");
  31. }
  32. public void testGenericsItdNpe_pr267559() {
  33. runTest("generics and itd npe");
  34. }
  35. public void testAnnoStyleLong_pr266564() {
  36. runTest("annotation style long");
  37. }
  38. public void testAnnoStyleLong_pr266564_2() {
  39. runTest("annotation style long - 2");
  40. }
  41. public void testUnusedPrivateWarning_pr266420() {
  42. runTest("unused private warning");
  43. }
  44. public void testUnusedPrivateWarning_pr266420_2() {
  45. runTest("unused private warning - 2");
  46. }
  47. public void testUnusedPrivateWarning_pr266420_3() {
  48. runTest("unused private warning - 3");
  49. }
  50. /**
  51. * This test program can be used to compare handles for faulted in binary aspects with handles that would be used if the aspect
  52. * was available as source. There are two compile steps in the xml for the test - commenting out the first will allow the source
  53. * handles to be seen, leaving it in will switch to binary. Effectively the only difference should be that in the binary case
  54. * the handles are prefixed 'binaries'.
  55. *
  56. * Change due to bug 274558: now AJDT wants (blah.class as the 'source file' for the ITD so that is another difference when
  57. * switching.
  58. */
  59. public void testItdsAspectPathModel_pr265729_1() {
  60. runTest("aspectpath model");
  61. AsmManager model = AsmManager.lastActiveStructureModel;
  62. IHierarchy top = model.getHierarchy();
  63. printModel(model);
  64. IProgramElement ipe = null;
  65. // ITD METHOD
  66. // should be the ITD method from the binary aspect:
  67. // public Color Orange.getColor() { return Color.orange; }
  68. ipe = top.findElementForType("demo", "Orange");
  69. assertNotNull(ipe);
  70. assertEquals("<demo{Orange.java[Orange", ipe.getHandleIdentifier());
  71. IRelationship ir = model.getRelationshipMap().get(ipe).get(0);
  72. String itdMethodHandle = ir.getTargets().get(0);
  73. // handle when all source: <{Aspect.java}Aspect)Orange.getColor
  74. // assertEquals("/binaries<{Aspect.java}Aspect)Orange.getColor", itdMethodHandle);
  75. assertEquals("/binaries<(Aspect.class'Aspect)Orange.getColor", itdMethodHandle);
  76. IProgramElement itdpe = model.getHierarchy().findElementForHandle(itdMethodHandle);
  77. assertEquals("java.awt.Color", itdpe.getCorrespondingType(true));
  78. // ITD FIELD
  79. // should be the ITD field from the binary aspect:
  80. // public Color Strawberry.color
  81. ipe = top.findElementForType("demo", "Strawberry");
  82. assertNotNull(ipe);
  83. assertEquals("<demo{Strawberry.java[Strawberry", ipe.getHandleIdentifier());
  84. ir = model.getRelationshipMap().get(ipe).get(0);
  85. String itdFieldHandle = ir.getTargets().get(0);
  86. // source handle <{Aspect.java}Aspect)Strawberry.color
  87. // assertEquals("/binaries<{Aspect.java}Aspect)Strawberry.color", itdFieldHandle);
  88. assertEquals("/binaries<(Aspect.class'Aspect,Strawberry.color", itdFieldHandle);
  89. IProgramElement itdfpe = model.getHierarchy().findElementForHandle(itdMethodHandle);
  90. assertEquals("java.awt.Color", itdfpe.getCorrespondingType(true));
  91. // ITD CONSTRUCTOR
  92. // /binaries< Aspect.java}Aspect)java.awt.Color demo.Strawberry.color
  93. ipe = top.findElementForType("demo", "Fruit");
  94. assertNotNull(ipe);
  95. assertEquals("<demo{Fruit.java[Fruit", ipe.getHandleIdentifier());
  96. ir = model.getRelationshipMap().get(ipe).get(0);
  97. String itdCtorHandle = ir.getTargets().get(0);
  98. // source handle <{Aspect.java}Aspect)Fruit.Fruit_new)QColor;)QString;
  99. // assertEquals("/binaries<{Aspect.java}Aspect)Fruit.Fruit_new)QColor;)QString;", itdCtorHandle);
  100. assertEquals("/binaries<(Aspect.class'Aspect)Fruit.Fruit_new)QColor;)QString;", itdCtorHandle);
  101. IProgramElement itdcpe = model.getHierarchy().findElementForHandle(itdCtorHandle);
  102. List<char[]> ptypes = itdcpe.getParameterTypes();
  103. assertEquals("java.awt.Color", new String(ptypes.get(0)));
  104. assertEquals("java.lang.String", new String(ptypes.get(1)));
  105. }
  106. private void printModel(AsmManager model) {
  107. try {
  108. AsmManager.dumptree(model.getHierarchy().getRoot(), 0);
  109. model.dumprels(new PrintWriter(System.out));
  110. } catch (Exception e) {
  111. }
  112. }
  113. public void testGenericsAopXml_pr266220() {
  114. runTest("generics and aop.xml");
  115. }
  116. public void testOptimizingIf_pr266165_1() {
  117. runTest("optimizing if for constant reference - 1");
  118. }
  119. public void testOptimizingIf_pr266165_2() {
  120. runTest("optimizing if for constant reference - 2");
  121. }
  122. public void testOptimizingIf_pr266165_3() {
  123. runTest("optimizing if for constant reference - 3");
  124. }
  125. public void testOptimizingIf_pr266165_4() {
  126. runTest("optimizing if for constant reference - 4");
  127. }
  128. // public void testAnnoInherited_pr265695() {
  129. // runTest("anno inherited");
  130. // }
  131. //
  132. // public void testAnnoInherited_pr265695_2() {
  133. // runTest("new syntax for inherited anno - 1");
  134. // }
  135. //
  136. // public void testAnnoInherited_pr265695_3() {
  137. // runTest("new syntax for inherited anno - 3");
  138. // }
  139. public void testParserProblemSubArrayPatterns_pr148508() {
  140. runTest("parser problem for array subtypes");
  141. }
  142. public void testVarargs_pr265418() {
  143. runTest("varargs");
  144. }
  145. public void testIncorrectDateResolution_pr265360() {
  146. runTest("incorrect resolution of Date");
  147. }
  148. public void testDualPreClinit_pr233032() {
  149. runTest("dual preClinit");
  150. }
  151. public void testHandles_pr263310() {
  152. runTest("inner handles");
  153. IHierarchy top = AsmManager.lastActiveStructureModel.getHierarchy();
  154. IProgramElement ipe = null;
  155. ipe = findElementAtLine(top.getRoot(), 13);
  156. assertEquals("<p{HandleTestingAspect.java'HandleTestingAspect[InnerClass'InnerInnerAspect|1", ipe.getHandleIdentifier());
  157. // ipe = findElementAtLine(top.getRoot(), 29);
  158. // assertEquals("<x*OverrideOptions.aj}OverrideOptions&around!2",
  159. // ipe.getHandleIdentifier());
  160. }
  161. public void testHandles_pr263666() {
  162. runTest("around advice handles");
  163. IHierarchy top = AsmManager.lastActiveStructureModel.getHierarchy();
  164. IProgramElement ipe = null;
  165. ipe = findElementAtLine(top.getRoot(), 22);
  166. assertEquals("<x*OverrideOptions.aj'OverrideOptions&around", ipe.getHandleIdentifier());
  167. ipe = findElementAtLine(top.getRoot(), 29);
  168. assertEquals("<x*OverrideOptions.aj'OverrideOptions&around!2", ipe.getHandleIdentifier());
  169. }
  170. // Only one of two aspects named
  171. public void testAopConfig1() {
  172. runTest("aop config - 1");
  173. }
  174. // Only one of two aspects named - and named one is scoped to only affect
  175. // one type
  176. public void testAopConfig2() {
  177. runTest("aop config - 2");
  178. }
  179. // Invalid scope specified - cannot be parsed as type pattern
  180. public void testAopConfig3() {
  181. runTest("aop config - 3");
  182. }
  183. // excluding types from weaving
  184. public void testAopConfig4() {
  185. runTest("aop config - 4");
  186. }
  187. // excluding types from weaving
  188. public void testAopConfig5() {
  189. runTest("aop config - 5");
  190. }
  191. public void testAjcThisNotRead() {
  192. runTest("ajcthis not read");
  193. }
  194. public void testRecursiveCflow() {
  195. runTest("recursive cflow");
  196. }
  197. public void testAnnoDecprecedence_pr256779() {
  198. runTest("anno decprecedence");
  199. }
  200. //
  201. public void testBrokenLVT_pr194314_1() throws Exception {
  202. runTest("broken lvt - 1");
  203. Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Service"), "method_aroundBody1$advice");
  204. if (m.getLocalVariableTable() == null) {
  205. fail("Local variable table should not be null");
  206. }
  207. // Method:
  208. // private static final void method_aroundBody1$advice(Service, long,
  209. // JoinPoint, ServiceInterceptor, ProceedingJoinPoint);
  210. LocalVariable[] lvt = m.getLocalVariableTable().getLocalVariableTable();
  211. assertEquals(7, lvt.length); // no aroundClosure compared to second
  212. // version of this test
  213. assertEquals("LService; ajc$this(0) start=0 len=86", stringify(m.getLocalVariableTable(), 0));
  214. assertEquals("J l(1) start=0 len=86", stringify(m.getLocalVariableTable(), 1));
  215. assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(3) start=0 len=86", stringify(m.getLocalVariableTable(), 2));
  216. assertEquals("LServiceInterceptor; ajc$aspectInstance(4) start=0 len=86", stringify(m.getLocalVariableTable(), 3));
  217. assertEquals("Lorg/aspectj/lang/ProceedingJoinPoint; pjp(5) start=0 len=86", stringify(m.getLocalVariableTable(), 4));
  218. assertEquals("[Ljava/lang/Object; args(6) start=9 len=77", stringify(m.getLocalVariableTable(), 5));
  219. assertEquals("J id(7) start=21 len=65", stringify(m.getLocalVariableTable(), 6));
  220. }
  221. public void testBrokenLVT_pr194314_2() throws Exception {
  222. runTest("broken lvt - 2");
  223. Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Service"), "method_aroundBody1$advice");
  224. if (m.getLocalVariableTable() == null) {
  225. fail("Local variable table should not be null");
  226. }
  227. System.out.println(m.getLocalVariableTable());
  228. LocalVariable[] lvt = m.getLocalVariableTable().getLocalVariableTable();
  229. assertEquals(8, lvt.length);
  230. // private static final void method_aroundBody1$advice(Service, long,
  231. // JoinPoint, ServiceInterceptorCodeStyle, AroundClosure,
  232. // JoinPoint);
  233. assertEquals("LService; ajc$this(0) start=0 len=68", stringify(m.getLocalVariableTable(), 0));
  234. assertEquals("J l(1) start=0 len=68", stringify(m.getLocalVariableTable(), 1));
  235. assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(3) start=0 len=68", stringify(m.getLocalVariableTable(), 2));
  236. assertEquals("LServiceInterceptorCodeStyle; ajc$aspectInstance(4) start=0 len=68", stringify(m.getLocalVariableTable(), 3));
  237. assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(5) start=0 len=68",
  238. stringify(m.getLocalVariableTable(), 4));
  239. assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(6) start=0 len=68", stringify(m.getLocalVariableTable(), 5));
  240. assertEquals("[Ljava/lang/Object; args(7) start=9 len=59", stringify(m.getLocalVariableTable(), 6));
  241. assertEquals("J id(8) start=21 len=47", stringify(m.getLocalVariableTable(), 7));
  242. }
  243. /**
  244. * This test checks that local variable table for the interMethodDispatcher is built correctly, for the related code see
  245. * IntertypeMethodDeclaration.generateDispatchMethod(). It checks non-static and static ITDs. Once the information here is
  246. * correct then around advice on ITDs can also be correct.
  247. */
  248. public void testBrokenLVT_pr194314_3() throws Exception {
  249. runTest("broken lvt - 3");
  250. // Check intermethoddispatchers have the lvts correct
  251. // first ITD: public void I.foo(String s,int i,String[] ss) {}
  252. Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "X"), "ajc$interMethodDispatch1$X$I$foo");
  253. LocalVariableTable lvt = m.getLocalVariableTable();
  254. assertNotNull(lvt);
  255. assertEquals("LI; ajc$this_(0) start=0 len=10", stringify(lvt, 0));
  256. assertEquals("Ljava/lang/String; s(1) start=0 len=10", stringify(lvt, 1));
  257. assertEquals("I i(2) start=0 len=10", stringify(lvt, 2));
  258. assertEquals("[Ljava/lang/String; ss(3) start=0 len=10", stringify(lvt, 3));
  259. // second ITD: public void I.fooStatic(Long l,int i,String[] ss) {}
  260. m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "X"), "ajc$interMethodDispatch1$X$C$fooStatic");
  261. lvt = m.getLocalVariableTable();
  262. assertNotNull(lvt);
  263. assertEquals("J l(0) start=0 len=7", stringify(lvt, 0));
  264. assertEquals("I i(2) start=0 len=7", stringify(lvt, 1));
  265. assertEquals("[Ljava/lang/String; ss(3) start=0 len=7", stringify(lvt, 2));
  266. // Now let's check the around advice on the calls to those ITDs
  267. // non-static:
  268. m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "C"), "foo_aroundBody1$advice");
  269. lvt = m.getLocalVariableTable();
  270. assertNotNull(lvt);
  271. assertEquals("LC; ajc$this(0) start=0 len=0", stringify(lvt, 0));
  272. assertEquals("LI; target(1) start=0 len=0", stringify(lvt, 1));
  273. assertEquals("Ljava/lang/String; s(2) start=0 len=0", stringify(lvt, 2));
  274. assertEquals("I i(3) start=0 len=0", stringify(lvt, 3));
  275. assertEquals("[Ljava/lang/String; ss(4) start=0 len=0", stringify(lvt, 4));
  276. assertEquals("LX; ajc$aspectInstance(5) start=0 len=0", stringify(lvt, 5));
  277. assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(6) start=0 len=0", stringify(lvt, 6));
  278. // static:
  279. m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "C"), "fooStatic_aroundBody3$advice");
  280. lvt = m.getLocalVariableTable();
  281. assertNotNull(lvt);
  282. assertEquals("LC; ajc$this(0) start=0 len=0", stringify(lvt, 0));
  283. assertEquals("J l(1) start=0 len=0", stringify(lvt, 1));
  284. assertEquals("I i(3) start=0 len=0", stringify(lvt, 2));
  285. assertEquals("[Ljava/lang/String; ss(4) start=0 len=0", stringify(lvt, 3));
  286. assertEquals("LX; ajc$aspectInstance(5) start=0 len=0", stringify(lvt, 4));
  287. assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(6) start=0 len=0", stringify(lvt, 5));
  288. }
  289. // Single piece of advice on before execution of a method with a this and a
  290. // parameter
  291. public void testDebuggingBeforeAdvice_pr262509() throws Exception {
  292. runTest("debugging before advice");
  293. Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo"), "foo");
  294. assertEquals("LFoo; this(0) start=0 len=13", stringify(method.getLocalVariableTable(), 0));
  295. assertEquals("LBar; bar(1) start=0 len=13", stringify(method.getLocalVariableTable(), 1));
  296. }
  297. // Single piece of advice on before execution of a method with a this and a
  298. // parameter and other various locals within it
  299. public void testDebuggingBeforeAdvice_pr262509_2() throws Exception {
  300. // Compile with -preserveAllLocals
  301. runTest("debugging before advice - 2");
  302. Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo2"), "foo");
  303. // System.out.println(stringify(method.getLocalVariableTable()));
  304. List<LocalVariable> l = sortedLocalVariables(method.getLocalVariableTable());
  305. assertEquals("LBar; bar(1) start=0 len=34", stringify(l, 0));
  306. assertEquals("Ljava/lang/Exception; e(3) start=29 len=4", stringify(l, 1));
  307. assertEquals("LFoo2; this(0) start=0 len=34", stringify(l, 4));
  308. assertEquals("Ljava/lang/String; s(2) start=15 len=19", stringify(l, 2));
  309. // With the 1.8 compiler looks like len=7 and not len=10 here, the goto to jump to the return is no longer included
  310. // in the variable range
  311. assertEquals("Ljava/lang/String; s2(3) start=18 len=7", stringify(l, 3));
  312. }
  313. // Two pieces of advice on before execution of a method with a this and a
  314. // parameter and another local within it
  315. public void testDebuggingBeforeAdvice_pr262509_3() throws Exception {
  316. // Compile with -preserveAllLocals
  317. runTest("debugging before advice - 3");
  318. Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo3"), "foo");
  319. System.out.println(stringify(method.getLocalVariableTable()));
  320. assertEquals("LFoo3; this(0) start=0 len=35", stringify(method.getLocalVariableTable(), 0));
  321. assertEquals("LBar; bar(1) start=0 len=35", stringify(method.getLocalVariableTable(), 1));
  322. assertEquals("Ljava/lang/Exception; e(2) start=30 len=4", stringify(method.getLocalVariableTable(), 2));
  323. }
  324. public void testRogueErrors_pr246393_1() {
  325. runTest("rogue errors - 1");
  326. }
  327. // public void testNameClash_pr262257() {
  328. // runTest("name clash");
  329. // fail("incomplete");
  330. // }
  331. public void testCompilingSpring_pr260384() {
  332. runTest("compiling spring");
  333. }
  334. public void testCompilingSpring_pr260384_2() {
  335. runTest("compiling spring - 2");
  336. }
  337. public void testCompilingSpring_pr260384_3() {
  338. runTest("compiling spring - 3");
  339. }
  340. public void testCompilingSpring_pr260384_4() {
  341. runTest("compiling spring - 4");
  342. }
  343. public void testAtAspectJDecp_pr164016() {
  344. runTest("ataspectj decp 164016");
  345. }
  346. public void testAtAspectJDecp_pr258788() {
  347. runTest("ataspectj decp 258788");
  348. }
  349. // ---
  350. public static Test suite() {
  351. return XMLBasedAjcTestCase.loadSuite(Ajc164Tests.class);
  352. }
  353. @Override
  354. protected java.net.URL getSpecFile() {
  355. return getClassResource("ajc164.xml");
  356. }
  357. private IProgramElement findElementAtLine(IProgramElement whereToLook, int line) {
  358. if (whereToLook == null) {
  359. return null;
  360. }
  361. if (whereToLook.getSourceLocation() != null && whereToLook.getSourceLocation().getLine() == line) {
  362. return whereToLook;
  363. }
  364. List<IProgramElement> kids = whereToLook.getChildren();
  365. for (Iterator<IProgramElement> iterator = kids.iterator(); iterator.hasNext();) {
  366. IProgramElement object = iterator.next();
  367. if (object.getSourceLocation() != null && object.getSourceLocation().getLine() == line) {
  368. return object;
  369. }
  370. IProgramElement gotSomething = findElementAtLine(object, line);
  371. if (gotSomething != null) {
  372. return gotSomething;
  373. }
  374. }
  375. return null;
  376. }
  377. }