Search in sources :

Example 21 with Advised

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);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopConfigException(cn.taketoday.aop.framework.AopConfigException) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 22 with Advised

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);
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 23 with Advised

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);
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 24 with Advised

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);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) AopConfigException(cn.taketoday.aop.framework.AopConfigException) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 25 with Advised

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());
    }
}
Also used : Target(java.lang.annotation.Target) Advised(cn.taketoday.aop.framework.Advised) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Aggregations

Advised (cn.taketoday.aop.framework.Advised)85 Test (org.junit.jupiter.api.Test)58 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)40 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)23 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)18 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)18 Advisor (cn.taketoday.aop.Advisor)13 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)12 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)12 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)12 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)10 Test (org.junit.Test)10 NopInterceptor (cn.taketoday.aop.NopInterceptor)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)8 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)7 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 RetryContext (cn.taketoday.retry.RetryContext)6 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)6 ArrayList (java.util.ArrayList)6