use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanChangeArgumentsIndependentlyOnClonedInvocation.
/**
* We want to change the arguments on a clone: it shouldn't affect the original.
*/
@Test
public void testCanChangeArgumentsIndependentlyOnClonedInvocation() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
/**
* Changes the name, then changes it back.
*/
MethodInterceptor nameReverter = mi -> {
MethodInvocation clone = ((ReflectiveMethodInvocation) mi).invocableClone();
String oldName = ((ITestBean) mi.getThis()).getName();
clone.getArguments()[0] = oldName;
// Original method invocation should be unaffected by changes to argument list of clone
mi.proceed();
return clone.proceed();
};
class NameSaver implements MethodInterceptor {
private List<Object> names = new ArrayList<>();
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
names.add(mi.getArguments()[0]);
return mi.proceed();
}
}
NameSaver saver = new NameSaver();
pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, nameReverter));
pc.addAdvisor(new DefaultPointcutAdvisor(Pointcuts.SETTERS, saver));
ITestBean it = (ITestBean) createProxy(pc);
String name1 = "tony";
String name2 = "gordon";
tb.setName(name1);
assertThat(tb.getName()).isEqualTo(name1);
it.setName(name2);
// NameReverter saved it back
assertThat(it.getName()).isEqualTo(name1);
assertThat(saver.names.size()).isEqualTo(2);
assertThat(saver.names.get(0)).isEqualTo(name2);
assertThat(saver.names.get(1)).isEqualTo(name1);
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testContext.
/**
* @param context if true, want context
*/
private void testContext(final boolean context) throws Throwable {
final String s = "foo";
// Test return value
MethodInterceptor mi = invocation -> {
if (!context) {
assertNoInvocationContext();
} else {
assertThat(ExposeInvocationInterceptor.currentInvocation()).as("have context").isNotNull();
}
return s;
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
if (context) {
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
}
pc.addAdvice(mi);
// Keep CGLIB happy
if (requiresTarget()) {
pc.setTarget(new TestBean());
}
AopProxy aop = createAopProxy(pc);
assertNoInvocationContext();
ITestBean tb = (ITestBean) aop.getProxy();
assertNoInvocationContext();
assertThat(tb.getName()).as("correct return value").isSameAs(s);
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testCanGetFactoryReferenceAndManipulate.
@Test
public void testCanGetFactoryReferenceAndManipulate() {
ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1");
assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
// Trigger lazy initialization.
config.getObject();
assertThat(config.getAdvisors().length).as("Have one advisors").isEqualTo(1);
assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
ITestBean tb = (ITestBean) factory.getBean("test1");
// no exception
tb.hashCode();
final Exception ex = new UnsupportedOperationException("invoke");
// Add evil interceptor to head of list
config.addAdvice(0, (MethodInterceptor) invocation -> {
throw ex;
});
assertThat(config.getAdvisors().length).as("Have correct advisor count").isEqualTo(2);
ITestBean tb1 = (ITestBean) factory.getBean("test1");
assertThatExceptionOfType(Exception.class).isThrownBy(tb1::toString).isSameAs(ex);
}
use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
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 org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.
the class UnsupportedInterceptor method getAdvisedProxy.
private ITestBean getAdvisedProxy(TestBean target) {
ProxyFactory pf = new ProxyFactory(new Class<?>[] { ITestBean.class });
pf.setProxyTargetClass(true);
MethodInterceptor advice = new NopInterceptor();
Pointcut pointcut = new Pointcut() {
@Override
public ClassFilter getClassFilter() {
return ClassFilter.TRUE;
}
@Override
public MethodMatcher getMethodMatcher() {
return MethodMatcher.TRUE;
}
@Override
public boolean equals(Object obj) {
return true;
}
@Override
public int hashCode() {
return 0;
}
};
pf.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice));
pf.setTarget(target);
pf.setFrozen(true);
pf.setExposeProxy(false);
return (ITestBean) pf.getProxy();
}
Aggregations