Search in sources :

Example 6 with Pointcut

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

the class ControlFlowPointcutTests method testSelectiveApplication.

/**
	 * Check that we can use a cflow pointcut only in conjunction with
	 * a static pointcut: e.g. all setter methods that are invoked under
	 * a particular class. This greatly reduces the number of calls
	 * to the cflow pointcut, meaning that it's not so prohibitively
	 * expensive.
	 */
@Test
public void testSelectiveApplication() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
    Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
    // Not advised, not under One
    target.setAge(16);
    assertEquals(0, nop.getCount());
    // Not advised; under One but not a setter
    assertEquals(16, new One().getAge(proxied));
    assertEquals(0, nop.getCount());
    // Won't be advised
    new One().set(proxied);
    assertEquals(1, nop.getCount());
    // We saved most evaluations
    assertEquals(1, cflow.getEvaluations());
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 7 with Pointcut

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

the class PointcutsTests method testSimpleIntersection.

/**
	 * The intersection of these two pointcuts leaves nothing.
	 */
@Test
public void testSimpleIntersection() {
    Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut);
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Integer(6)));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_ABSQUATULATE, TestBean.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 8 with Pointcut

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

the class PointcutsTests method testIntersectionOfSpecificGettersAndSubclassGetters.

/**
	 * Intersection should be MyTestBean getAge() only:
	 * it's the union of allClassGetAge and subclass getters
	 */
@Test
public void testIntersectionOfSpecificGettersAndSubclassGetters() {
    assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class));
    assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class));
    assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, TestBean.class));
    assertFalse(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, TestBean.class));
    assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_NAME, MyTestBean.class));
    assertTrue(Pointcuts.matches(myTestBeanGetterPointcut, TEST_BEAN_GET_AGE, MyTestBean.class));
    Pointcut intersection = Pointcuts.intersection(allClassGetAgePointcut, myTestBeanGetterPointcut);
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class));
    assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class));
    // Matches subclass of MyTestBean
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class));
    assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class));
    // Now intersection with MyTestBeanSubclass getters should eliminate MyTestBean target
    intersection = Pointcuts.intersection(intersection, myTestBeanSubclassGetterPointcut);
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, TestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBean.class));
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBean.class));
    // Still matches subclass of MyTestBean
    assertFalse(Pointcuts.matches(intersection, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class));
    assertTrue(Pointcuts.matches(intersection, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class));
    // Now union with all TestBean methods
    Pointcut union = Pointcuts.union(intersection, allTestBeanMethodsPointcut);
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_NAME, TestBean.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBean.class));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class));
    // Still matches subclass of MyTestBean
    assertFalse(Pointcuts.matches(union, TEST_BEAN_GET_NAME, MyTestBeanSubclass.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBeanSubclass.class));
    assertTrue(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class));
    assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, MyTestBean.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 9 with Pointcut

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

the class ComposablePointcutTests method testMatchAll.

@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));
}
Also used : Pointcut(org.springframework.aop.Pointcut) NestedRuntimeException(org.springframework.core.NestedRuntimeException) Test(org.junit.Test)

Example 10 with Pointcut

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

the class PointcutsTests method testUnionOfSettersAndGetters.

/**
	 * Should match all setters and getters on any class
	 */
@Test
public void testUnionOfSettersAndGetters() {
    Pointcut union = Pointcuts.union(allClassGetterPointcut, 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(union, TEST_BEAN_ABSQUATULATE, TestBean.class));
}
Also used : Pointcut(org.springframework.aop.Pointcut) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

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