Search in sources :

Example 21 with Pointcut

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

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);
    assertThat(nop.getCount()).isEqualTo(0);
    // Not advised; under One but not a setter
    assertThat(new One().getAge(proxied)).isEqualTo(16);
    assertThat(nop.getCount()).isEqualTo(0);
    // Won't be advised
    new One().set(proxied);
    assertThat(nop.getCount()).isEqualTo(1);
    // We saved most evaluations
    assertThat(cflow.getEvaluations()).isEqualTo(1);
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 22 with Pointcut

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

the class AnnotationMatchingPointcutTests method classLevelPointcuts.

@Test
public void classLevelPointcuts() {
    Pointcut pointcut1 = new AnnotationMatchingPointcut(Qualifier.class, true);
    Pointcut pointcut2 = new AnnotationMatchingPointcut(Qualifier.class, true);
    Pointcut pointcut3 = new AnnotationMatchingPointcut(Qualifier.class);
    assertThat(pointcut1.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class);
    assertThat(pointcut2.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class);
    assertThat(pointcut3.getClassFilter().getClass()).isEqualTo(AnnotationClassFilter.class);
    assertThat(pointcut1.getClassFilter().toString()).contains(Qualifier.class.getName());
    assertThat(pointcut1.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE);
    assertThat(pointcut2.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE);
    assertThat(pointcut3.getMethodMatcher()).isEqualTo(MethodMatcher.TRUE);
    assertThat(pointcut1).isEqualTo(pointcut2);
    assertThat(pointcut1).isNotEqualTo(pointcut3);
    assertThat(pointcut1.hashCode()).isEqualTo(pointcut2.hashCode());
    // #1 and #3 have equivalent hash codes even though equals() returns false.
    assertThat(pointcut1.hashCode()).isEqualTo(pointcut3.hashCode());
    assertThat(pointcut1.toString()).isEqualTo(pointcut2.toString());
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) Qualifier(jakarta.inject.Qualifier) Test(org.junit.jupiter.api.Test)

Example 23 with Pointcut

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

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 = new AnnotationMatchingPointcut(null, asyncAnnotationType, true);
        if (result == null) {
            result = new ComposablePointcut(cpc);
        } else {
            result.union(cpc);
        }
        result = result.union(mpc);
    }
    return (result != null ? result : Pointcut.TRUE);
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) ComposablePointcut(cn.taketoday.aop.support.ComposablePointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) ComposablePointcut(cn.taketoday.aop.support.ComposablePointcut)

Example 24 with Pointcut

use of cn.taketoday.aop.Pointcut in project today-framework 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)

Example 25 with Pointcut

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

the class MethodValidationPostProcessor method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    Pointcut pointcut = new AnnotationMatchingPointcut(validatedAnnotationType, true);
    this.advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(validator));
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) AnnotationMatchingPointcut(cn.taketoday.aop.support.annotation.AnnotationMatchingPointcut) 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