@Override public ClassFilter getClassFilter() { return pointcut.getClassFilter(); }
/** * Create a ComposablePointcut based on the given Pointcut. * @param pointcut the original Pointcut */ public ComposablePointcut(Pointcut pointcut) { Assert.notNull(pointcut, "Pointcut must not be null"); this.classFilter = pointcut.getClassFilter(); this.methodMatcher = pointcut.getMethodMatcher(); }
/** * Apply a union with the given Pointcut. * <p>Note that for a Pointcut union, methods will only match if their * original ClassFilter (from the originating Pointcut) matches as well. * MethodMatchers and ClassFilters from different Pointcuts will never * get interleaved with each other. * @param other the Pointcut to apply a union with * @return this composable pointcut (for call chaining) */ public ComposablePointcut union(Pointcut other) { this.methodMatcher = MethodMatchers.union( this.methodMatcher, this.classFilter, other.getMethodMatcher(), other.getClassFilter()); this.classFilter = ClassFilters.union(this.classFilter, other.getClassFilter()); return this; }
/** * Apply an intersection with the given Pointcut. * @param other the Pointcut to apply an intersection with * @return this composable pointcut (for call chaining) */ public ComposablePointcut intersection(Pointcut other) { this.classFilter = ClassFilters.intersection(this.classFilter, other.getClassFilter()); this.methodMatcher = MethodMatchers.intersection(this.methodMatcher, other.getMethodMatcher()); return this; }
if (!pc.getClassFilter().matches(targetClass)) { return false;
/** * Perform the least expensive check for a pointcut match. * @param pointcut the pointcut to match * @param method the candidate method * @param targetClass the target class * @param args arguments to the method * @return whether there's a runtime match */ public static boolean matches(Pointcut pointcut, Method method, Class<?> targetClass, Object... args) { Assert.notNull(pointcut, "Pointcut must not be null"); if (pointcut == Pointcut.TRUE) { return true; } if (pointcut.getClassFilter().matches(targetClass)) { // Only check if it gets past first hurdle. MethodMatcher mm = pointcut.getMethodMatcher(); if (mm.matches(method, targetClass)) { // We may need additional runtime (argument) check. return (!mm.isRuntime() || mm.matches(method, targetClass, args)); } } return false; }
/** * Build a 'safe' pointcut that excludes the AspectJ advice method itself. * @return a composable pointcut that builds on the original AspectJ expression pointcut * @see #getPointcut() */ public final Pointcut buildSafePointcut() { Pointcut pc = getPointcut(); MethodMatcher safeMethodMatcher = MethodMatchers.intersection( new AdviceExcludingMethodMatcher(this.aspectJAdviceMethod), pc.getMethodMatcher()); return new ComposablePointcut(pc.getClassFilter(), safeMethodMatcher); }
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) { MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); boolean match;
@Test public void testInvalidExpression() { String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number) && args(Double)"; try { getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution fail("Invalid expression should throw IllegalArgumentException"); } catch (IllegalArgumentException ex) { assertTrue(true); } }
@Test public void testWithUnsupportedPointcutPrimitive() { String expression = "call(int org.springframework.tests.sample.beans.TestBean.getAge())"; try { getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution... fail("Should not support call pointcuts"); } catch (UnsupportedPointcutPrimitiveException ex) { assertEquals("Should not support call pointcut", PointcutPrimitive.CALL, ex.getUnsupportedPrimitive()); } }
@Test public void testMatchAll() throws NoSuchMethodException { Pointcut pc = new ComposablePointcut(); assertTrue(pc.getClassFilter().matches(Object.class)); assertTrue(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode"), Exception.class)); }
@Test public void testMatchWithArgs() throws Exception { String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number)) && args(Double)"; Pointcut pointcut = getPointcut(expression); ClassFilter classFilter = pointcut.getClassFilter(); MethodMatcher methodMatcher = pointcut.getMethodMatcher(); assertMatchesTestBeanClass(classFilter); // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); assertTrue("Should match with setSomeNumber with Double input", methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12))); assertFalse("Should not match setSomeNumber with Integer input", methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11))); assertFalse("Should not match getAge", methodMatcher.matches(getAge, TestBean.class)); assertTrue("Should be a runtime match", methodMatcher.isRuntime()); }
@Test public void testMatchExplicit() { String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())"; Pointcut pointcut = getPointcut(expression); ClassFilter classFilter = pointcut.getClassFilter(); MethodMatcher methodMatcher = pointcut.getMethodMatcher(); assertMatchesTestBeanClass(classFilter); // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); assertFalse("Should not be a runtime match", methodMatcher.isRuntime()); assertMatchesGetAge(methodMatcher); assertFalse("Expression should match setAge() method", methodMatcher.matches(setAge, TestBean.class)); }
@Test public void testMatchWithTypePattern() throws Exception { String expression = "execution(* *..TestBean.*Age(..))"; Pointcut pointcut = getPointcut(expression); ClassFilter classFilter = pointcut.getClassFilter(); MethodMatcher methodMatcher = pointcut.getMethodMatcher(); assertMatchesTestBeanClass(classFilter); // not currently testable in a reliable fashion //assertDoesNotMatchStringClass(classFilter); assertFalse("Should not be a runtime match", methodMatcher.isRuntime()); assertMatchesGetAge(methodMatcher); assertTrue("Expression should match setAge(int) method", methodMatcher.matches(setAge, TestBean.class)); }
@Test public void testPerTarget() throws SecurityException, NoSuchMethodException { AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS); InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl( ajexp, TestBean.class.getMethod("getAge"), af, new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean"), 1, "someBean"); assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut); assertTrue(ajpa.isPerInstance()); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class)); assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getAge"), TestBean.class)); assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches( TestBean.class.getMethod("getSpouse"), TestBean.class)); }
/** * Create a ComposablePointcut based on the given Pointcut. * @param pointcut the original Pointcut */ public ComposablePointcut(Pointcut pointcut) { Assert.notNull(pointcut, "Pointcut must not be null"); this.classFilter = pointcut.getClassFilter(); this.methodMatcher = pointcut.getMethodMatcher(); }
/** * Apply an intersection with the given Pointcut. * @param other the Pointcut to apply an intersection with * @return this composable pointcut (for call chaining) */ public ComposablePointcut intersection(Pointcut other) { this.classFilter = ClassFilters.intersection(this.classFilter, other.getClassFilter()); this.methodMatcher = MethodMatchers.intersection(this.methodMatcher, other.getMethodMatcher()); return this; }
/** * Match all methods that <b>both</b> the given pointcuts match * @param a input pointcut * @param b input pointput * @return a distinct Pointcut that matches all methods that both the * given pointcuts match */ public static Pointcut intersection(Pointcut a, Pointcut b) { return new ComposablePointcut(a.getClassFilter(), a.getMethodMatcher()).intersection(b); }
public ComposablePointcut intersection(Pointcut other) { this.classFilter = ClassFilters.intersection(this.classFilter, other.getClassFilter()); this.methodMatcher = MethodMatchers.intersection(this.methodMatcher, other.getMethodMatcher()); return this; }
/** * Build a 'safe' pointcut that excludes the AspectJ advice method itself. * @return a composable pointcut that builds on the original AspectJ expression pointcut * @see #getPointcut() */ public final Pointcut buildSafePointcut() { Pointcut pc = getPointcut(); MethodMatcher safeMethodMatcher = MethodMatchers.intersection( new AdviceExcludingMethodMatcher(this.aspectJAdviceMethod), pc.getMethodMatcher()); return new ComposablePointcut(pc.getClassFilter(), safeMethodMatcher); }