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.

AjTypeWithAspectsTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.internal.lang.reflect;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.Method;
  15. import java.lang.reflect.Modifier;
  16. import junit.framework.TestCase;
  17. import org.aspectj.internal.lang.annotation.ajcDeclareEoW;
  18. import org.aspectj.internal.lang.annotation.ajcPrivileged;
  19. import org.aspectj.lang.annotation.AdviceName;
  20. import org.aspectj.lang.annotation.After;
  21. import org.aspectj.lang.annotation.AfterReturning;
  22. import org.aspectj.lang.annotation.AfterThrowing;
  23. import org.aspectj.lang.annotation.Around;
  24. import org.aspectj.lang.annotation.Aspect;
  25. import org.aspectj.lang.annotation.Before;
  26. import org.aspectj.lang.annotation.DeclareError;
  27. import org.aspectj.lang.annotation.DeclareWarning;
  28. import org.aspectj.lang.reflect.Advice;
  29. import org.aspectj.lang.reflect.AdviceKind;
  30. import org.aspectj.lang.reflect.AjType;
  31. import org.aspectj.lang.reflect.AjTypeSystem;
  32. import org.aspectj.lang.reflect.DeclareErrorOrWarning;
  33. import org.aspectj.lang.reflect.NoSuchAdviceException;
  34. import org.aspectj.lang.reflect.NoSuchPointcutException;
  35. import org.aspectj.lang.reflect.PerClause;
  36. import org.aspectj.lang.reflect.PerClauseKind;
  37. import org.aspectj.lang.reflect.Pointcut;
  38. import org.aspectj.lang.reflect.PointcutBasedPerClause;
  39. import org.aspectj.lang.reflect.TypePatternBasedPerClause;
  40. public class AjTypeWithAspectsTest extends TestCase {
  41. private AjType<SimpleAspect> sa;
  42. protected void setUp() throws Exception {
  43. super.setUp();
  44. sa = AjTypeSystem.getAjType(SimpleAspect.class);
  45. }
  46. public void testGetPerClause() {
  47. AjType<PerThisAspect> perThisA = AjTypeSystem.getAjType(PerThisAspect.class);
  48. AjType<PerTargetAspect> perTargetA = AjTypeSystem.getAjType(PerTargetAspect.class);
  49. AjType<PerCflowAspect> perCflowA = AjTypeSystem.getAjType(PerCflowAspect.class);
  50. AjType<PerCflowbelowAspect> perCflowbelowA = AjTypeSystem.getAjType(PerCflowbelowAspect.class);
  51. AjType<PerTypeWithin> perTypeWithinA = AjTypeSystem.getAjType(PerTypeWithin.class);
  52. PerClause pc = perThisA.getPerClause();
  53. assertEquals(PerClauseKind.PERTHIS,pc.getKind());
  54. assertEquals("pc()",((PointcutBasedPerClause)pc).getPointcutExpression().asString());
  55. assertEquals("perthis(pc())",pc.toString());
  56. pc= perTargetA.getPerClause();
  57. assertEquals(PerClauseKind.PERTARGET,pc.getKind());
  58. assertEquals("pc()",((PointcutBasedPerClause)pc).getPointcutExpression().asString());
  59. assertEquals("pertarget(pc())",pc.toString());
  60. pc= perCflowA.getPerClause();
  61. assertEquals(PerClauseKind.PERCFLOW,pc.getKind());
  62. assertEquals("pc()",((PointcutBasedPerClause)pc).getPointcutExpression().asString());
  63. assertEquals("percflow(pc())",pc.toString());
  64. pc= perCflowbelowA.getPerClause();
  65. assertEquals(PerClauseKind.PERCFLOWBELOW,pc.getKind());
  66. assertEquals("pc()",((PointcutBasedPerClause)pc).getPointcutExpression().asString());
  67. assertEquals("percflowbelow(pc())",pc.toString());
  68. pc= perTypeWithinA.getPerClause();
  69. assertEquals(PerClauseKind.PERTYPEWITHIN,pc.getKind());
  70. assertEquals("org.aspectj..*",((TypePatternBasedPerClause)pc).getTypePattern().asString());
  71. assertEquals("pertypewithin(org.aspectj..*)",pc.toString());
  72. }
  73. public void testGetDeclaredField() throws Exception{
  74. Field f = sa.getDeclaredField("s");
  75. try {
  76. Field f2 = sa.getDeclaredField("ajc$xyz$s");
  77. fail("Expecting NoSuchFieldException");
  78. } catch (NoSuchFieldException nsf) {}
  79. }
  80. public void testGetField() throws Exception {
  81. Field f = sa.getField("s");
  82. try {
  83. Field f2 = sa.getField("ajc$xyz$s");
  84. fail("Expecting NoSuchFieldException");
  85. } catch (NoSuchFieldException nsf) {}
  86. }
  87. public void testGetDeclaredFields() {
  88. Field[] fields = sa.getDeclaredFields();
  89. assertEquals(1,fields.length);
  90. assertEquals("s",fields[0].getName());
  91. }
  92. public void testGetFields() {
  93. Field[] fields = sa.getFields();
  94. assertEquals(1,fields.length);
  95. assertEquals("s",fields[0].getName());
  96. }
  97. public void testGetDeclaredMethod() throws Exception {
  98. Method m = sa.getDeclaredMethod("aMethod");
  99. try {
  100. Method m2 = sa.getDeclaredMethod("logEntry");
  101. fail("Expecting NoSuchMethodException");
  102. } catch(NoSuchMethodException ex) {}
  103. try {
  104. Method m3 = sa.getDeclaredMethod("ajc$before$123");
  105. fail("Expecting NoSuchMethodException");
  106. } catch(NoSuchMethodException ex) {}
  107. }
  108. public void testGetMethod() throws Exception {
  109. Method m = sa.getMethod("aMethod");
  110. try {
  111. Method m2 = sa.getMethod("logEntry");
  112. fail("Expecting NoSuchMethodException");
  113. } catch(NoSuchMethodException ex) {}
  114. try {
  115. Method m3 = sa.getMethod("ajc$before$123");
  116. fail("Expecting NoSuchMethodException");
  117. } catch(NoSuchMethodException ex) {}
  118. }
  119. public void testGetDeclaredMethods() {
  120. Method[] ms = sa.getDeclaredMethods();
  121. assertEquals(1,ms.length);
  122. assertEquals("aMethod",ms[0].getName());
  123. }
  124. public void testGetMethods() {
  125. Method[] ms = sa.getMethods();
  126. assertEquals(10,ms.length);
  127. //AV was corrupted, cannot rely on ordering
  128. String match = "";
  129. for (int i = 0; i < ms.length; i++) {
  130. match = match + "--" + ms[i].getName();
  131. }
  132. assertTrue(match.indexOf("aMethod") >=0);
  133. }
  134. public void testGetDeclaredPointcut() throws Exception {
  135. Pointcut p1 = sa.getDeclaredPointcut("simpleAspectMethodExecution");
  136. assertEquals("simpleAspectMethodExecution",p1.getName());
  137. assertEquals("execution(* SimpleAspect.*(..))",p1.getPointcutExpression().asString());
  138. assertEquals("simpleAspectMethodExecution() : execution(* SimpleAspect.*(..))",p1.toString());
  139. assertEquals(sa,p1.getDeclaringType());
  140. assertEquals(0,p1.getParameterTypes().length);
  141. assertTrue(Modifier.isPublic(p1.getModifiers()));
  142. Pointcut p2 = sa.getDeclaredPointcut("simpleAspectCall");
  143. assertEquals("simpleAspectCall",p2.getName());
  144. assertEquals("call(* SimpleAspect.*(..))",p2.getPointcutExpression().asString());
  145. assertEquals(sa,p2.getDeclaringType());
  146. assertEquals(1,p2.getParameterTypes().length);
  147. assertTrue(Modifier.isPrivate(p2.getModifiers()));
  148. try {
  149. Pointcut p3 = sa.getDeclaredPointcut("sausages");
  150. fail("Expecting NoSuchPointcutExcetpion");
  151. } catch (NoSuchPointcutException ex) {
  152. assertEquals("sausages",ex.getName());
  153. }
  154. }
  155. public void testGetPointcut() throws Exception {
  156. Pointcut p1 = sa.getPointcut("simpleAspectMethodExecution");
  157. assertEquals("simpleAspectMethodExecution",p1.getName());
  158. assertEquals("execution(* SimpleAspect.*(..))",p1.getPointcutExpression().asString());
  159. assertEquals(sa,p1.getDeclaringType());
  160. assertEquals(0,p1.getParameterTypes().length);
  161. assertTrue(Modifier.isPublic(p1.getModifiers()));
  162. Pointcut p2 = sa.getDeclaredPointcut("simpleAspectCall");
  163. assertEquals("simpleAspectCall",p2.getName());
  164. assertEquals("call(* SimpleAspect.*(..))",p2.getPointcutExpression().asString());
  165. assertEquals(sa,p2.getDeclaringType());
  166. assertEquals(1,p2.getParameterTypes().length);
  167. assertTrue(Modifier.isPrivate(p2.getModifiers()));
  168. try {
  169. Pointcut p3 = sa.getPointcut("sausages");
  170. fail("Expecting NoSuchPointcutExcetpion");
  171. } catch (NoSuchPointcutException ex) {
  172. assertEquals("sausages",ex.getName());
  173. }
  174. }
  175. public void testGetDeclaredPointcuts() {
  176. Pointcut[] pcs = sa.getDeclaredPointcuts();
  177. assertEquals(2,pcs.length);
  178. // AV was corrupted, cannot rely on ordering
  179. String match = "simpleAspectMethodExecution--simpleAspectCall";
  180. assertTrue(match.indexOf(pcs[0].getName()) >= 0);
  181. assertTrue(match.indexOf(pcs[1].getName()) >= 0);
  182. }
  183. public void testGetPointcuts() {
  184. Pointcut[] pcs = sa.getPointcuts();
  185. assertEquals(1,pcs.length);
  186. assertEquals("simpleAspectMethodExecution",pcs[0].getName());
  187. }
  188. public void testGetDeclaredAdvice() {
  189. Advice[] advice = sa.getDeclaredAdvice();
  190. assertEquals(10,advice.length);
  191. advice = sa.getDeclaredAdvice(AdviceKind.BEFORE);
  192. assertEquals(2,advice.length);
  193. advice = sa.getDeclaredAdvice(AdviceKind.AFTER);
  194. assertEquals(2,advice.length);
  195. advice = sa.getDeclaredAdvice(AdviceKind.AFTER_RETURNING);
  196. assertEquals(2,advice.length);
  197. advice = sa.getDeclaredAdvice(AdviceKind.AFTER_THROWING);
  198. assertEquals(2,advice.length);
  199. advice = sa.getDeclaredAdvice(AdviceKind.AROUND);
  200. assertEquals(2,advice.length);
  201. advice = sa.getDeclaredAdvice(AdviceKind.BEFORE,AdviceKind.AFTER);
  202. assertEquals(4,advice.length);
  203. advice = sa.getDeclaredAdvice(AdviceKind.BEFORE);
  204. // AV: corrupted test: cannot rely on ordering since a Set is used behind
  205. Advice aone, atwo;
  206. if (advice[0].getName()!=null && advice[0].getName().length()>0) {
  207. aone = advice[0];
  208. atwo = advice[1];
  209. } else {
  210. aone = advice[1];
  211. atwo = advice[0];
  212. }
  213. assertEquals("execution(* SimpleAspect.*(..))",aone.getPointcutExpression().toString());
  214. assertEquals("@AdviceName(\"logEntry\") before() : execution(* SimpleAspect.*(..))",aone.toString());
  215. assertEquals("logEntry",aone.getName());
  216. assertEquals(AdviceKind.BEFORE,aone.getKind());
  217. assertEquals("execution(* SimpleAspect.*(..))",atwo.getPointcutExpression().toString());
  218. assertEquals("",atwo.getName());
  219. assertEquals("before() : execution(* SimpleAspect.*(..))",atwo.toString());
  220. }
  221. public void testGetAdvice() {
  222. Advice[] advice = sa.getDeclaredAdvice();
  223. assertEquals(10,advice.length);
  224. advice = sa.getDeclaredAdvice(AdviceKind.BEFORE);
  225. assertEquals(2,advice.length);
  226. advice = sa.getDeclaredAdvice(AdviceKind.AFTER);
  227. assertEquals(2,advice.length);
  228. advice = sa.getDeclaredAdvice(AdviceKind.AFTER_RETURNING);
  229. assertEquals(2,advice.length);
  230. advice = sa.getDeclaredAdvice(AdviceKind.AFTER_THROWING);
  231. assertEquals(2,advice.length);
  232. advice = sa.getDeclaredAdvice(AdviceKind.AROUND);
  233. assertEquals(2,advice.length);
  234. advice = sa.getDeclaredAdvice(AdviceKind.BEFORE,AdviceKind.AFTER);
  235. assertEquals(4,advice.length);
  236. }
  237. public void testGetNamedAdvice() throws Exception {
  238. Advice a = sa.getAdvice("logItAll");
  239. assertEquals("logItAll",a.getName());
  240. assertEquals(AdviceKind.AROUND,a.getKind());
  241. a = sa.getAdvice("whatGoesAround");
  242. assertEquals("whatGoesAround",a.getName());
  243. assertEquals(AdviceKind.AROUND,a.getKind());
  244. try {
  245. a = sa.getAdvice("ajc$after$123");
  246. fail("Expecting NoSuchAdviceException");
  247. } catch (NoSuchAdviceException ex) {
  248. assertEquals("ajc$after$123",ex.getName());
  249. }
  250. try {
  251. a = sa.getAdvice("");
  252. fail("Expecting IllegalArgumentException");
  253. } catch (IllegalArgumentException ex) {
  254. ;
  255. }
  256. }
  257. public void testGetNamedDeclaredAdvice() throws Exception {
  258. Advice a = sa.getDeclaredAdvice("logItAll");
  259. assertEquals("logItAll",a.getName());
  260. assertEquals(AdviceKind.AROUND,a.getKind());
  261. a = sa.getDeclaredAdvice("whatGoesAround");
  262. assertEquals("whatGoesAround",a.getName());
  263. assertEquals(AdviceKind.AROUND,a.getKind());
  264. try {
  265. a = sa.getDeclaredAdvice("ajc$after$123");
  266. fail("Expecting NoSuchAdviceException");
  267. } catch (NoSuchAdviceException ex) {
  268. assertEquals("ajc$after$123",ex.getName());
  269. }
  270. try {
  271. a = sa.getDeclaredAdvice("");
  272. fail("Expecting IllegalArgumentException");
  273. } catch (IllegalArgumentException ex) {
  274. ;
  275. }
  276. }
  277. public void testIsPrivileged() {
  278. assertFalse(sa.isPrivileged());
  279. assertTrue(AjTypeSystem.getAjType(SimplePrivilegedAspect.class).isPrivileged());
  280. }
  281. public void testIsAspect() {
  282. assertTrue(sa.isAspect());
  283. }
  284. public void testIsMemberAspect() {
  285. assertFalse(AjTypeSystem.getAjType(SimplePrivilegedAspect.class).isMemberAspect());
  286. assertTrue(AjTypeSystem.getAjType(SimplePrivilegedAspect.MemberAspect.class).isMemberAspect());
  287. }
  288. public void testGetDeclareEoWarnings() {
  289. DeclareErrorOrWarning[] deows = sa.getDeclareErrorOrWarnings();
  290. assertEquals(4,deows.length);
  291. boolean foundCodeWarning = false;
  292. boolean foundCodeError = false;
  293. boolean foundAnnWarning = false;
  294. boolean foundAnnError = false;
  295. for (DeclareErrorOrWarning deow : deows) {
  296. if (deow.isError()) {
  297. if (deow.getMessage().equals("dont call this method code")) {
  298. foundCodeError = true;
  299. assertEquals("declare error : call(* DontDoIt.*(..)) : \"dont call this method code\"",deow.toString());
  300. }
  301. if (deow.getMessage().equals("dont call this method ann")) foundAnnError = true;
  302. assertEquals("call(* DontDoIt.*(..))",deow.getPointcutExpression().toString());
  303. } else {
  304. if (deow.getMessage().equals("dont call this method code")) foundCodeWarning = true;
  305. if (deow.getMessage().equals("dont call this method ann")) foundAnnWarning = true;
  306. assertEquals("call(* DontDoIt.*(..))",deow.getPointcutExpression().toString());
  307. }
  308. }
  309. assertTrue(foundCodeWarning && foundAnnWarning && foundCodeError && foundAnnError);
  310. }
  311. }
  312. @Aspect
  313. class SimpleAspect {
  314. // regular field
  315. public String s;
  316. // synthetic field
  317. public String ajc$xyz$s;
  318. // regular method
  319. public void aMethod() {}
  320. // advice method, annotation style
  321. @Before("execution(* SimpleAspect.*(..))")
  322. public void logEntry() {}
  323. // advice method, code style
  324. @Before("execution(* SimpleAspect.*(..))")
  325. public void ajc$before$123() {}
  326. // advice method, annotation style
  327. @After("execution(* SimpleAspect.*(..))")
  328. public void logFinally() {}
  329. // advice method, code style
  330. @After("execution(* SimpleAspect.*(..))")
  331. public void ajc$after$123() {}
  332. // advice method, annotation style
  333. @AfterReturning("execution(* SimpleAspect.*(..))")
  334. public void logExit() {}
  335. // advice method, code style
  336. @AfterReturning("execution(* SimpleAspect.*(..))")
  337. public void ajc$afterReturning$123() {}
  338. // advice method, annotation style
  339. @AfterThrowing("execution(* SimpleAspect.*(..))")
  340. public void logException() {}
  341. // advice method, code style
  342. @AfterThrowing("execution(* SimpleAspect.*(..))")
  343. public void ajc$afterThrowing$123() {}
  344. // advice method, annotation style
  345. @Around("execution(* SimpleAspect.*(..))")
  346. public void logItAll() {}
  347. // advice method, code style
  348. @Around("execution(* SimpleAspect.*(..))")
  349. @AdviceName("whatGoesAround")
  350. public void ajc$around$123() {}
  351. // pointcut, annotation style
  352. @org.aspectj.lang.annotation.Pointcut("execution(* SimpleAspect.*(..))")
  353. public void simpleAspectMethodExecution() {};
  354. // pointcut, code style
  355. @org.aspectj.lang.annotation.Pointcut("call(* SimpleAspect.*(..))")
  356. private void ajc$pointcut$$simpleAspectCall$123(SimpleAspect target) {};
  357. // decw, ann style
  358. @DeclareWarning("call(* DontDoIt.*(..))")
  359. public static final String dontDoIt = "dont call this method ann";
  360. // decw, code style
  361. @ajcDeclareEoW(pointcut="call(* DontDoIt.*(..))",message="dont call this method code",isError=false)
  362. private void ajc$declare_eow$123() {}
  363. // dec., ann style
  364. @DeclareError("call(* DontDoIt.*(..))")
  365. public static final String dontDoItISaid = "dont call this method ann";
  366. // decw, code style
  367. @ajcDeclareEoW(pointcut="call(* DontDoIt.*(..))",message="dont call this method code",isError=true)
  368. private void ajc$declare_eow$124() {}
  369. }
  370. @Aspect
  371. @ajcPrivileged
  372. class SimplePrivilegedAspect {
  373. @Aspect
  374. static class MemberAspect {}
  375. }
  376. @Aspect("perthis(pc())")
  377. class PerThisAspect {}
  378. @Aspect("pertarget(pc())")
  379. class PerTargetAspect {}
  380. @Aspect("percflow(pc())")
  381. class PerCflowAspect {}
  382. @Aspect("percflowbelow(pc())")
  383. class PerCflowbelowAspect {}
  384. @Aspect("pertypewithin(org.aspectj..*)")
  385. class PerTypeWithin {}