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.

CommonPointcutExpressionTests.java 44KB

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