Search in sources :

Example 11 with Pointcut

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

the class PointcutsTests method testUnionOfSpecificGetters.

@Test
public void testUnionOfSpecificGetters() {
    Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut);
    assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class));
    // Union with all setters
    union = Pointcuts.union(union, allClassSetterPointcut);
    assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_NAME, TestBean.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
}
Also used : Pointcut(org.springframework.aop.Pointcut) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 12 with Pointcut

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

the class PointcutsTests method testUnionOfAllSettersAndSubclassSetters.

/**
	 * Tests vertical composition. First pointcut matches all setters.
	 * Second one matches all getters in the MyTestBean class. TestBean getters shouldn't pass.
	 */
@Test
public void testUnionOfAllSettersAndSubclassSetters() {
    assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
    assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6)));
    assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class));
    Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut);
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class));
    // Still doesn't match superclass setter
    assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, new Integer(6)));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
}
Also used : Pointcut(org.springframework.aop.Pointcut) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 13 with Pointcut

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

the class AopUtilsTests method testPointcutAppliesToOneMethodOnObject.

@Test
public void testPointcutAppliesToOneMethodOnObject() {
    class TestPointcut extends StaticMethodMatcherPointcut {

        @Override
        public boolean matches(Method method, Class<?> clazz) {
            return method.getName().equals("hashCode");
        }
    }
    Pointcut pc = new TestPointcut();
    // will return true if we're not proxying interfaces
    assertTrue(AopUtils.canApply(pc, Object.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 14 with Pointcut

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

the class AbstractAspectJAdvice method buildSafePointcut.

/**
	 * 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);
}
Also used : ComposablePointcut(org.springframework.aop.support.ComposablePointcut) Pointcut(org.springframework.aop.Pointcut) MethodMatcher(org.springframework.aop.MethodMatcher) StaticMethodMatcher(org.springframework.aop.support.StaticMethodMatcher) ComposablePointcut(org.springframework.aop.support.ComposablePointcut)

Example 15 with Pointcut

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

the class CallCountingInterceptor method getAdvisedProxy.

private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
    TestBean target = new TestBean();
    Pointcut pointcut = getPointcut(pointcutExpression);
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    advisor.setAdvice(interceptor);
    advisor.setPointcut(pointcut);
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    return (TestBean) pf.getProxy();
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) 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