Search in sources :

Example 16 with Pointcut

use of org.springframework.aop.Pointcut in project spring-framework by spring-projects.

the class CallCountingInterceptor method testMatchWithTypePattern.

@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));
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ClassFilter(org.springframework.aop.ClassFilter) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Example 17 with Pointcut

use of org.springframework.aop.Pointcut in project spring-framework by spring-projects.

the class CallCountingInterceptor method testMatchExplicit.

@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));
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ClassFilter(org.springframework.aop.ClassFilter) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Example 18 with Pointcut

use of org.springframework.aop.Pointcut in project spring-framework by spring-projects.

the class CallCountingInterceptor method testMatchWithArgs.

@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());
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ClassFilter(org.springframework.aop.ClassFilter) MethodMatcher(org.springframework.aop.MethodMatcher) Test(org.junit.Test)

Example 19 with Pointcut

use of org.springframework.aop.Pointcut in project spring-framework by spring-projects.

the class AsyncAnnotationAdvisor method buildPointcut.

/**
	 * Calculate a pointcut for the given async annotation types, if any.
	 * @param asyncAnnotationTypes the async annotation types to introspect
	 * @return the applicable Pointcut object, or {@code null} if none
	 */
protected Pointcut buildPointcut(Set<Class<? extends Annotation>> asyncAnnotationTypes) {
    ComposablePointcut result = null;
    for (Class<? extends Annotation> asyncAnnotationType : asyncAnnotationTypes) {
        Pointcut cpc = new AnnotationMatchingPointcut(asyncAnnotationType, true);
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(asyncAnnotationType);
        if (result == null) {
            result = new ComposablePointcut(cpc);
        } else {
            result.union(cpc);
        }
        result = result.union(mpc);
    }
    return result;
}
Also used : ComposablePointcut(org.springframework.aop.support.ComposablePointcut) Pointcut(org.springframework.aop.Pointcut) AnnotationMatchingPointcut(org.springframework.aop.support.annotation.AnnotationMatchingPointcut) AnnotationMatchingPointcut(org.springframework.aop.support.annotation.AnnotationMatchingPointcut) ComposablePointcut(org.springframework.aop.support.ComposablePointcut)

Example 20 with Pointcut

use of org.springframework.aop.Pointcut in project spring-framework by spring-projects.

the class UnsupportedInterceptor method getAdvisedProxy.

private ITestBean getAdvisedProxy(TestBean target) {
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { ITestBean.class });
    pf.setProxyTargetClass(true);
    MethodInterceptor advice = new NopInterceptor();
    Pointcut pointcut = new Pointcut() {

        @Override
        public ClassFilter getClassFilter() {
            return ClassFilter.TRUE;
        }

        @Override
        public MethodMatcher getMethodMatcher() {
            return MethodMatcher.TRUE;
        }

        @Override
        public boolean equals(Object obj) {
            return true;
        }

        @Override
        public int hashCode() {
            return 0;
        }
    };
    pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));
    pf.setTarget(target);
    pf.setFrozen(true);
    pf.setExposeProxy(false);
    return (ITestBean) pf.getProxy();
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor)

Aggregations

Pointcut (org.springframework.aop.Pointcut)20 Test (org.junit.Test)14 TestBean (org.springframework.tests.sample.beans.TestBean)10 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 MethodMatcher (org.springframework.aop.MethodMatcher)4 ClassFilter (org.springframework.aop.ClassFilter)3 ComposablePointcut (org.springframework.aop.support.ComposablePointcut)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 AnnotationMatchingPointcut (org.springframework.aop.support.annotation.AnnotationMatchingPointcut)3 Method (java.lang.reflect.Method)2 PointcutExpression (org.aspectj.weaver.tools.PointcutExpression)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 StaticMethodMatcher (org.springframework.aop.support.StaticMethodMatcher)1 NestedRuntimeException (org.springframework.core.NestedRuntimeException)1