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