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);
}
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());
}
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);
}
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();
}
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));
}
Aggregations