Search in sources :

Example 1 with ProxyFactory

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

the class PerThisAspect method createProxy.

protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
    ProxyFactory pf = new ProxyFactory(target);
    if (interfaces.length > 1 || interfaces[0].isInterface()) {
        pf.setInterfaces(interfaces);
    } else {
        pf.setProxyTargetClass(true);
    }
    // Required everywhere we use AspectJ proxies
    pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pf.addAdvisors(advisors);
    pf.setExposeProxy(true);
    return pf.getProxy();
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory)

Example 2 with ProxyFactory

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

the class TigerAspectJExpressionPointcutTests method testAnnotationOnCglibProxyMethod.

@Test
public void testAnnotationOnCglibProxyMethod() throws Exception {
    String expression = "@annotation(test.annotation.transaction.Tx)";
    AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
    ajexp.setExpression(expression);
    ProxyFactory factory = new ProxyFactory(new BeanA());
    factory.setProxyTargetClass(true);
    BeanA proxy = (BeanA) factory.getProxy();
    assertThat(ajexp.matches(BeanA.class.getMethod("getAge"), proxy.getClass())).isTrue();
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 3 with ProxyFactory

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

the class PersistenceExceptionTranslationAdvisorTests method createProxy.

protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) {
    MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
    mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1));
    ProxyFactory pf = new ProxyFactory(target);
    pf.addInterface(RepositoryInterface.class);
    addPersistenceExceptionTranslation(pf, mpet);
    return (RepositoryInterface) pf.getProxy();
}
Also used : MapPersistenceExceptionTranslator(cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException)

Example 4 with ProxyFactory

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

the class AsyncAnnotationBeanPostProcessorTests method invokedAsynchronouslyOnProxyTarget.

@Test
public void invokedAsynchronouslyOnProxyTarget() {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBeanDefinition("postProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class));
    TestBean tb = new TestBean();
    ProxyFactory pf = new ProxyFactory(ITestBean.class, (MethodInterceptor) invocation -> invocation.getMethod().invoke(tb, invocation.getArguments()));
    context.registerBean("target", ITestBean.class, () -> (ITestBean) pf.getProxy());
    context.refresh();
    ITestBean testBean = context.getBean("target", ITestBean.class);
    testBean.test();
    Thread mainThread = Thread.currentThread();
    testBean.await(3000);
    Thread asyncThread = testBean.getThread();
    assertThat(asyncThread).isNotSameAs(mainThread);
    context.close();
}
Also used : Configuration(cn.taketoday.context.annotation.Configuration) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AsyncUncaughtExceptionHandler(cn.taketoday.aop.interceptor.AsyncUncaughtExceptionHandler) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Future(java.util.concurrent.Future) ClassPathResource(cn.taketoday.core.io.ClassPathResource) AopUtils(cn.taketoday.aop.support.AopUtils) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ThreadPoolTaskExecutor(cn.taketoday.scheduling.concurrent.ThreadPoolTaskExecutor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Method(java.lang.reflect.Method) Executor(java.util.concurrent.Executor) Bean(cn.taketoday.context.annotation.Bean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ReflectionUtils(cn.taketoday.util.ReflectionUtils) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) ListenableFuture(cn.taketoday.util.concurrent.ListenableFuture) GenericXmlApplicationContext(cn.taketoday.context.support.GenericXmlApplicationContext) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with ProxyFactory

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

the class ApplicationListenerMethodAdapterTests method invokeListenerInvalidProxy.

@Test
public void invokeListenerInvalidProxy() {
    Object target = new InvalidProxyTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(target);
    proxyFactory.addInterface(SimpleService.class);
    Object bean = proxyFactory.getProxy(getClass().getClassLoader());
    Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
    StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean);
    assertThatIllegalStateException().isThrownBy(() -> listener.onApplicationEvent(createGenericTestEvent("test"))).withMessageContaining("handleIt2");
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Aggregations

ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)206 Test (org.junit.jupiter.api.Test)171 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)91 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)86 NopInterceptor (cn.taketoday.aop.NopInterceptor)39 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)21 Advised (cn.taketoday.aop.framework.Advised)19 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)17 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)16 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)16 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)16 IOException (java.io.IOException)16 TransactionInterceptor (cn.taketoday.transaction.interceptor.TransactionInterceptor)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Method (java.lang.reflect.Method)12 Advisor (cn.taketoday.aop.Advisor)10 MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)10 AopUtils (cn.taketoday.aop.support.AopUtils)10 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)10