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.

CommonAdvancedPointcutExpressionTests.java 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * Andy Clement
  10. *******************************************************************************/
  11. package org.aspectj.matcher.tools;
  12. import junit.framework.TestCase;
  13. import org.aspectj.weaver.ResolvedMember;
  14. import org.aspectj.weaver.ResolvedType;
  15. import org.aspectj.weaver.World;
  16. import org.aspectj.weaver.tools.StandardPointcutExpression;
  17. import org.aspectj.weaver.tools.StandardPointcutParser;
  18. /**
  19. * Test the use of the pointcut parser and matching infrastructure. The org.aspectj.matcher.tools infrastructure used should not be
  20. * aware of what kind of World it is working with and only operate in terms of the type abstraction expressed in the
  21. * org.aspectj.matcher project (so Members, etc). These tests require some testdata types.
  22. *
  23. * This is based on the Reflection oriented PointcutExpressionTest in the weaver project.
  24. *
  25. * @author Andy Clement
  26. */
  27. public abstract class CommonAdvancedPointcutExpressionTests extends TestCase {
  28. private World world;
  29. private StandardPointcutParser pointcutParser;
  30. protected abstract World getWorld();
  31. protected void setUp() throws Exception {
  32. super.setUp();
  33. world = getWorld();
  34. pointcutParser = StandardPointcutParser.getPointcutParserSupportingAllPrimitives(world);
  35. }
  36. public void testResolvingOneType() {
  37. assertFalse(world.resolve("testdata.SomeAnnotation").isMissing());
  38. assertFalse(world.resolve("testdata.MethodLevelAnnotation").isMissing());
  39. assertFalse(world.resolve("testdata.AnnotatedClass").isMissing());
  40. }
  41. public void testTypeLevelAnnotationMatchingWithStaticInitialization01() {
  42. StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("staticinitialization(@testdata.SomeAnnotation *)");
  43. ResolvedType jlString = world.resolve("java.lang.String");
  44. ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
  45. assertTrue(ex.matchesStaticInitialization(tAnnotatedClass).alwaysMatches());
  46. assertTrue(ex.matchesStaticInitialization(jlString).neverMatches());
  47. }
  48. public void testTypeLevelAnnotationMatchingWithExecution01() {
  49. StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("execution(* (@testdata.SomeAnnotation *).*(..))");
  50. ResolvedType jlString = world.resolve("java.lang.String");
  51. ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
  52. assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "annotatedMethod", "()V")).alwaysMatches());
  53. assertTrue(ex.matchesMethodExecution(getMethod(jlString, "valueOf", "(Z)Ljava/lang/String;")).neverMatches());
  54. }
  55. public void testMethodLevelAnnotationMatchingWithExecution01() {
  56. StandardPointcutExpression ex = pointcutParser
  57. .parsePointcutExpression("execution(@testdata.MethodLevelAnnotation * *(..))");
  58. ResolvedType jlString = world.resolve("java.lang.String");
  59. ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
  60. assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "annotatedMethod", "()V")).alwaysMatches());
  61. assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "nonAnnotatedMethod", "()V")).neverMatches());
  62. assertTrue(ex.matchesMethodExecution(getMethod(jlString, "valueOf", "(Z)Ljava/lang/String;")).neverMatches());
  63. }
  64. //
  65. // ResolvedMember stringSplitMethod = getMethod(jlString, "split", "(Ljava/lang/String;I)[Ljava/lang/String;");
  66. // ResolvedMember stringValueOfIntMethod = getMethod(jlString, "valueOf", "(I)Ljava/lang/String;");
  67. // ResolvedMember listAddMethod = getMethod(juList, "add", "(Ljava/lang/Object;)Z");
  68. // public void testResolveTypeAndRetrieveMethod() {
  69. // ResolvedType type = world.resolve("java.lang.String");
  70. // assertNotNull(type);
  71. // ResolvedMember method = getMethod(type, "valueOf", "(Z)Ljava/lang/String;"); // grab the method 'String valueOf()'
  72. // assertNotNull(method);
  73. // }
  74. //
  75. // public void testMethodExecutionMatching01() {
  76. // checkAlwaysMatches("execution(String valueOf(boolean))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  77. // }
  78. //
  79. // public void testMethodExecutionMatching02() {
  80. // checkAlwaysMatches("execution(* *val*(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  81. // checkAlwaysMatches("execution(String *(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  82. // checkAlwaysMatches("execution(* *(boolean))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  83. // checkAlwaysMatches("execution(* j*..*.valueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  84. // checkAlwaysMatches("execution(* *(*))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  85. //
  86. // checkNeverMatches("execution(* vulueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  87. // checkNeverMatches("execution(int *(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  88. // checkNeverMatches("execution(* valueOf(String))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  89. // checkNeverMatches("execution(private * valueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
  90. // }
  91. //
  92. // public void testMethodExecutionMatching03() {
  93. // checkAlwaysMatches("execution(* *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
  94. // checkAlwaysMatches("execution(*[] *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
  95. // checkAlwaysMatches("execution(*b*[] *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
  96. // }
  97. //
  98. // public void testMethodMatchesStaticInitialization() {
  99. // StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("staticinitialization(java.lang.String)");
  100. // assertNotNull(ex);
  101. //
  102. // ResolvedType jlString = world.resolve("java.lang.String");
  103. //
  104. // boolean b = ex.matchesStaticInitialization(jlString).alwaysMatches();
  105. // assertTrue(b);
  106. // }
  107. // public void testMethodExecutionMatching04() {
  108. // was execution((* *..A.aa(..))
  109. // assertTrue("Should match execution of A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
  110. // assertTrue("Should match execution of B.aa", ex.matchesMethodExecution(bsaa).alwaysMatches());
  111. // assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
  112. // ex = p.parsePointcutExpression("call(* *..A.a*(int))");
  113. // assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
  114. //
  115. // // test this
  116. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  117. // assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
  118. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  119. // assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
  120. // assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
  121. //
  122. // // test target
  123. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  124. // assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
  125. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  126. // assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
  127. // assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
  128. //
  129. // // test args
  130. // ex = p.parsePointcutExpression("args(..,int)");
  131. // assertTrue("Should match A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
  132. // assertTrue("Should match A.aaa", ex.matchesMethodExecution(aaa).alwaysMatches());
  133. // assertTrue("Should not match A.a", ex.matchesMethodExecution(a).neverMatches());
  134. //
  135. // // within
  136. // ex = p.parsePointcutExpression("within(*..A)");
  137. // assertTrue("Matches in class A", ex.matchesMethodExecution(a).alwaysMatches());
  138. // assertTrue("Does not match in class B", ex.matchesMethodExecution(bsaa).neverMatches());
  139. //
  140. // // withincode
  141. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  142. // assertTrue("Should not match", ex.matchesMethodExecution(a).neverMatches());
  143. // public void testMatchesMethodCall() {
  144. // PointcutExpression ex = p.parsePointcutExpression("call(* *..A.a*(..))");
  145. // assertTrue("Should match call to A.a()", ex.matchesMethodCall(a, a).alwaysMatches());
  146. // assertTrue("Should match call to A.aaa()", ex.matchesMethodCall(aaa, a).alwaysMatches());
  147. // assertTrue("Should match call to B.aa()", ex.matchesMethodCall(bsaa, a).alwaysMatches());
  148. // assertTrue("Should not match call to B.b()", ex.matchesMethodCall(b, a).neverMatches());
  149. // ex = p.parsePointcutExpression("call(* *..A.a*(int))");
  150. // assertTrue("Should match call to A.aa()", ex.matchesMethodCall(aa, a).alwaysMatches());
  151. // assertTrue("Should not match call to A.a()", ex.matchesMethodCall(a, a).neverMatches());
  152. // ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  153. // assertTrue("Should match call to A.aaa() from Client", ex.matchesMethodCall(aaa, foo).alwaysMatches());
  154. // ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  155. // assertTrue("Should match call to A.aaa() from B", ex.matchesMethodCall(aaa, b).alwaysMatches());
  156. // assertTrue("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).maybeMatches());
  157. // assertFalse("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).alwaysMatches());
  158. // ex = p.parsePointcutExpression("execution(* *.*(..))");
  159. // assertTrue("Should not match call to A.aa", ex.matchesMethodCall(aa, a).neverMatches());
  160. // // this
  161. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  162. // assertTrue("Should match Client", ex.matchesMethodCall(a, foo).alwaysMatches());
  163. // assertTrue("Should not match A", ex.matchesMethodCall(a, a).neverMatches());
  164. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  165. // assertTrue("Should maybe match B", ex.matchesMethodCall(bsaa, a).maybeMatches());
  166. // assertFalse("Should maybe match B", ex.matchesMethodCall(bsaa, a).alwaysMatches());
  167. // // target
  168. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  169. // assertTrue("Should not match Client", ex.matchesMethodCall(a, a).neverMatches());
  170. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  171. // assertTrue("Should match A", ex.matchesMethodCall(a, a).alwaysMatches());
  172. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  173. // assertTrue("Should maybe match A", ex.matchesMethodCall(aa, a).maybeMatches());
  174. // assertFalse("Should maybe match A", ex.matchesMethodCall(aa, a).alwaysMatches());
  175. // // test args
  176. // ex = p.parsePointcutExpression("args(..,int)");
  177. // assertTrue("Should match A.aa", ex.matchesMethodCall(aa, a).alwaysMatches());
  178. // assertTrue("Should match A.aaa", ex.matchesMethodCall(aaa, a).alwaysMatches());
  179. // assertTrue("Should not match A.a", ex.matchesMethodCall(a, a).neverMatches());
  180. // // within
  181. // ex = p.parsePointcutExpression("within(*..A)");
  182. // assertTrue("Matches in class A", ex.matchesMethodCall(a, a).alwaysMatches());
  183. // assertTrue("Does not match in class B", ex.matchesMethodCall(a, b).neverMatches());
  184. // assertTrue("Matches in class A", ex.matchesMethodCall(a, A.class).alwaysMatches());
  185. // assertTrue("Does not match in class B", ex.matchesMethodCall(a, B.class).neverMatches());
  186. // // withincode
  187. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  188. // assertTrue("Should match", ex.matchesMethodCall(b, bsaa).alwaysMatches());
  189. // assertTrue("Should not match", ex.matchesMethodCall(b, b).neverMatches());
  190. // }
  191. // public void testMatchesConstructorCall() {
  192. // PointcutExpression ex = p.parsePointcutExpression("call(new(String))");
  193. // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  194. // assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, b).alwaysMatches());
  195. // assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
  196. // ex = p.parsePointcutExpression("call(*..A.new(String))");
  197. // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  198. // assertTrue("Should not match B(String)", ex.matchesConstructorCall(bsStringCons, foo).neverMatches());
  199. // // this
  200. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  201. // assertTrue("Should match Client", ex.matchesConstructorCall(asCons, foo).alwaysMatches());
  202. // assertTrue("Should not match A", ex.matchesConstructorCall(asCons, a).neverMatches());
  203. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  204. // assertTrue("Should maybe match B", ex.matchesConstructorCall(asCons, a).maybeMatches());
  205. // assertFalse("Should maybe match B", ex.matchesConstructorCall(asCons, a).alwaysMatches());
  206. // // target
  207. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  208. // assertTrue("Should not match Client", ex.matchesConstructorCall(asCons, foo).neverMatches());
  209. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  210. // assertTrue("Should not match A (no target)", ex.matchesConstructorCall(asCons, a).neverMatches());
  211. // // args
  212. // ex = p.parsePointcutExpression("args(String)");
  213. // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  214. // assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, foo).alwaysMatches());
  215. // assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
  216. // // within
  217. // ex = p.parsePointcutExpression("within(*..A)");
  218. // assertTrue("Matches in class A", ex.matchesConstructorCall(asCons, a).alwaysMatches());
  219. // assertTrue("Does not match in class B", ex.matchesConstructorCall(asCons, b).neverMatches());
  220. // // withincode
  221. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  222. // assertTrue("Should match", ex.matchesConstructorCall(bsCons, aa).alwaysMatches());
  223. // assertTrue("Should not match", ex.matchesConstructorCall(bsCons, b).neverMatches());
  224. // }
  225. //
  226. // public void testMatchesConstructorExecution() {
  227. // PointcutExpression ex = p.parsePointcutExpression("execution(new(String))");
  228. // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  229. // assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
  230. // assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
  231. // ex = p.parsePointcutExpression("execution(*..A.new(String))");
  232. // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  233. // assertTrue("Should not match B(String)", ex.matchesConstructorExecution(bsStringCons).neverMatches());
  234. //
  235. // // test this
  236. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  237. // assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  238. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  239. // assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
  240. // assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
  241. // assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
  242. // assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
  243. //
  244. // // test target
  245. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  246. // assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  247. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  248. // assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
  249. // assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
  250. // assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
  251. // assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
  252. //
  253. // // within
  254. // ex = p.parsePointcutExpression("within(*..A)");
  255. // assertTrue("Matches in class A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  256. // assertTrue("Does not match in class B", ex.matchesConstructorExecution(bsCons).neverMatches());
  257. //
  258. // // withincode
  259. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  260. // assertTrue("Does not match", ex.matchesConstructorExecution(bsCons).neverMatches());
  261. //
  262. // // args
  263. // ex = p.parsePointcutExpression("args(String)");
  264. // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  265. // assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
  266. // assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
  267. // }
  268. //
  269. // public void testMatchesAdviceExecution() {
  270. // PointcutExpression ex = p.parsePointcutExpression("adviceexecution()");
  271. // assertTrue("Should match (advice) A.a", ex.matchesAdviceExecution(a).alwaysMatches());
  272. // // test this
  273. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  274. // assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
  275. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  276. // assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
  277. // assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
  278. // assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
  279. //
  280. // // test target
  281. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  282. // assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
  283. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  284. // assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
  285. // assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
  286. // assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
  287. //
  288. // // test within
  289. // ex = p.parsePointcutExpression("within(*..A)");
  290. // assertTrue("Matches in class A", ex.matchesAdviceExecution(a).alwaysMatches());
  291. // assertTrue("Does not match in class B", ex.matchesAdviceExecution(b).neverMatches());
  292. //
  293. // // withincode
  294. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  295. // assertTrue("Does not match", ex.matchesAdviceExecution(a).neverMatches());
  296. //
  297. // // test args
  298. // ex = p.parsePointcutExpression("args(..,int)");
  299. // assertTrue("Should match A.aa", ex.matchesAdviceExecution(aa).alwaysMatches());
  300. // assertTrue("Should match A.aaa", ex.matchesAdviceExecution(aaa).alwaysMatches());
  301. // assertTrue("Should not match A.a", ex.matchesAdviceExecution(a).neverMatches());
  302. // }
  303. //
  304. // public void testMatchesHandler() {
  305. // PointcutExpression ex = p.parsePointcutExpression("handler(Exception)");
  306. // assertTrue("Should match catch(Exception)", ex.matchesHandler(Exception.class, Client.class).alwaysMatches());
  307. // assertTrue("Should not match catch(Throwable)", ex.matchesHandler(Throwable.class, Client.class).neverMatches());
  308. // // test this
  309. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  310. // assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  311. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  312. // assertTrue("Maybe matches B", ex.matchesHandler(Exception.class, a).maybeMatches());
  313. // assertFalse("Maybe matches B", ex.matchesHandler(Exception.class, a).alwaysMatches());
  314. // assertTrue("Does not match client", ex.matchesHandler(Exception.class, foo).neverMatches());
  315. // // target - no target for exception handlers
  316. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  317. // assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).neverMatches());
  318. // // args
  319. // ex = p.parsePointcutExpression("args(Exception)");
  320. // assertTrue("Should match Exception", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  321. // assertTrue("Should match RuntimeException", ex.matchesHandler(RuntimeException.class, foo).alwaysMatches());
  322. // assertTrue("Should not match String", ex.matchesHandler(String.class, foo).neverMatches());
  323. // assertTrue("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).maybeMatches());
  324. // assertFalse("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).alwaysMatches());
  325. // // within
  326. // ex = p.parsePointcutExpression("within(*..Client)");
  327. // assertTrue("Matches in class Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  328. // assertTrue("Does not match in class B", ex.matchesHandler(Exception.class, b).neverMatches());
  329. // // withincode
  330. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  331. // assertTrue("Matches within aa", ex.matchesHandler(Exception.class, aa).alwaysMatches());
  332. // assertTrue("Does not match within b", ex.matchesHandler(Exception.class, b).neverMatches());
  333. // }
  334. //
  335. // public void testMatchesInitialization() {
  336. // PointcutExpression ex = p.parsePointcutExpression("initialization(new(String))");
  337. // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  338. // assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
  339. // assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
  340. // ex = p.parsePointcutExpression("initialization(*..A.new(String))");
  341. // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  342. // assertTrue("Should not match B(String)", ex.matchesInitialization(bsStringCons).neverMatches());
  343. // // test this
  344. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  345. // assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
  346. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  347. // assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
  348. // assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
  349. //
  350. // // test target
  351. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  352. // assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
  353. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  354. // assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
  355. // assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
  356. // // within
  357. // ex = p.parsePointcutExpression("within(*..A)");
  358. // assertTrue("Matches in class A", ex.matchesInitialization(asCons).alwaysMatches());
  359. // assertTrue("Does not match in class B", ex.matchesInitialization(bsCons).neverMatches());
  360. // // withincode
  361. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  362. // assertTrue("Does not match", ex.matchesInitialization(bsCons).neverMatches());
  363. // // args
  364. // ex = p.parsePointcutExpression("args(String)");
  365. // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  366. // assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
  367. // assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
  368. // }
  369. //
  370. // public void testMatchesPreInitialization() {
  371. // PointcutExpression ex = p.parsePointcutExpression("preinitialization(new(String))");
  372. // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  373. // assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
  374. // assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
  375. // ex = p.parsePointcutExpression("preinitialization(*..A.new(String))");
  376. // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  377. // assertTrue("Should not match B(String)", ex.matchesPreInitialization(bsStringCons).neverMatches());
  378. // // test this
  379. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  380. // assertTrue("No match, no this at preinit", ex.matchesPreInitialization(asCons).neverMatches());
  381. //
  382. // // test target
  383. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  384. // assertTrue("No match, no target at preinit", ex.matchesPreInitialization(asCons).neverMatches());
  385. //
  386. // // within
  387. // ex = p.parsePointcutExpression("within(*..A)");
  388. // assertTrue("Matches in class A", ex.matchesPreInitialization(asCons).alwaysMatches());
  389. // assertTrue("Does not match in class B", ex.matchesPreInitialization(bsCons).neverMatches());
  390. // // withincode
  391. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  392. // assertTrue("Does not match", ex.matchesPreInitialization(bsCons).neverMatches());
  393. // // args
  394. // ex = p.parsePointcutExpression("args(String)");
  395. // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  396. // assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
  397. // assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
  398. // }
  399. //
  400. // public void testMatchesStaticInitialization() {
  401. // // staticinit
  402. // PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
  403. // assertTrue("Matches A", ex.matchesStaticInitialization(A.class).alwaysMatches());
  404. // assertTrue("Matches B", ex.matchesStaticInitialization(B.class).alwaysMatches());
  405. // assertTrue("Doesn't match Client", ex.matchesStaticInitialization(Client.class).neverMatches());
  406. // // this
  407. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  408. // assertTrue("No this", ex.matchesStaticInitialization(A.class).neverMatches());
  409. // // target
  410. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  411. // assertTrue("No target", ex.matchesStaticInitialization(A.class).neverMatches());
  412. //
  413. // // args
  414. // ex = p.parsePointcutExpression("args()");
  415. // assertTrue("No args", ex.matchesStaticInitialization(A.class).alwaysMatches());
  416. // ex = p.parsePointcutExpression("args(String)");
  417. // assertTrue("No args", ex.matchesStaticInitialization(A.class).neverMatches());
  418. //
  419. // // within
  420. // ex = p.parsePointcutExpression("within(*..A)");
  421. // assertTrue("Matches in class A", ex.matchesStaticInitialization(A.class).alwaysMatches());
  422. // assertTrue("Does not match in class B", ex.matchesStaticInitialization(B.class).neverMatches());
  423. //
  424. // // withincode
  425. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  426. // assertTrue("Does not match", ex.matchesStaticInitialization(A.class).neverMatches());
  427. // }
  428. //
  429. // public void testMatchesFieldSet() {
  430. // PointcutExpression ex = p.parsePointcutExpression("set(* *..A+.*)");
  431. // assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
  432. // assertTrue("matches y", ex.matchesFieldSet(y, foo).alwaysMatches());
  433. // assertTrue("does not match n", ex.matchesFieldSet(n, foo).neverMatches());
  434. // // this
  435. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  436. // assertTrue("matches Client", ex.matchesFieldSet(x, foo).alwaysMatches());
  437. // assertTrue("does not match A", ex.matchesFieldSet(n, a).neverMatches());
  438. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  439. // assertTrue("maybe matches A", ex.matchesFieldSet(x, a).maybeMatches());
  440. // assertFalse("maybe matches A", ex.matchesFieldSet(x, a).alwaysMatches());
  441. // // target
  442. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  443. // assertTrue("matches B", ex.matchesFieldSet(y, foo).alwaysMatches());
  444. // assertTrue("maybe matches A", ex.matchesFieldSet(x, foo).maybeMatches());
  445. // assertFalse("maybe matches A", ex.matchesFieldSet(x, foo).alwaysMatches());
  446. // // args
  447. // ex = p.parsePointcutExpression("args(int)");
  448. // assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
  449. // assertTrue("matches y", ex.matchesFieldSet(y, a).alwaysMatches());
  450. // assertTrue("does not match n", ex.matchesFieldSet(n, a).neverMatches());
  451. // // within
  452. // ex = p.parsePointcutExpression("within(*..A)");
  453. // assertTrue("Matches in class A", ex.matchesFieldSet(x, a).alwaysMatches());
  454. // assertTrue("Does not match in class B", ex.matchesFieldSet(x, b).neverMatches());
  455. // // withincode
  456. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  457. // assertTrue("Should match", ex.matchesFieldSet(x, aa).alwaysMatches());
  458. // assertTrue("Should not match", ex.matchesFieldSet(x, b).neverMatches());
  459. // }
  460. //
  461. // public void testMatchesFieldGet() {
  462. // PointcutExpression ex = p.parsePointcutExpression("get(* *..A+.*)");
  463. // assertTrue("matches x", ex.matchesFieldGet(x, a).alwaysMatches());
  464. // assertTrue("matches y", ex.matchesFieldGet(y, foo).alwaysMatches());
  465. // assertTrue("does not match n", ex.matchesFieldGet(n, foo).neverMatches());
  466. // // this
  467. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  468. // assertTrue("matches Client", ex.matchesFieldGet(x, foo).alwaysMatches());
  469. // assertTrue("does not match A", ex.matchesFieldGet(n, a).neverMatches());
  470. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  471. // assertTrue("maybe matches A", ex.matchesFieldGet(x, a).maybeMatches());
  472. // assertFalse("maybe matches A", ex.matchesFieldGet(x, a).alwaysMatches());
  473. // // target
  474. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  475. // assertTrue("matches B", ex.matchesFieldGet(y, foo).alwaysMatches());
  476. // assertTrue("maybe matches A", ex.matchesFieldGet(x, foo).maybeMatches());
  477. // assertFalse("maybe matches A", ex.matchesFieldGet(x, foo).alwaysMatches());
  478. // // args - no args at get join point
  479. // ex = p.parsePointcutExpression("args(int)");
  480. // assertTrue("matches x", ex.matchesFieldGet(x, a).neverMatches());
  481. // // within
  482. // ex = p.parsePointcutExpression("within(*..A)");
  483. // assertTrue("Matches in class A", ex.matchesFieldGet(x, a).alwaysMatches());
  484. // assertTrue("Does not match in class B", ex.matchesFieldGet(x, b).neverMatches());
  485. // // withincode
  486. // ex = p.parsePointcutExpression("withincode(* a*(..))");
  487. // assertTrue("Should match", ex.matchesFieldGet(x, aa).alwaysMatches());
  488. // assertTrue("Should not match", ex.matchesFieldGet(x, b).neverMatches());
  489. // }
  490. //
  491. // public void testArgsMatching() {
  492. // // too few args
  493. // PointcutExpression ex = p.parsePointcutExpression("args(*,*,*,*)");
  494. // assertTrue("Too few args", ex.matchesMethodExecution(foo).neverMatches());
  495. // assertTrue("Matching #args", ex.matchesMethodExecution(bar).alwaysMatches());
  496. // // one too few + ellipsis
  497. // ex = p.parsePointcutExpression("args(*,*,*,..)");
  498. // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(foo).alwaysMatches());
  499. // // exact number + ellipsis
  500. // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
  501. // assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
  502. // // too many + ellipsis
  503. // ex = p.parsePointcutExpression("args(*,..,*)");
  504. // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
  505. // assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
  506. // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(aaa).alwaysMatches());
  507. // // exact match
  508. // ex = p.parsePointcutExpression("args(String,int,Number)");
  509. // assertTrue("Matches exactly", ex.matchesMethodExecution(foo).alwaysMatches());
  510. // // maybe match
  511. // ex = p.parsePointcutExpression("args(String,int,Double)");
  512. // assertTrue("Matches maybe", ex.matchesMethodExecution(foo).maybeMatches());
  513. // assertFalse("Matches maybe", ex.matchesMethodExecution(foo).alwaysMatches());
  514. // // never match
  515. // ex = p.parsePointcutExpression("args(String,Integer,Number)");
  516. // if (LangUtil.isVMGreaterOrEqual(15)) {
  517. // assertTrue("matches", ex.matchesMethodExecution(foo).alwaysMatches());
  518. // } else {
  519. // assertTrue("Does not match", ex.matchesMethodExecution(foo).neverMatches());
  520. // }
  521. // }
  522. //
  523. // // public void testMatchesDynamically() {
  524. // // // everything other than this,target,args should just return true
  525. // // PointcutExpression ex = p.parsePointcutExpression("call(* *.*(..)) && execution(* *.*(..)) &&" +
  526. // // "get(* *) && set(* *) && initialization(new(..)) && preinitialization(new(..)) &&" +
  527. // // "staticinitialization(X) && adviceexecution() && within(Y) && withincode(* *.*(..)))");
  528. // // assertTrue("Matches dynamically",ex.matchesDynamically(a,b,new Object[0]));
  529. // // // this
  530. // // ex = p.parsePointcutExpression("this(String)");
  531. // // assertTrue("String matches",ex.matchesDynamically("",this,new Object[0]));
  532. // // assertFalse("Object doesn't match",ex.matchesDynamically(new Object(),this,new Object[0]));
  533. // // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  534. // // assertTrue("A matches",ex.matchesDynamically(new A(""),this,new Object[0]));
  535. // // assertTrue("B matches",ex.matchesDynamically(new B(""),this,new Object[0]));
  536. // // // target
  537. // // ex = p.parsePointcutExpression("target(String)");
  538. // // assertTrue("String matches",ex.matchesDynamically(this,"",new Object[0]));
  539. // // assertFalse("Object doesn't match",ex.matchesDynamically(this,new Object(),new Object[0]));
  540. // // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  541. // // assertTrue("A matches",ex.matchesDynamically(this,new A(""),new Object[0]));
  542. // // assertTrue("B matches",ex.matchesDynamically(this,new B(""),new Object[0]));
  543. // // // args
  544. // // ex = p.parsePointcutExpression("args(*,*,*,*)");
  545. // // assertFalse("Too few args",ex.matchesDynamically(null,null,new Object[]{a,b}));
  546. // // assertTrue("Matching #args",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  547. // // // one too few + ellipsis
  548. // // ex = p.parsePointcutExpression("args(*,*,*,..)");
  549. // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  550. // // // exact number + ellipsis
  551. // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
  552. // // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
  553. // // // too many + ellipsis
  554. // // ex = p.parsePointcutExpression("args(*,..,*)");
  555. // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  556. // // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a}));
  557. // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
  558. // // // exact match
  559. // // ex = p.parsePointcutExpression("args(String,int,Number)");
  560. // // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
  561. // // ex = p.parsePointcutExpression("args(String,Integer,Number)");
  562. // // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
  563. // // // never match
  564. // // ex = p.parsePointcutExpression("args(String,Integer,Number)");
  565. // // assertFalse("Does not match",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
  566. // // }
  567. //
  568. // public void testGetPointcutExpression() {
  569. // PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
  570. // assertEquals("staticinitialization(*..A+)", ex.getPointcutExpression());
  571. // }
  572. //
  573. // public void testCouldMatchJoinPointsInType() {
  574. // PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
  575. // assertTrue("Could maybe match String (as best we know at this point)", ex.couldMatchJoinPointsInType(String.class));
  576. // assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
  577. // ex = p.parsePointcutExpression("within(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  578. // assertFalse("Will never match String", ex.couldMatchJoinPointsInType(String.class));
  579. // assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
  580. // }
  581. //
  582. // public void testMayNeedDynamicTest() {
  583. // PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
  584. // assertFalse("No dynamic test needed", ex.mayNeedDynamicTest());
  585. // ex = p
  586. // .parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..)) && args(org.aspectj.weaver.tools.PointcutExpressionTest.X)");
  587. // assertTrue("Dynamic test needed", ex.mayNeedDynamicTest());
  588. // }
  589. // static class A {
  590. // public A(String s) {
  591. // }
  592. //
  593. // public void a() {
  594. // }
  595. //
  596. // public void aa(int i) {
  597. // }
  598. //
  599. // public void aaa(String s, int i) {
  600. // }
  601. //
  602. // int x;
  603. // }
  604. //
  605. // static class B extends A {
  606. // public B() {
  607. // super("");
  608. // }
  609. //
  610. // public B(String s) {
  611. // super(s);
  612. // }
  613. //
  614. // public String b() {
  615. // return null;
  616. // }
  617. //
  618. // public void aa(int i) {
  619. // }
  620. //
  621. // int y;
  622. // }
  623. //
  624. // static class Client {
  625. // public Client() {
  626. // }
  627. //
  628. // Number n;
  629. //
  630. // public void foo(String s, int i, Number n) {
  631. // }
  632. //
  633. // public void bar(String s, int i, Integer i2, Number n) {
  634. // }
  635. // }
  636. //
  637. // static class X {
  638. // }
  639. private ResolvedMember getMethod(ResolvedType type, String methodName, String methodSignature) {
  640. ResolvedMember[] methods = type.getDeclaredMethods();
  641. for (ResolvedMember method : methods) {
  642. if (method.getName().equals(methodName)
  643. && (methodSignature == null || methodSignature.equals(method.getSignature()))) {
  644. return method;
  645. }
  646. }
  647. return null;
  648. }
  649. @SuppressWarnings("unused")
  650. private void checkAlwaysMatches(String pointcutExpression, String type, String methodName, String methodSignature) {
  651. StandardPointcutExpression ex = pointcutParser.parsePointcutExpression(pointcutExpression);
  652. assertNotNull(ex);
  653. ResolvedType resolvedType = world.resolve(type);
  654. ResolvedMember method = getMethod(resolvedType, methodName, methodSignature);
  655. assertNotNull(method);
  656. boolean b = ex.matchesMethodExecution(method).alwaysMatches();
  657. assertTrue(b);
  658. }
  659. @SuppressWarnings("unused")
  660. private void checkNeverMatches(String pointcutExpression, String type, String methodName, String methodSignature) {
  661. StandardPointcutExpression ex = pointcutParser.parsePointcutExpression(pointcutExpression);
  662. assertNotNull(ex);
  663. ResolvedType resolvedType = world.resolve(type);
  664. ResolvedMember method = getMethod(resolvedType, methodName, methodSignature);
  665. assertNotNull(method);
  666. boolean b = ex.matchesMethodExecution(method).neverMatches();
  667. assertTrue(b);
  668. }
  669. }