|
| 1 | +package com.ysj.tinySpring.aop; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import org.aspectj.weaver.tools.PointcutExpression; |
| 8 | +import org.aspectj.weaver.tools.PointcutParser; |
| 9 | +import org.aspectj.weaver.tools.PointcutPrimitive; |
| 10 | +import org.aspectj.weaver.tools.ShadowMatch; |
| 11 | + |
| 12 | +/** |
| 13 | + * 通过AspectJ表达式识别切点 |
| 14 | + * |
| 15 | + */ |
| 16 | +public class AspectJExpressionPointcut implements Pointcut, ClassFilter, MethodMatcher { |
| 17 | + |
| 18 | + private PointcutParser pointcutParser; |
| 19 | + |
| 20 | + private String expression; |
| 21 | + |
| 22 | + private PointcutExpression pointcutExpression; |
| 23 | + |
| 24 | + private static final Set<PointcutPrimitive> DEFAULT_SUPPORTED_PRIMITIVES = new HashSet<PointcutPrimitive>(); |
| 25 | + |
| 26 | + static { |
| 27 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.EXECUTION); |
| 28 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.ARGS); |
| 29 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.REFERENCE); |
| 30 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.THIS); |
| 31 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.TARGET); |
| 32 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.WITHIN); |
| 33 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ANNOTATION); |
| 34 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_WITHIN); |
| 35 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ARGS); |
| 36 | + DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_TARGET); |
| 37 | + } |
| 38 | + |
| 39 | + public AspectJExpressionPointcut() { |
| 40 | + this(DEFAULT_SUPPORTED_PRIMITIVES); |
| 41 | + } |
| 42 | + |
| 43 | + public AspectJExpressionPointcut(Set<PointcutPrimitive> supportedPrimitives) { |
| 44 | + pointcutParser = PointcutParser |
| 45 | + .getPointcutParserSupportingSpecifiedPrimitivesAndUsingContextClassloaderForResolution(supportedPrimitives); |
| 46 | + } |
| 47 | + |
| 48 | + protected void checkReadyToMatch() { |
| 49 | + if (pointcutExpression == null) { |
| 50 | + pointcutExpression = buildPointcutExpression(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private PointcutExpression buildPointcutExpression() { |
| 55 | + return pointcutParser.parsePointcutExpression(expression); |
| 56 | + } |
| 57 | + |
| 58 | + public void setExpression(String expression) { |
| 59 | + this.expression = expression; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public ClassFilter getClassFilter() { |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public MethodMatcher getMethodMatcher() { |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean matches(Class targetClass) { |
| 74 | + checkReadyToMatch(); |
| 75 | + return pointcutExpression.couldMatchJoinPointsInType(targetClass); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean matches(Method method, Class targetClass) { |
| 80 | + checkReadyToMatch(); |
| 81 | + ShadowMatch shadowMatch = pointcutExpression.matchesMethodExecution(method); |
| 82 | + if (shadowMatch.alwaysMatches()) { |
| 83 | + return true; |
| 84 | + } else if (shadowMatch.neverMatches()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + // TODO:其他情况不判断了!见org.springframework.aop.aspectj.RuntimeTestWalker |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments