use of org.springframework.aop.support.DefaultPointcutAdvisor 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.springframework.aop.support.DefaultPointcutAdvisor 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();
}
use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testValuesStick.
/**
* Simple test that if we set values we can get them out again.
*/
@Test
public void testValuesStick() {
int age1 = 33;
int age2 = 37;
String name = "tony";
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
ITestBean tb = (ITestBean) pf1.getProxy();
assertThat(tb.getAge()).isEqualTo(age1);
tb.setAge(age2);
assertThat(tb.getAge()).isEqualTo(age2);
assertThat(tb.getName()).isNull();
tb.setName(name);
assertThat(tb.getName()).isEqualTo(name);
}
use of org.springframework.aop.support.DefaultPointcutAdvisor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
pc.addAdvisor(advisor);
assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
pc.setOpaque(true);
assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertThat(proxied.getAge()).isEqualTo(10);
assertThat(mba.getCalls()).isEqualTo(1);
boolean condition = proxied instanceof Advised;
assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
Aggregations