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.

StandardPointcutExpressionImpl.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver.internal.tools;
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.Member;
  13. import org.aspectj.weaver.ResolvedMember;
  14. import org.aspectj.weaver.ResolvedType;
  15. import org.aspectj.weaver.Shadow;
  16. import org.aspectj.weaver.World;
  17. import org.aspectj.weaver.ast.Literal;
  18. import org.aspectj.weaver.ast.Test;
  19. import org.aspectj.weaver.patterns.AbstractPatternNodeVisitor;
  20. import org.aspectj.weaver.patterns.AnnotationPointcut;
  21. import org.aspectj.weaver.patterns.ArgsAnnotationPointcut;
  22. import org.aspectj.weaver.patterns.ArgsPointcut;
  23. import org.aspectj.weaver.patterns.CflowPointcut;
  24. import org.aspectj.weaver.patterns.ExposedState;
  25. import org.aspectj.weaver.patterns.IfPointcut;
  26. import org.aspectj.weaver.patterns.NotAnnotationTypePattern;
  27. import org.aspectj.weaver.patterns.NotPointcut;
  28. import org.aspectj.weaver.patterns.Pointcut;
  29. import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
  30. import org.aspectj.weaver.patterns.ThisOrTargetPointcut;
  31. import org.aspectj.weaver.patterns.WithinAnnotationPointcut;
  32. import org.aspectj.weaver.patterns.WithinCodeAnnotationPointcut;
  33. import org.aspectj.weaver.reflect.ReflectionFastMatchInfo;
  34. import org.aspectj.weaver.reflect.StandardShadow;
  35. import org.aspectj.weaver.reflect.StandardShadowMatchImpl;
  36. import org.aspectj.weaver.tools.DefaultMatchingContext;
  37. import org.aspectj.weaver.tools.MatchingContext;
  38. import org.aspectj.weaver.tools.PointcutParameter;
  39. import org.aspectj.weaver.tools.ShadowMatch;
  40. import org.aspectj.weaver.tools.StandardPointcutExpression;
  41. /**
  42. * Map from weaver.tools interface to internal Pointcut implementation...
  43. */
  44. public class StandardPointcutExpressionImpl implements StandardPointcutExpression {
  45. private World world;
  46. private Pointcut pointcut;
  47. private String expression;
  48. private PointcutParameter[] parameters;
  49. private MatchingContext matchContext = new DefaultMatchingContext();
  50. public StandardPointcutExpressionImpl(Pointcut pointcut, String expression, PointcutParameter[] params, World inWorld) {
  51. this.pointcut = pointcut;
  52. this.expression = expression;
  53. this.world = inWorld;
  54. this.parameters = params;
  55. if (this.parameters == null) {
  56. this.parameters = new PointcutParameter[0];
  57. }
  58. }
  59. public Pointcut getUnderlyingPointcut() {
  60. return this.pointcut;
  61. }
  62. /*
  63. * (non-Javadoc)
  64. *
  65. * @see org.aspectj.weaver.tools.PointcutExpression#setMatchingContext(org.aspectj.weaver.tools.MatchingContext)
  66. */
  67. public void setMatchingContext(MatchingContext aMatchContext) {
  68. this.matchContext = aMatchContext;
  69. }
  70. public boolean couldMatchJoinPointsInType(Class aClass) {
  71. ResolvedType matchType = world.resolve(aClass.getName());
  72. ReflectionFastMatchInfo info = new ReflectionFastMatchInfo(matchType, null, this.matchContext, world);
  73. return pointcut.fastMatch(info).maybeTrue();
  74. }
  75. public boolean mayNeedDynamicTest() {
  76. HasPossibleDynamicContentVisitor visitor = new HasPossibleDynamicContentVisitor();
  77. pointcut.traverse(visitor, null);
  78. return visitor.hasDynamicContent();
  79. }
  80. private ExposedState getExposedState() {
  81. return new ExposedState(parameters.length);
  82. }
  83. // public ShadowMatch matchesMethodExecution(Method aMethod) {
  84. // return matchesExecution(aMethod);
  85. // }
  86. public ShadowMatch matchesMethodExecution(ResolvedMember aMethod) {
  87. return matchesExecution(aMethod);
  88. }
  89. public ShadowMatch matchesConstructorExecution(Constructor aConstructor) {
  90. return null;
  91. // return matchesExecution(aConstructor);
  92. }
  93. // private ShadowMatch matchesExecution(Member aMember) {
  94. // Shadow s = ReflectionShadow.makeExecutionShadow(world, aMember, this.matchContext);
  95. // ShadowMatchImpl sm = getShadowMatch(s);
  96. // sm.setSubject(aMember);
  97. // sm.setWithinCode(null);
  98. // sm.setWithinType(aMember.getDeclaringClass());
  99. // return sm;
  100. // }
  101. private ShadowMatch matchesExecution(ResolvedMember aMember) {
  102. Shadow s = StandardShadow.makeExecutionShadow(world, aMember, this.matchContext);
  103. StandardShadowMatchImpl sm = getShadowMatch(s);
  104. sm.setSubject(aMember);
  105. sm.setWithinCode(null);
  106. sm.setWithinType((ResolvedType) aMember.getDeclaringType());
  107. return sm;
  108. }
  109. // public ShadowMatch matchesStaticInitialization(Class aClass) {
  110. // Shadow s = ReflectionShadow.makeStaticInitializationShadow(world, aClass, this.matchContext);
  111. // StandardShadowMatchImpl sm = getShadowMatch(s);
  112. // sm.setSubject(null);
  113. // sm.setWithinCode(null);
  114. // sm.setWithinType(aClass);
  115. // return sm;
  116. // }
  117. public ShadowMatch matchesStaticInitialization(ResolvedType aType) {
  118. Shadow s = StandardShadow.makeStaticInitializationShadow(world, aType, this.matchContext);
  119. StandardShadowMatchImpl sm = getShadowMatch(s);
  120. sm.setSubject(null);
  121. sm.setWithinCode(null);
  122. sm.setWithinType(aType);
  123. return sm;
  124. }
  125. // public ShadowMatch matchesAdviceExecution(Method aMethod) {
  126. // Shadow s = ReflectionShadow.makeAdviceExecutionShadow(world, aMethod, this.matchContext);
  127. // StandardShadowMatchImpl sm = getShadowMatch(s);
  128. // sm.setSubject(aMethod);
  129. // sm.setWithinCode(null);
  130. // sm.setWithinType(aMethod.getDeclaringClass());
  131. // return sm;
  132. // }
  133. //
  134. // public ShadowMatch matchesInitialization(Constructor aConstructor) {
  135. // Shadow s = ReflectionShadow.makeInitializationShadow(world, aConstructor, this.matchContext);
  136. // StandardShadowMatchImpl sm = getShadowMatch(s);
  137. // sm.setSubject(aConstructor);
  138. // sm.setWithinCode(null);
  139. // sm.setWithinType(aConstructor.getDeclaringClass());
  140. // return sm;
  141. // }
  142. //
  143. // public ShadowMatch matchesPreInitialization(Constructor aConstructor) {
  144. // Shadow s = ReflectionShadow.makePreInitializationShadow(world, aConstructor, this.matchContext);
  145. // StandardShadowMatchImpl sm = getShadowMatch(s);
  146. // sm.setSubject(aConstructor);
  147. // sm.setWithinCode(null);
  148. // sm.setWithinType(aConstructor.getDeclaringClass());
  149. // return sm;
  150. // }
  151. //
  152. public ShadowMatch matchesMethodCall(ResolvedMember aMethod, ResolvedMember withinCode) {
  153. Shadow s = StandardShadow.makeCallShadow(world, aMethod, withinCode, this.matchContext);
  154. StandardShadowMatchImpl sm = getShadowMatch(s);
  155. sm.setSubject(aMethod);
  156. sm.setWithinCode(withinCode);
  157. sm.setWithinType((ResolvedType) withinCode.getDeclaringType());
  158. return sm;
  159. }
  160. //
  161. // public ShadowMatch matchesMethodCall(Method aMethod, Class callerType) {
  162. // Shadow s = ReflectionShadow.makeCallShadow(world, aMethod, callerType, this.matchContext);
  163. // ShadowMatchImpl sm = getShadowMatch(s);
  164. // sm.setSubject(aMethod);
  165. // sm.setWithinCode(null);
  166. // sm.setWithinType(callerType);
  167. // return sm;
  168. // }
  169. //
  170. // public ShadowMatch matchesConstructorCall(Constructor aConstructor, Class callerType) {
  171. // Shadow s = ReflectionShadow.makeCallShadow(world, aConstructor, callerType, this.matchContext);
  172. // ShadowMatchImpl sm = getShadowMatch(s);
  173. // sm.setSubject(aConstructor);
  174. // sm.setWithinCode(null);
  175. // sm.setWithinType(callerType);
  176. // return sm;
  177. // }
  178. //
  179. // public ShadowMatch matchesConstructorCall(Constructor aConstructor, Member withinCode) {
  180. // Shadow s = ReflectionShadow.makeCallShadow(world, aConstructor, withinCode, this.matchContext);
  181. // ShadowMatchImpl sm = getShadowMatch(s);
  182. // sm.setSubject(aConstructor);
  183. // sm.setWithinCode(withinCode);
  184. // sm.setWithinType(withinCode.getDeclaringClass());
  185. // return sm;
  186. // }
  187. //
  188. // public ShadowMatch matchesHandler(Class exceptionType, Class handlingType) {
  189. // Shadow s = ReflectionShadow.makeHandlerShadow(world, exceptionType, handlingType, this.matchContext);
  190. // ShadowMatchImpl sm = getShadowMatch(s);
  191. // sm.setSubject(null);
  192. // sm.setWithinCode(null);
  193. // sm.setWithinType(handlingType);
  194. // return sm;
  195. // }
  196. //
  197. // public ShadowMatch matchesHandler(Class exceptionType, Member withinCode) {
  198. // Shadow s = ReflectionShadow.makeHandlerShadow(world, exceptionType, withinCode, this.matchContext);
  199. // ShadowMatchImpl sm = getShadowMatch(s);
  200. // sm.setSubject(null);
  201. // sm.setWithinCode(withinCode);
  202. // sm.setWithinType(withinCode.getDeclaringClass());
  203. // return sm;
  204. // }
  205. //
  206. // public ShadowMatch matchesFieldGet(Field aField, Class withinType) {
  207. // Shadow s = ReflectionShadow.makeFieldGetShadow(world, aField, withinType, this.matchContext);
  208. // ShadowMatchImpl sm = getShadowMatch(s);
  209. // sm.setSubject(aField);
  210. // sm.setWithinCode(null);
  211. // sm.setWithinType(withinType);
  212. // return sm;
  213. // }
  214. //
  215. // public ShadowMatch matchesFieldGet(Field aField, Member withinCode) {
  216. // Shadow s = ReflectionShadow.makeFieldGetShadow(world, aField, withinCode, this.matchContext);
  217. // ShadowMatchImpl sm = getShadowMatch(s);
  218. // sm.setSubject(aField);
  219. // sm.setWithinCode(withinCode);
  220. // sm.setWithinType(withinCode.getDeclaringClass());
  221. // return sm;
  222. // }
  223. //
  224. // public ShadowMatch matchesFieldSet(Field aField, Class withinType) {
  225. // Shadow s = ReflectionShadow.makeFieldSetShadow(world, aField, withinType, this.matchContext);
  226. // ShadowMatchImpl sm = getShadowMatch(s);
  227. // sm.setSubject(aField);
  228. // sm.setWithinCode(null);
  229. // sm.setWithinType(withinType);
  230. // return sm;
  231. // }
  232. //
  233. // public ShadowMatch matchesFieldSet(Field aField, Member withinCode) {
  234. // Shadow s = ReflectionShadow.makeFieldSetShadow(world, aField, withinCode, this.matchContext);
  235. // StandardShadowMatchImpl sm = getShadowMatch(s);
  236. // sm.setSubject(aField);
  237. // sm.setWithinCode(withinCode);
  238. // sm.setWithinType(withinCode.getDeclaringClass());
  239. // return sm;
  240. // }
  241. private StandardShadowMatchImpl getShadowMatch(Shadow forShadow) {
  242. org.aspectj.util.FuzzyBoolean match = pointcut.match(forShadow);
  243. Test residueTest = Literal.TRUE;
  244. ExposedState state = getExposedState();
  245. if (match.maybeTrue()) {
  246. residueTest = pointcut.findResidue(forShadow, state);
  247. }
  248. StandardShadowMatchImpl sm = new StandardShadowMatchImpl(match, residueTest, state, parameters);
  249. sm.setMatchingContext(this.matchContext);
  250. return sm;
  251. }
  252. /*
  253. * (non-Javadoc)
  254. *
  255. * @see org.aspectj.weaver.tools.PointcutExpression#getPointcutExpression()
  256. */
  257. public String getPointcutExpression() {
  258. return expression;
  259. }
  260. private static class HasPossibleDynamicContentVisitor extends AbstractPatternNodeVisitor {
  261. private boolean hasDynamicContent = false;
  262. public boolean hasDynamicContent() {
  263. return hasDynamicContent;
  264. }
  265. @Override
  266. public Object visit(WithinAnnotationPointcut node, Object data) {
  267. hasDynamicContent = true;
  268. return null;
  269. }
  270. @Override
  271. public Object visit(WithinCodeAnnotationPointcut node, Object data) {
  272. hasDynamicContent = true;
  273. return null;
  274. }
  275. @Override
  276. public Object visit(AnnotationPointcut node, Object data) {
  277. hasDynamicContent = true;
  278. return null;
  279. }
  280. @Override
  281. public Object visit(ArgsAnnotationPointcut node, Object data) {
  282. hasDynamicContent = true;
  283. return null;
  284. }
  285. @Override
  286. public Object visit(ArgsPointcut node, Object data) {
  287. hasDynamicContent = true;
  288. return null;
  289. }
  290. @Override
  291. public Object visit(CflowPointcut node, Object data) {
  292. hasDynamicContent = true;
  293. return null;
  294. }
  295. @Override
  296. public Object visit(IfPointcut node, Object data) {
  297. hasDynamicContent = true;
  298. return null;
  299. }
  300. @Override
  301. public Object visit(NotAnnotationTypePattern node, Object data) {
  302. return node.getNegatedPattern().accept(this, data);
  303. }
  304. @Override
  305. public Object visit(NotPointcut node, Object data) {
  306. return node.getNegatedPointcut().accept(this, data);
  307. }
  308. @Override
  309. public Object visit(ThisOrTargetAnnotationPointcut node, Object data) {
  310. hasDynamicContent = true;
  311. return null;
  312. }
  313. @Override
  314. public Object visit(ThisOrTargetPointcut node, Object data) {
  315. hasDynamicContent = true;
  316. return null;
  317. }
  318. }
  319. public static class Handler implements Member {
  320. private Class<?> decClass;
  321. private Class<?> exType;
  322. public Handler(Class decClass, Class exType) {
  323. this.decClass = decClass;
  324. this.exType = exType;
  325. }
  326. public int getModifiers() {
  327. return 0;
  328. }
  329. public Class getDeclaringClass() {
  330. return decClass;
  331. }
  332. public String getName() {
  333. return null;
  334. }
  335. public Class getHandledExceptionType() {
  336. return exType;
  337. }
  338. public boolean isSynthetic() {
  339. return false;
  340. }
  341. }
  342. }