Search in sources :

Example 6 with Advised

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

the class JoinPointMonitorAtAspectJAspect method checkXmlAspect.

private void checkXmlAspect(String appContextFile) {
    ApplicationContext context = new ClassPathXmlApplicationContext(appContextFile, getClass());
    ICounter counter = (ICounter) context.getBean("counter");
    boolean condition = counter instanceof Advised;
    assertThat(condition).as("Proxy didn't get created").isTrue();
    counter.increment();
    JoinPointMonitorAspect callCountingAspect = (JoinPointMonitorAspect) context.getBean("monitoringAspect");
    assertThat(callCountingAspect.beforeExecutions).as("Advise didn't get executed").isEqualTo(1);
    assertThat(callCountingAspect.aroundExecutions).as("Advise didn't get executed").isEqualTo(1);
}
Also used : ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) Advised(cn.taketoday.aop.framework.Advised)

Example 7 with Advised

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

the class CreatesTestBean method withFrozenProxy.

@Test
void withFrozenProxy() {
    ITestBean testBean = (ITestBean) beanFactory.getBean("frozenBean");
    assertThat(((Advised) testBean).isFrozen()).isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 8 with Advised

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

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor) Test(org.junit.jupiter.api.Test)

Example 9 with Advised

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

the class SimpleBeforeAdviceInterceptor method getAdviceImpl.

private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
    Advised advised = (Advised) tb;
    Advisor advisor = advised.getAdvisors()[0];
    return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
Also used : Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor)

Example 10 with Advised

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