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.

Java15PointcutExpressionTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.tools;
  13. import java.lang.annotation.Retention;
  14. import java.lang.annotation.RetentionPolicy;
  15. import java.lang.reflect.Method;
  16. import org.aspectj.lang.annotation.Pointcut;
  17. import junit.framework.Test;
  18. import junit.framework.TestCase;
  19. import junit.framework.TestSuite;
  20. /**
  21. * @author colyer
  22. *
  23. */
  24. public class Java15PointcutExpressionTest extends TestCase {
  25. public static Test suite() {
  26. TestSuite suite = new TestSuite("Java15PointcutExpressionTest");
  27. suite.addTestSuite(Java15PointcutExpressionTest.class);
  28. return suite;
  29. }
  30. private PointcutParser parser;
  31. private Method a;
  32. private Method b;
  33. private Method c;
  34. public void testAtThis() {
  35. PointcutExpression atThis = parser.parsePointcutExpression("@this(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  36. ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
  37. ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
  38. assertTrue("maybe matches A",sMatch1.maybeMatches());
  39. assertTrue("maybe matches B",sMatch2.maybeMatches());
  40. JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
  41. assertFalse("does not match",jp1.matches());
  42. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
  43. assertTrue("matches",jp2.matches());
  44. }
  45. public void testAtTarget() {
  46. PointcutExpression atTarget = parser.parsePointcutExpression("@target(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  47. ShadowMatch sMatch1 = atTarget.matchesMethodExecution(a);
  48. ShadowMatch sMatch2 = atTarget.matchesMethodExecution(b);
  49. assertTrue("maybe matches A",sMatch1.maybeMatches());
  50. assertTrue("maybe matches B",sMatch2.maybeMatches());
  51. JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
  52. assertFalse("does not match",jp1.matches());
  53. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
  54. assertTrue("matches",jp2.matches());
  55. }
  56. public void testAtThisWithBinding() {
  57. PointcutParameter param = parser.createPointcutParameter("a",MyAnnotation.class);
  58. B myB = new B();
  59. MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
  60. PointcutExpression atThis = parser.parsePointcutExpression("@this(a)",A.class,new PointcutParameter[] {param});
  61. ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
  62. ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
  63. assertTrue("maybe matches A",sMatch1.maybeMatches());
  64. assertTrue("maybe matches B",sMatch2.maybeMatches());
  65. JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
  66. assertFalse("does not match",jp1.matches());
  67. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(myB, myB, new Object[0]);
  68. assertTrue("matches",jp2.matches());
  69. assertEquals(1,jp2.getParameterBindings().length);
  70. assertEquals("should be myB's annotation",bAnnotation,jp2.getParameterBindings()[0].getBinding());
  71. }
  72. public void testAtTargetWithBinding() {
  73. PointcutParameter param = parser.createPointcutParameter("a",MyAnnotation.class);
  74. B myB = new B();
  75. MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
  76. PointcutExpression atThis = parser.parsePointcutExpression("@target(a)",A.class,new PointcutParameter[] {param});
  77. ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
  78. ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
  79. assertTrue("maybe matches A",sMatch1.maybeMatches());
  80. assertTrue("maybe matches B",sMatch2.maybeMatches());
  81. JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
  82. assertFalse("does not match",jp1.matches());
  83. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(myB, myB, new Object[0]);
  84. assertTrue("matches",jp2.matches());
  85. assertEquals(1,jp2.getParameterBindings().length);
  86. assertEquals("should be myB's annotation",bAnnotation,jp2.getParameterBindings()[0].getBinding());
  87. }
  88. public void testAtArgs() {
  89. PointcutExpression atArgs = parser.parsePointcutExpression("@args(..,org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  90. ShadowMatch sMatch1 = atArgs.matchesMethodExecution(a);
  91. ShadowMatch sMatch2 = atArgs.matchesMethodExecution(c);
  92. assertTrue("never matches A",sMatch1.neverMatches());
  93. assertTrue("maybe matches C",sMatch2.maybeMatches());
  94. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[]{new A(),new B()});
  95. assertTrue("matches",jp2.matches());
  96. atArgs = parser.parsePointcutExpression("@args(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation,org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  97. sMatch1 = atArgs.matchesMethodExecution(a);
  98. sMatch2 = atArgs.matchesMethodExecution(c);
  99. assertTrue("never matches A",sMatch1.neverMatches());
  100. assertTrue("maybe matches C",sMatch2.maybeMatches());
  101. JoinPointMatch jp1 = sMatch2.matchesJoinPoint(new A(), new A(), new Object[] {new A(), new B()});
  102. assertFalse("does not match",jp1.matches());
  103. jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[] {new B(),new B()});
  104. assertTrue("matches",jp2.matches());
  105. }
  106. public void testAtArgsWithBinding() {
  107. PointcutParameter p1 = parser.createPointcutParameter("a",MyAnnotation.class);
  108. PointcutParameter p2 = parser.createPointcutParameter("b", MyAnnotation.class);
  109. PointcutExpression atArgs = parser.parsePointcutExpression("@args(..,a)",A.class,new PointcutParameter[] {p1});
  110. ShadowMatch sMatch2 = atArgs.matchesMethodExecution(c);
  111. assertTrue("maybe matches C",sMatch2.maybeMatches());
  112. JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[]{new A(),new B()});
  113. assertTrue("matches",jp2.matches());
  114. assertEquals(1,jp2.getParameterBindings().length);
  115. MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
  116. assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[0].getBinding());
  117. atArgs = parser.parsePointcutExpression("@args(a,b)",A.class,new PointcutParameter[] {p1,p2});
  118. sMatch2 = atArgs.matchesMethodExecution(c);
  119. assertTrue("maybe matches C",sMatch2.maybeMatches());
  120. jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[] {new B(),new B()});
  121. assertTrue("matches",jp2.matches());
  122. assertEquals(2,jp2.getParameterBindings().length);
  123. assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[0].getBinding());
  124. assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[1].getBinding());
  125. }
  126. public void testAtWithin() {
  127. PointcutExpression atWithin = parser.parsePointcutExpression("@within(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  128. ShadowMatch sMatch1 = atWithin.matchesMethodExecution(a);
  129. ShadowMatch sMatch2 = atWithin.matchesMethodExecution(b);
  130. assertTrue("does not match a",sMatch1.neverMatches());
  131. assertTrue("matches b",sMatch2.alwaysMatches());
  132. }
  133. public void testAtWithinWithBinding() {
  134. PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
  135. PointcutExpression atWithin = parser.parsePointcutExpression("@within(x)",B.class,new PointcutParameter[] {p1});
  136. ShadowMatch sMatch1 = atWithin.matchesMethodExecution(a);
  137. ShadowMatch sMatch2 = atWithin.matchesMethodExecution(b);
  138. assertTrue("does not match a",sMatch1.neverMatches());
  139. assertTrue("matches b",sMatch2.alwaysMatches());
  140. JoinPointMatch jpm = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
  141. assertTrue(jpm.matches());
  142. assertEquals(1,jpm.getParameterBindings().length);
  143. MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
  144. assertEquals("annotation on B",bAnnotation,jpm.getParameterBindings()[0].getBinding());
  145. }
  146. public void testAtWithinCode() {
  147. PointcutExpression atWithinCode = parser.parsePointcutExpression("@withincode(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  148. ShadowMatch sMatch1 = atWithinCode.matchesMethodCall(a,b);
  149. ShadowMatch sMatch2 = atWithinCode.matchesMethodCall(a,a);
  150. assertTrue("does not match from b",sMatch1.neverMatches());
  151. assertTrue("matches from a",sMatch2.alwaysMatches());
  152. }
  153. public void testAtWithinCodeWithBinding() {
  154. PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
  155. PointcutExpression atWithinCode = parser.parsePointcutExpression("@withincode(x)",A.class,new PointcutParameter[] {p1});
  156. ShadowMatch sMatch2 = atWithinCode.matchesMethodCall(a,a);
  157. assertTrue("matches from a",sMatch2.alwaysMatches());
  158. JoinPointMatch jpm = sMatch2.matchesJoinPoint(new A(), new A(), new Object[0]);
  159. assertEquals(1,jpm.getParameterBindings().length);
  160. MyAnnotation annOna = a.getAnnotation(MyAnnotation.class);
  161. assertEquals("MyAnnotation on a",annOna,jpm.getParameterBindings()[0].getBinding());
  162. }
  163. public void testAtAnnotation() {
  164. PointcutExpression atAnnotation = parser.parsePointcutExpression("@annotation(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
  165. ShadowMatch sMatch1 = atAnnotation.matchesMethodCall(b,a);
  166. ShadowMatch sMatch2 = atAnnotation.matchesMethodCall(a,a);
  167. assertTrue("does not match call to b",sMatch1.neverMatches());
  168. assertTrue("matches call to a",sMatch2.alwaysMatches());
  169. }
  170. public void testAtAnnotationWithBinding() {
  171. PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
  172. PointcutExpression atAnnotation = parser.parsePointcutExpression("@annotation(x)",A.class,new PointcutParameter[] {p1});
  173. ShadowMatch sMatch2 = atAnnotation.matchesMethodCall(a,a);
  174. assertTrue("matches call to a",sMatch2.alwaysMatches());
  175. JoinPointMatch jpm = sMatch2.matchesJoinPoint(new A(), new A(), new Object[0]);
  176. assertTrue(jpm.matches());
  177. assertEquals(1,jpm.getParameterBindings().length);
  178. MyAnnotation annOna = a.getAnnotation(MyAnnotation.class);
  179. assertEquals("MyAnnotation on a",annOna,jpm.getParameterBindings()[0].getBinding());
  180. }
  181. public void testReferencePointcutNoParams() {
  182. PointcutExpression pc = parser.parsePointcutExpression("foo()",C.class,new PointcutParameter[0]);
  183. ShadowMatch sMatch1 = pc.matchesMethodCall(a,b);
  184. ShadowMatch sMatch2 = pc.matchesMethodExecution(a);
  185. assertTrue("no match on call",sMatch1.neverMatches());
  186. assertTrue("match on execution",sMatch2.alwaysMatches());
  187. pc = parser.parsePointcutExpression("org.aspectj.weaver.tools.Java15PointcutExpressionTest.C.foo()");
  188. sMatch1 = pc.matchesMethodCall(a,b);
  189. sMatch2 = pc.matchesMethodExecution(a);
  190. assertTrue("no match on call",sMatch1.neverMatches());
  191. assertTrue("match on execution",sMatch2.alwaysMatches());
  192. }
  193. public void testReferencePointcutParams() {
  194. PointcutParameter p1 = parser.createPointcutParameter("x",A.class);
  195. PointcutExpression pc = parser.parsePointcutExpression("goo(x)",C.class,new PointcutParameter[] {p1});
  196. ShadowMatch sMatch1 = pc.matchesMethodCall(a,b);
  197. ShadowMatch sMatch2 = pc.matchesMethodExecution(a);
  198. assertTrue("no match on call",sMatch1.neverMatches());
  199. assertTrue("match on execution",sMatch2.maybeMatches());
  200. A anA = new A();
  201. JoinPointMatch jpm = sMatch2.matchesJoinPoint(anA, new A(), new Object[0]);
  202. assertTrue(jpm.matches());
  203. assertEquals("should be bound to anA",anA,jpm.getParameterBindings()[0].getBinding());
  204. }
  205. public void testExecutionWithClassFileRetentionAnnotation() {
  206. PointcutExpression pc1 = parser.parsePointcutExpression("execution(@org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation * *(..))");
  207. PointcutExpression pc2 = parser.parsePointcutExpression("execution(@org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyClassFileRetentionAnnotation * *(..))");
  208. ShadowMatch sMatch = pc1.matchesMethodExecution(a);
  209. assertTrue("matches",sMatch.alwaysMatches());
  210. sMatch = pc2.matchesMethodExecution(a);
  211. assertTrue("no match",sMatch.neverMatches());
  212. sMatch = pc1.matchesMethodExecution(b);
  213. assertTrue("no match",sMatch.neverMatches());
  214. sMatch = pc2.matchesMethodExecution(b);
  215. assertTrue("matches",sMatch.alwaysMatches());
  216. }
  217. protected void setUp() throws Exception {
  218. super.setUp();
  219. parser = new PointcutParser();
  220. a = A.class.getMethod("a");
  221. b = B.class.getMethod("b");
  222. c = B.class.getMethod("c",new Class[] {A.class,B.class});
  223. }
  224. @Retention(RetentionPolicy.RUNTIME)
  225. private @interface MyAnnotation {}
  226. private @interface MyClassFileRetentionAnnotation {}
  227. private static class A {
  228. @MyAnnotation public void a() {}
  229. }
  230. @MyAnnotation
  231. private static class B {
  232. @MyClassFileRetentionAnnotation public void b() {}
  233. public void c(A anA, B aB) {}
  234. }
  235. private static class C {
  236. @Pointcut("execution(* *(..))")
  237. public void foo() {}
  238. @Pointcut(value="execution(* *(..)) && this(x)", argNames="x")
  239. public void goo(A x) {}
  240. }
  241. }