Search in sources :

Example 6 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-infrastructure by TAKETODAY.

the class ComposablePointcutTests method testMatchAll.

@Test
public void testMatchAll() throws NoSuchMethodException {
    Pointcut pc = new ComposablePointcut();
    assertThat(pc.getClassFilter().matches(Object.class)).isTrue();
    assertThat(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode"), Exception.class)).isTrue();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) Test(org.junit.jupiter.api.Test)

Example 7 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-infrastructure by TAKETODAY.

the class PointcutsTests method testIntersectionOfSpecificGettersAndSubclassGetters.

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

Example 8 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-infrastructure by TAKETODAY.

the class PointcutsTests method testUnionOfSettersAndGetters.

/**
 * Should match all setters and getters on any class
 */
@Test
public void testUnionOfSettersAndGetters() {
    Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut);
    assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isTrue();
    assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue();
    assertThat(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class)).isFalse();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) Test(org.junit.jupiter.api.Test)

Example 9 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-infrastructure by TAKETODAY.

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() {
    assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse();
    assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, 6)).isTrue();
    assertThat(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class)).isFalse();
    Pointcut union = Pointcuts.union(myTestBeanSetterPointcut, allClassGetterPointcut);
    assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, TestBean.class)).isTrue();
    assertThat(Pointcuts.matches(union, TEST_BEAN_GET_AGE, MyTestBean.class)).isTrue();
    // Still doesn't match superclass setter
    assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, MyTestBean.class, 6)).isTrue();
    assertThat(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, 6)).isFalse();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) Test(org.junit.jupiter.api.Test)

Example 10 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.Pointcut) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor)

Aggregations

Pointcut (cn.taketoday.aop.Pointcut)51 Test (org.junit.jupiter.api.Test)42 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)13 AnnotationMatchingPointcut (cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)10 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)7 ClassFilter (cn.taketoday.aop.ClassFilter)6 MethodMatcher (cn.taketoday.aop.MethodMatcher)6 Qualifier (cn.taketoday.beans.factory.annotation.Qualifier)6 Qualifier (jakarta.inject.Qualifier)6 Method (java.lang.reflect.Method)6 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)5 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)4 Nullable (cn.taketoday.lang.Nullable)4 PointcutExpression (org.aspectj.weaver.tools.PointcutExpression)4 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)3 DefaultMethodInvocation (cn.taketoday.aop.framework.DefaultMethodInvocation)2 ComposablePointcut (cn.taketoday.aop.support.ComposablePointcut)2 StaticMethodMatcherPointcut (cn.taketoday.aop.support.StaticMethodMatcherPointcut)2 NopInterceptor (cn.taketoday.aop.NopInterceptor)1