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.

PointcutExpressionTest.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.tools;
  12. import java.lang.reflect.Constructor;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.Method;
  15. import org.aspectj.util.LangUtil;
  16. import junit.framework.TestCase;
  17. public class PointcutExpressionTest extends TestCase {
  18. PointcutParser p;
  19. Constructor asCons;
  20. Constructor bsCons;
  21. Constructor bsStringCons;
  22. Constructor clientCons;
  23. Method a;
  24. Method aa;
  25. Method aaa;
  26. Field x;
  27. Field y;
  28. Method b;
  29. Method bsaa;
  30. Field n;
  31. Method foo;
  32. Method bar;
  33. public void testMatchesMethodCall() {
  34. PointcutExpression ex = p.parsePointcutExpression("call(* *..A.a*(..))");
  35. assertTrue("Should match call to A.a()", ex.matchesMethodCall(a, a).alwaysMatches());
  36. assertTrue("Should match call to A.aaa()", ex.matchesMethodCall(aaa, a).alwaysMatches());
  37. assertTrue("Should match call to B.aa()", ex.matchesMethodCall(bsaa, a).alwaysMatches());
  38. assertTrue("Should not match call to B.b()", ex.matchesMethodCall(b, a).neverMatches());
  39. ex = p.parsePointcutExpression("call(* *..A.a*(int))");
  40. assertTrue("Should match call to A.aa()", ex.matchesMethodCall(aa, a).alwaysMatches());
  41. assertTrue("Should not match call to A.a()", ex.matchesMethodCall(a, a).neverMatches());
  42. ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  43. assertTrue("Should match call to A.aaa() from Client", ex.matchesMethodCall(aaa, foo).alwaysMatches());
  44. ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  45. assertTrue("Should match call to A.aaa() from B", ex.matchesMethodCall(aaa, b).alwaysMatches());
  46. assertTrue("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).maybeMatches());
  47. assertFalse("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).alwaysMatches());
  48. ex = p.parsePointcutExpression("execution(* *.*(..))");
  49. assertTrue("Should not match call to A.aa", ex.matchesMethodCall(aa, a).neverMatches());
  50. // this
  51. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  52. assertTrue("Should match Client", ex.matchesMethodCall(a, foo).alwaysMatches());
  53. assertTrue("Should not match A", ex.matchesMethodCall(a, a).neverMatches());
  54. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  55. assertTrue("Should maybe match B", ex.matchesMethodCall(bsaa, a).maybeMatches());
  56. assertFalse("Should maybe match B", ex.matchesMethodCall(bsaa, a).alwaysMatches());
  57. // target
  58. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  59. assertTrue("Should not match Client", ex.matchesMethodCall(a, a).neverMatches());
  60. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  61. assertTrue("Should match A", ex.matchesMethodCall(a, a).alwaysMatches());
  62. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  63. assertTrue("Should maybe match A", ex.matchesMethodCall(aa, a).maybeMatches());
  64. assertFalse("Should maybe match A", ex.matchesMethodCall(aa, a).alwaysMatches());
  65. // test args
  66. ex = p.parsePointcutExpression("args(..,int)");
  67. assertTrue("Should match A.aa", ex.matchesMethodCall(aa, a).alwaysMatches());
  68. assertTrue("Should match A.aaa", ex.matchesMethodCall(aaa, a).alwaysMatches());
  69. assertTrue("Should not match A.a", ex.matchesMethodCall(a, a).neverMatches());
  70. // within
  71. ex = p.parsePointcutExpression("within(*..A)");
  72. assertTrue("Matches in class A", ex.matchesMethodCall(a, a).alwaysMatches());
  73. assertTrue("Does not match in class B", ex.matchesMethodCall(a, b).neverMatches());
  74. assertTrue("Matches in class A", ex.matchesMethodCall(a, A.class).alwaysMatches());
  75. assertTrue("Does not match in class B", ex.matchesMethodCall(a, B.class).neverMatches());
  76. // withincode
  77. ex = p.parsePointcutExpression("withincode(* a*(..))");
  78. assertTrue("Should match", ex.matchesMethodCall(b, bsaa).alwaysMatches());
  79. assertTrue("Should not match", ex.matchesMethodCall(b, b).neverMatches());
  80. }
  81. public void testMatchesMethodExecution() {
  82. PointcutExpression ex = p.parsePointcutExpression("execution(* *..A.aa(..))");
  83. assertTrue("Should match execution of A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
  84. assertTrue("Should match execution of B.aa", ex.matchesMethodExecution(bsaa).alwaysMatches());
  85. assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
  86. ex = p.parsePointcutExpression("call(* *..A.a*(int))");
  87. assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
  88. // test this
  89. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  90. assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
  91. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  92. assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
  93. assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
  94. // test target
  95. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  96. assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
  97. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  98. assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
  99. assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
  100. // test args
  101. ex = p.parsePointcutExpression("args(..,int)");
  102. assertTrue("Should match A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
  103. assertTrue("Should match A.aaa", ex.matchesMethodExecution(aaa).alwaysMatches());
  104. assertTrue("Should not match A.a", ex.matchesMethodExecution(a).neverMatches());
  105. // within
  106. ex = p.parsePointcutExpression("within(*..A)");
  107. assertTrue("Matches in class A", ex.matchesMethodExecution(a).alwaysMatches());
  108. assertTrue("Does not match in class B", ex.matchesMethodExecution(bsaa).neverMatches());
  109. // withincode
  110. ex = p.parsePointcutExpression("withincode(* a*(..))");
  111. assertTrue("Should not match", ex.matchesMethodExecution(a).neverMatches());
  112. }
  113. public void testMatchesConstructorCall() {
  114. PointcutExpression ex = p.parsePointcutExpression("call(new(String))");
  115. assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  116. assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, b).alwaysMatches());
  117. assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
  118. ex = p.parsePointcutExpression("call(*..A.new(String))");
  119. assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  120. assertTrue("Should not match B(String)", ex.matchesConstructorCall(bsStringCons, foo).neverMatches());
  121. // this
  122. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  123. assertTrue("Should match Client", ex.matchesConstructorCall(asCons, foo).alwaysMatches());
  124. assertTrue("Should not match A", ex.matchesConstructorCall(asCons, a).neverMatches());
  125. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  126. assertTrue("Should maybe match B", ex.matchesConstructorCall(asCons, a).maybeMatches());
  127. assertFalse("Should maybe match B", ex.matchesConstructorCall(asCons, a).alwaysMatches());
  128. // target
  129. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  130. assertTrue("Should not match Client", ex.matchesConstructorCall(asCons, foo).neverMatches());
  131. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  132. assertTrue("Should not match A (no target)", ex.matchesConstructorCall(asCons, a).neverMatches());
  133. // args
  134. ex = p.parsePointcutExpression("args(String)");
  135. assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
  136. assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, foo).alwaysMatches());
  137. assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
  138. // within
  139. ex = p.parsePointcutExpression("within(*..A)");
  140. assertTrue("Matches in class A", ex.matchesConstructorCall(asCons, a).alwaysMatches());
  141. assertTrue("Does not match in class B", ex.matchesConstructorCall(asCons, b).neverMatches());
  142. // withincode
  143. ex = p.parsePointcutExpression("withincode(* a*(..))");
  144. assertTrue("Should match", ex.matchesConstructorCall(bsCons, aa).alwaysMatches());
  145. assertTrue("Should not match", ex.matchesConstructorCall(bsCons, b).neverMatches());
  146. }
  147. public void testMatchesConstructorExecution() {
  148. PointcutExpression ex = p.parsePointcutExpression("execution(new(String))");
  149. assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  150. assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
  151. assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
  152. ex = p.parsePointcutExpression("execution(*..A.new(String))");
  153. assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  154. assertTrue("Should not match B(String)", ex.matchesConstructorExecution(bsStringCons).neverMatches());
  155. // test this
  156. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  157. assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  158. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  159. assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
  160. assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
  161. assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
  162. assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
  163. // test target
  164. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  165. assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  166. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  167. assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
  168. assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
  169. assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
  170. assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
  171. // within
  172. ex = p.parsePointcutExpression("within(*..A)");
  173. assertTrue("Matches in class A", ex.matchesConstructorExecution(asCons).alwaysMatches());
  174. assertTrue("Does not match in class B", ex.matchesConstructorExecution(bsCons).neverMatches());
  175. // withincode
  176. ex = p.parsePointcutExpression("withincode(* a*(..))");
  177. assertTrue("Does not match", ex.matchesConstructorExecution(bsCons).neverMatches());
  178. // args
  179. ex = p.parsePointcutExpression("args(String)");
  180. assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
  181. assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
  182. assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
  183. }
  184. public void testMatchesAdviceExecution() {
  185. PointcutExpression ex = p.parsePointcutExpression("adviceexecution()");
  186. assertTrue("Should match (advice) A.a", ex.matchesAdviceExecution(a).alwaysMatches());
  187. // test this
  188. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  189. assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
  190. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  191. assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
  192. assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
  193. assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
  194. // test target
  195. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  196. assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
  197. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  198. assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
  199. assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
  200. assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
  201. // test within
  202. ex = p.parsePointcutExpression("within(*..A)");
  203. assertTrue("Matches in class A", ex.matchesAdviceExecution(a).alwaysMatches());
  204. assertTrue("Does not match in class B", ex.matchesAdviceExecution(b).neverMatches());
  205. // withincode
  206. ex = p.parsePointcutExpression("withincode(* a*(..))");
  207. assertTrue("Does not match", ex.matchesAdviceExecution(a).neverMatches());
  208. // test args
  209. ex = p.parsePointcutExpression("args(..,int)");
  210. assertTrue("Should match A.aa", ex.matchesAdviceExecution(aa).alwaysMatches());
  211. assertTrue("Should match A.aaa", ex.matchesAdviceExecution(aaa).alwaysMatches());
  212. assertTrue("Should not match A.a", ex.matchesAdviceExecution(a).neverMatches());
  213. }
  214. public void testMatchesHandler() {
  215. PointcutExpression ex = p.parsePointcutExpression("handler(Exception)");
  216. assertTrue("Should match catch(Exception)", ex.matchesHandler(Exception.class, Client.class).alwaysMatches());
  217. assertTrue("Should not match catch(Throwable)", ex.matchesHandler(Throwable.class, Client.class).neverMatches());
  218. // test this
  219. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  220. assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  221. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  222. assertTrue("Maybe matches B", ex.matchesHandler(Exception.class, a).maybeMatches());
  223. assertFalse("Maybe matches B", ex.matchesHandler(Exception.class, a).alwaysMatches());
  224. assertTrue("Does not match client", ex.matchesHandler(Exception.class, foo).neverMatches());
  225. // target - no target for exception handlers
  226. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  227. assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).neverMatches());
  228. // args
  229. ex = p.parsePointcutExpression("args(Exception)");
  230. assertTrue("Should match Exception", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  231. assertTrue("Should match RuntimeException", ex.matchesHandler(RuntimeException.class, foo).alwaysMatches());
  232. assertTrue("Should not match String", ex.matchesHandler(String.class, foo).neverMatches());
  233. assertTrue("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).maybeMatches());
  234. assertFalse("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).alwaysMatches());
  235. // within
  236. ex = p.parsePointcutExpression("within(*..Client)");
  237. assertTrue("Matches in class Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
  238. assertTrue("Does not match in class B", ex.matchesHandler(Exception.class, b).neverMatches());
  239. // withincode
  240. ex = p.parsePointcutExpression("withincode(* a*(..))");
  241. assertTrue("Matches within aa", ex.matchesHandler(Exception.class, aa).alwaysMatches());
  242. assertTrue("Does not match within b", ex.matchesHandler(Exception.class, b).neverMatches());
  243. }
  244. public void testMatchesInitialization() {
  245. PointcutExpression ex = p.parsePointcutExpression("initialization(new(String))");
  246. assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  247. assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
  248. assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
  249. ex = p.parsePointcutExpression("initialization(*..A.new(String))");
  250. assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  251. assertTrue("Should not match B(String)", ex.matchesInitialization(bsStringCons).neverMatches());
  252. // test this
  253. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  254. assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
  255. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  256. assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
  257. assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
  258. // test target
  259. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  260. assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
  261. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  262. assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
  263. assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
  264. // within
  265. ex = p.parsePointcutExpression("within(*..A)");
  266. assertTrue("Matches in class A", ex.matchesInitialization(asCons).alwaysMatches());
  267. assertTrue("Does not match in class B", ex.matchesInitialization(bsCons).neverMatches());
  268. // withincode
  269. ex = p.parsePointcutExpression("withincode(* a*(..))");
  270. assertTrue("Does not match", ex.matchesInitialization(bsCons).neverMatches());
  271. // args
  272. ex = p.parsePointcutExpression("args(String)");
  273. assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
  274. assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
  275. assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
  276. }
  277. public void testMatchesPreInitialization() {
  278. PointcutExpression ex = p.parsePointcutExpression("preinitialization(new(String))");
  279. assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  280. assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
  281. assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
  282. ex = p.parsePointcutExpression("preinitialization(*..A.new(String))");
  283. assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  284. assertTrue("Should not match B(String)", ex.matchesPreInitialization(bsStringCons).neverMatches());
  285. // test this
  286. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  287. assertTrue("No match, no this at preinit", ex.matchesPreInitialization(asCons).neverMatches());
  288. // test target
  289. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  290. assertTrue("No match, no target at preinit", ex.matchesPreInitialization(asCons).neverMatches());
  291. // within
  292. ex = p.parsePointcutExpression("within(*..A)");
  293. assertTrue("Matches in class A", ex.matchesPreInitialization(asCons).alwaysMatches());
  294. assertTrue("Does not match in class B", ex.matchesPreInitialization(bsCons).neverMatches());
  295. // withincode
  296. ex = p.parsePointcutExpression("withincode(* a*(..))");
  297. assertTrue("Does not match", ex.matchesPreInitialization(bsCons).neverMatches());
  298. // args
  299. ex = p.parsePointcutExpression("args(String)");
  300. assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
  301. assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
  302. assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
  303. }
  304. public void testMatchesStaticInitialization() {
  305. // staticinit
  306. PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
  307. assertTrue("Matches A", ex.matchesStaticInitialization(A.class).alwaysMatches());
  308. assertTrue("Matches B", ex.matchesStaticInitialization(B.class).alwaysMatches());
  309. assertTrue("Doesn't match Client", ex.matchesStaticInitialization(Client.class).neverMatches());
  310. // this
  311. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  312. assertTrue("No this", ex.matchesStaticInitialization(A.class).neverMatches());
  313. // target
  314. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  315. assertTrue("No target", ex.matchesStaticInitialization(A.class).neverMatches());
  316. // args
  317. ex = p.parsePointcutExpression("args()");
  318. assertTrue("No args", ex.matchesStaticInitialization(A.class).alwaysMatches());
  319. ex = p.parsePointcutExpression("args(String)");
  320. assertTrue("No args", ex.matchesStaticInitialization(A.class).neverMatches());
  321. // within
  322. ex = p.parsePointcutExpression("within(*..A)");
  323. assertTrue("Matches in class A", ex.matchesStaticInitialization(A.class).alwaysMatches());
  324. assertTrue("Does not match in class B", ex.matchesStaticInitialization(B.class).neverMatches());
  325. // withincode
  326. ex = p.parsePointcutExpression("withincode(* a*(..))");
  327. assertTrue("Does not match", ex.matchesStaticInitialization(A.class).neverMatches());
  328. }
  329. public void testMatchesFieldSet() {
  330. PointcutExpression ex = p.parsePointcutExpression("set(* *..A+.*)");
  331. assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
  332. assertTrue("matches y", ex.matchesFieldSet(y, foo).alwaysMatches());
  333. assertTrue("does not match n", ex.matchesFieldSet(n, foo).neverMatches());
  334. // this
  335. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  336. assertTrue("matches Client", ex.matchesFieldSet(x, foo).alwaysMatches());
  337. assertTrue("does not match A", ex.matchesFieldSet(n, a).neverMatches());
  338. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  339. assertTrue("maybe matches A", ex.matchesFieldSet(x, a).maybeMatches());
  340. assertFalse("maybe matches A", ex.matchesFieldSet(x, a).alwaysMatches());
  341. // target
  342. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  343. assertTrue("matches B", ex.matchesFieldSet(y, foo).alwaysMatches());
  344. assertTrue("maybe matches A", ex.matchesFieldSet(x, foo).maybeMatches());
  345. assertFalse("maybe matches A", ex.matchesFieldSet(x, foo).alwaysMatches());
  346. // args
  347. ex = p.parsePointcutExpression("args(int)");
  348. assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
  349. assertTrue("matches y", ex.matchesFieldSet(y, a).alwaysMatches());
  350. assertTrue("does not match n", ex.matchesFieldSet(n, a).neverMatches());
  351. // within
  352. ex = p.parsePointcutExpression("within(*..A)");
  353. assertTrue("Matches in class A", ex.matchesFieldSet(x, a).alwaysMatches());
  354. assertTrue("Does not match in class B", ex.matchesFieldSet(x, b).neverMatches());
  355. // withincode
  356. ex = p.parsePointcutExpression("withincode(* a*(..))");
  357. assertTrue("Should match", ex.matchesFieldSet(x, aa).alwaysMatches());
  358. assertTrue("Should not match", ex.matchesFieldSet(x, b).neverMatches());
  359. }
  360. public void testMatchesFieldGet() {
  361. PointcutExpression ex = p.parsePointcutExpression("get(* *..A+.*)");
  362. assertTrue("matches x", ex.matchesFieldGet(x, a).alwaysMatches());
  363. assertTrue("matches y", ex.matchesFieldGet(y, foo).alwaysMatches());
  364. assertTrue("does not match n", ex.matchesFieldGet(n, foo).neverMatches());
  365. // this
  366. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
  367. assertTrue("matches Client", ex.matchesFieldGet(x, foo).alwaysMatches());
  368. assertTrue("does not match A", ex.matchesFieldGet(n, a).neverMatches());
  369. ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  370. assertTrue("maybe matches A", ex.matchesFieldGet(x, a).maybeMatches());
  371. assertFalse("maybe matches A", ex.matchesFieldGet(x, a).alwaysMatches());
  372. // target
  373. ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  374. assertTrue("matches B", ex.matchesFieldGet(y, foo).alwaysMatches());
  375. assertTrue("maybe matches A", ex.matchesFieldGet(x, foo).maybeMatches());
  376. assertFalse("maybe matches A", ex.matchesFieldGet(x, foo).alwaysMatches());
  377. // args - no args at get join point
  378. ex = p.parsePointcutExpression("args(int)");
  379. assertTrue("matches x", ex.matchesFieldGet(x, a).neverMatches());
  380. // within
  381. ex = p.parsePointcutExpression("within(*..A)");
  382. assertTrue("Matches in class A", ex.matchesFieldGet(x, a).alwaysMatches());
  383. assertTrue("Does not match in class B", ex.matchesFieldGet(x, b).neverMatches());
  384. // withincode
  385. ex = p.parsePointcutExpression("withincode(* a*(..))");
  386. assertTrue("Should match", ex.matchesFieldGet(x, aa).alwaysMatches());
  387. assertTrue("Should not match", ex.matchesFieldGet(x, b).neverMatches());
  388. }
  389. public void testArgsMatching() {
  390. // too few args
  391. PointcutExpression ex = p.parsePointcutExpression("args(*,*,*,*)");
  392. assertTrue("Too few args", ex.matchesMethodExecution(foo).neverMatches());
  393. assertTrue("Matching #args", ex.matchesMethodExecution(bar).alwaysMatches());
  394. // one too few + ellipsis
  395. ex = p.parsePointcutExpression("args(*,*,*,..)");
  396. assertTrue("Matches with ellipsis", ex.matchesMethodExecution(foo).alwaysMatches());
  397. // exact number + ellipsis
  398. assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
  399. assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
  400. // too many + ellipsis
  401. ex = p.parsePointcutExpression("args(*,..,*)");
  402. assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
  403. assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
  404. assertTrue("Matches with ellipsis", ex.matchesMethodExecution(aaa).alwaysMatches());
  405. // exact match
  406. ex = p.parsePointcutExpression("args(String,int,Number)");
  407. assertTrue("Matches exactly", ex.matchesMethodExecution(foo).alwaysMatches());
  408. // maybe match
  409. ex = p.parsePointcutExpression("args(String,int,Double)");
  410. assertTrue("Matches maybe", ex.matchesMethodExecution(foo).maybeMatches());
  411. assertFalse("Matches maybe", ex.matchesMethodExecution(foo).alwaysMatches());
  412. // never match
  413. ex = p.parsePointcutExpression("args(String,Integer,Number)");
  414. if (LangUtil.is15VMOrGreater()) {
  415. assertTrue("matches", ex.matchesMethodExecution(foo).alwaysMatches());
  416. } else {
  417. assertTrue("Does not match", ex.matchesMethodExecution(foo).neverMatches());
  418. }
  419. }
  420. // public void testMatchesDynamically() {
  421. // // everything other than this,target,args should just return true
  422. // PointcutExpression ex = p.parsePointcutExpression("call(* *.*(..)) && execution(* *.*(..)) &&" +
  423. // "get(* *) && set(* *) && initialization(new(..)) && preinitialization(new(..)) &&" +
  424. // "staticinitialization(X) && adviceexecution() && within(Y) && withincode(* *.*(..)))");
  425. // assertTrue("Matches dynamically",ex.matchesDynamically(a,b,new Object[0]));
  426. // // this
  427. // ex = p.parsePointcutExpression("this(String)");
  428. // assertTrue("String matches",ex.matchesDynamically("",this,new Object[0]));
  429. // assertFalse("Object doesn't match",ex.matchesDynamically(new Object(),this,new Object[0]));
  430. // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  431. // assertTrue("A matches",ex.matchesDynamically(new A(""),this,new Object[0]));
  432. // assertTrue("B matches",ex.matchesDynamically(new B(""),this,new Object[0]));
  433. // // target
  434. // ex = p.parsePointcutExpression("target(String)");
  435. // assertTrue("String matches",ex.matchesDynamically(this,"",new Object[0]));
  436. // assertFalse("Object doesn't match",ex.matchesDynamically(this,new Object(),new Object[0]));
  437. // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
  438. // assertTrue("A matches",ex.matchesDynamically(this,new A(""),new Object[0]));
  439. // assertTrue("B matches",ex.matchesDynamically(this,new B(""),new Object[0]));
  440. // // args
  441. // ex = p.parsePointcutExpression("args(*,*,*,*)");
  442. // assertFalse("Too few args",ex.matchesDynamically(null,null,new Object[]{a,b}));
  443. // assertTrue("Matching #args",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  444. // // one too few + ellipsis
  445. // ex = p.parsePointcutExpression("args(*,*,*,..)");
  446. // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  447. // // exact number + ellipsis
  448. // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
  449. // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
  450. // // too many + ellipsis
  451. // ex = p.parsePointcutExpression("args(*,..,*)");
  452. // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
  453. // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a}));
  454. // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
  455. // // exact match
  456. // ex = p.parsePointcutExpression("args(String,int,Number)");
  457. // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
  458. // ex = p.parsePointcutExpression("args(String,Integer,Number)");
  459. // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
  460. // // never match
  461. // ex = p.parsePointcutExpression("args(String,Integer,Number)");
  462. // assertFalse("Does not match",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
  463. // }
  464. public void testGetPointcutExpression() {
  465. PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
  466. assertEquals("staticinitialization(*..A+)", ex.getPointcutExpression());
  467. }
  468. public void testCouldMatchJoinPointsInType() {
  469. PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
  470. assertFalse("Could maybe match String (as best we know at this point)", ex.couldMatchJoinPointsInType(String.class));
  471. assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
  472. ex = p.parsePointcutExpression("within(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
  473. assertFalse("Will never match String", ex.couldMatchJoinPointsInType(String.class));
  474. assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
  475. }
  476. public void testMayNeedDynamicTest() {
  477. PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
  478. assertFalse("No dynamic test needed", ex.mayNeedDynamicTest());
  479. ex = p
  480. .parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..)) && args(org.aspectj.weaver.tools.PointcutExpressionTest.X)");
  481. assertTrue("Dynamic test needed", ex.mayNeedDynamicTest());
  482. }
  483. protected void setUp() throws Exception {
  484. super.setUp();
  485. p = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass()
  486. .getClassLoader());
  487. asCons = A.class.getConstructor(new Class[] { String.class });
  488. bsCons = B.class.getConstructor(new Class[0]);
  489. bsStringCons = B.class.getConstructor(new Class[] { String.class });
  490. a = A.class.getMethod("a", new Class[0]);
  491. aa = A.class.getMethod("aa", new Class[] { int.class });
  492. aaa = A.class.getMethod("aaa", new Class[] { String.class, int.class });
  493. x = A.class.getDeclaredField("x");
  494. y = B.class.getDeclaredField("y");
  495. b = B.class.getMethod("b", new Class[0]);
  496. bsaa = B.class.getMethod("aa", new Class[] { int.class });
  497. clientCons = Client.class.getConstructor(new Class[0]);
  498. n = Client.class.getDeclaredField("n");
  499. foo = Client.class.getDeclaredMethod("foo", new Class[] { String.class, int.class, Number.class });
  500. bar = Client.class.getDeclaredMethod("bar", new Class[] { String.class, int.class, Integer.class, Number.class });
  501. }
  502. static class A {
  503. public A(String s) {
  504. }
  505. public void a() {
  506. }
  507. public void aa(int i) {
  508. }
  509. public void aaa(String s, int i) {
  510. }
  511. int x;
  512. }
  513. static class B extends A {
  514. public B() {
  515. super("");
  516. }
  517. public B(String s) {
  518. super(s);
  519. }
  520. public String b() {
  521. return null;
  522. }
  523. public void aa(int i) {
  524. }
  525. int y;
  526. }
  527. static class Client {
  528. public Client() {
  529. }
  530. Number n;
  531. public void foo(String s, int i, Number n) {
  532. }
  533. public void bar(String s, int i, Integer i2, Number n) {
  534. }
  535. }
  536. static class X {
  537. }
  538. }