use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testCannotRemoveAdvisorWhenFrozen.
@Test
public void testCannotRemoveAdvisorWhenFrozen() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThat(pc.isFrozen()).isFalse();
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to remove Advisor when frozen").isThrownBy(() -> advised.removeAdvisor(0)).withMessageContaining("frozen");
// Didn't get removed
assertThat(advised.getAdvisors().length).isEqualTo(1);
pc.setFrozen(false);
// Can now remove it
advised.removeAdvisor(0);
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(advised.getAdvisors().length).isEqualTo(0);
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class ProxyFactoryTests method testIndexOfMethods.
@Test
public void testIndexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
Advised advised = (Advised) pf.getProxy();
// Can use advised and ProxyFactory interchangeably
advised.addAdvice(nop);
pf.addAdvisor(advisor);
assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
assertThat(pf.indexOf(nop)).isEqualTo(0);
assertThat(pf.indexOf(advisor)).isEqualTo(1);
assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class UnsupportedInterceptor method testAddAdviceAtRuntime.
@Test
public void testAddAdviceAtRuntime() {
TestBean bean = new TestBean();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
ProxyFactory pf = new ProxyFactory();
pf.setTarget(bean);
pf.setFrozen(false);
pf.setOpaque(false);
pf.setProxyTargetClass(true);
TestBean proxy = (TestBean) pf.getProxy();
assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
proxy.getAge();
assertThat(cba.getCalls()).isEqualTo(0);
((Advised) proxy).addAdvice(cba);
proxy.getAge();
assertThat(cba.getCalls()).isEqualTo(1);
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testCannotAddAdvisorWhenFrozenUsingCast.
/**
* Check that casting to Advised can't get around advice freeze.
*/
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThat(pc.isFrozen()).isFalse();
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()))).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(advised.getAdvisors().length).isEqualTo(1);
}
use of cn.taketoday.aop.framework.Advised in project today-framework by TAKETODAY.
the class MethodInvokerUtils method getMethodInvokerByAnnotation.
/**
* Create {@link MethodInvoker} for the method with the provided annotation on the
* provided object. Annotations that cannot be applied to methods (i.e. that aren't
* annotated with an element type of METHOD) will cause an exception to be thrown.
*
* @param annotationType to be searched for
* @param target to be invoked
* @return MethodInvoker for the provided annotation, null if none is found.
*/
public static MethodInvoker getMethodInvokerByAnnotation(Class<? extends Annotation> annotationType, Object target) {
Assert.notNull(target, "Target must not be null");
Assert.notNull(annotationType, "AnnotationType must not be null");
if (!ObjectUtils.containsElement(annotationType.getAnnotation(Target.class).value(), ElementType.METHOD)) {
throw new IllegalArgumentException("Annotation [" + annotationType + "] is not a Method-level annotation.");
}
Class<?> targetClass = target instanceof Advised ? ((Advised) target).getTargetSource().getTargetClass() : target.getClass();
if (targetClass == null) {
// Proxy with no target cannot have annotations
return null;
}
final AtomicReference<Method> annotatedMethod = new AtomicReference<>();
ReflectionUtils.doWithMethods(targetClass, method -> {
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
if (annotation != null) {
if (annotatedMethod.get() != null) {
throw new IllegalArgumentException("found more than one method on target class [" + targetClass.getSimpleName() + "] with the annotation type [" + annotationType.getSimpleName() + "].");
}
annotatedMethod.set(method);
}
});
Method method = annotatedMethod.get();
if (method == null) {
return null;
} else {
return new SimpleMethodInvoker(target, annotatedMethod.get());
}
}
Aggregations