Consistent use of varargs plus related polishing

(cherry picked from commit deae872)
This commit is contained in:
Juergen Hoeller
2015-12-22 23:50:05 +01:00
parent bc671f05d6
commit 1cfeb07fa5
26 changed files with 390 additions and 365 deletions

View File

@@ -31,10 +31,13 @@ import java.util.Map;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import test.mixin.LockMixin;
import test.mixin.LockMixinAdvisor;
import test.mixin.Lockable;
import test.mixin.LockedException;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
@@ -72,11 +75,6 @@ import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
import org.springframework.util.StopWatch;
import test.mixin.LockMixin;
import test.mixin.LockMixinAdvisor;
import test.mixin.Lockable;
import test.mixin.LockedException;
import static org.junit.Assert.*;
/**
@@ -123,7 +121,7 @@ public abstract class AbstractAopProxyTests {
@Test(expected = AopConfigException.class)
public void testNoInterceptorsAndNoTarget() {
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
// Add no interceptors
AopProxy aop = createAopProxy(pc);
aop.getProxy();
@@ -408,7 +406,7 @@ public abstract class AbstractAopProxyTests {
return s;
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
if (context) {
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
}
@@ -452,7 +450,7 @@ public abstract class AbstractAopProxyTests {
throw expectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
@@ -487,7 +485,7 @@ public abstract class AbstractAopProxyTests {
throw unexpectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
@@ -520,7 +518,7 @@ public abstract class AbstractAopProxyTests {
throw unexpectedException;
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
pc.addAdvice(mi);
@@ -548,7 +546,7 @@ public abstract class AbstractAopProxyTests {
@Test
public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable {
NeedsToSeeProxy target = new NeedsToSeeProxy();
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {INeedsToSeeProxy.class});
AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class);
pc.setTarget(target);
pc.setExposeProxy(true);
@@ -563,7 +561,7 @@ public abstract class AbstractAopProxyTests {
public void testTargetCanGetInvocation() throws Throwable {
final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
@@ -600,7 +598,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testMixinWithIntroductionAdvisor() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
pc.addAdvisor(new LockMixinAdvisor());
pc.setTarget(tb);
@@ -610,7 +609,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testMixinWithIntroductionInfo() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
// We don't use an IntroductionAdvisor, we can just add an advice that implements IntroductionInfo
pc.addAdvice(new LockMixin());
pc.setTarget(tb);
@@ -648,7 +648,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testReplaceArgument() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
pc.setTarget(tb);
pc.addAdvisor(new StringSetterNullReplacementAdvice());
@@ -825,7 +826,7 @@ public abstract class AbstractAopProxyTests {
fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface");
}
catch (IllegalArgumentException ex) {
assertTrue(ex.getMessage().indexOf("interface") > -1);
assertTrue(ex.getMessage().contains("interface"));
}
// Check it still works: proxy factory state shouldn't have been corrupted
ITestBean proxied = (ITestBean) createProxy(pc);
@@ -873,7 +874,7 @@ public abstract class AbstractAopProxyTests {
fail("Shouldn't be able to add Advisor when frozen");
}
catch (AopConfigException ex) {
assertTrue(ex.getMessage().indexOf("frozen") > -1);
assertTrue(ex.getMessage().contains("frozen"));
}
// Check it still works: proxy factory state shouldn't have been corrupted
assertEquals(target.getAge(), proxied.getAge());
@@ -897,7 +898,7 @@ public abstract class AbstractAopProxyTests {
fail("Shouldn't be able to remove Advisor when frozen");
}
catch (AopConfigException ex) {
assertTrue(ex.getMessage().indexOf("frozen") > -1);
assertTrue(ex.getMessage().contains("frozen"));
}
// Didn't get removed
assertEquals(1, advised.getAdvisors().length);
@@ -938,7 +939,7 @@ public abstract class AbstractAopProxyTests {
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(new Class<?>[] {ITestBean.class});
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
@@ -946,15 +947,15 @@ public abstract class AbstractAopProxyTests {
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertTrue(proxyConfigString.indexOf(advisor.toString()) != -1);
assertTrue(proxyConfigString.indexOf("1 interface") != -1);
assertTrue(proxyConfigString.contains(advisor.toString()));
assertTrue(proxyConfigString.contains("1 interface"));
}
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(new Class<?>[] {ITestBean.class});
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
@@ -1055,7 +1056,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
pc.addAdvisor(dp);
pc.setTarget(tb);
@@ -1071,7 +1073,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
// Could apply dynamically to getAge/setAge but not to getName
TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
pc.addAdvisor(dp);
@@ -1093,7 +1096,8 @@ public abstract class AbstractAopProxyTests {
@Test
public void testStaticMethodPointcut() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(new Class<?>[] {ITestBean.class});
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
NopInterceptor di = new NopInterceptor();
TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
pc.addAdvisor(sp);
@@ -1207,6 +1211,7 @@ public abstract class AbstractAopProxyTests {
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
@Override
@@ -1214,12 +1219,13 @@ public abstract class AbstractAopProxyTests {
return m.getName().equals("overload") && m.getParameterTypes().length == 0;
}
});
NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterTypes().length == 1 &&
m.getParameterTypes()[0].equals(int.class);
m.getParameterTypes()[0].equals(int.class);
}
});
@@ -1245,24 +1251,22 @@ public abstract class AbstractAopProxyTests {
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertEquals(proxy, AopContext.currentProxy());
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
@@ -1351,7 +1355,7 @@ public abstract class AbstractAopProxyTests {
return invocation.proceed();
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<String, String>(), new HashMap<String, String>());
Map<String, String> firstValuesToAdd = new HashMap<String, String>();
firstValuesToAdd.put("test", "");
@@ -1683,7 +1687,7 @@ public abstract class AbstractAopProxyTests {
super(cleaner);
setPointcut(new DynamicMethodMatcherPointcut() {
@Override
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
public boolean matches(Method m, Class<?> targetClass, Object... args) {
return args[0] == null;
}
@Override
@@ -1706,8 +1710,8 @@ public abstract class AbstractAopProxyTests {
super(mi);
setPointcut(new DynamicMethodMatcherPointcut() {
@Override
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
boolean run = m.getName().indexOf(pattern) != -1;
public boolean matches(Method m, Class<?> targetClass, Object... args) {
boolean run = m.getName().contains(pattern);
if (run) ++count;
return run;
}
@@ -1725,8 +1729,8 @@ public abstract class AbstractAopProxyTests {
super(mi);
setPointcut(new DynamicMethodMatcherPointcut() {
@Override
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
boolean run = m.getName().indexOf(pattern) != -1;
public boolean matches(Method m, Class<?> targetClass, Object... args) {
boolean run = m.getName().contains(pattern);
if (run) ++count;
return run;
}
@@ -1750,7 +1754,7 @@ public abstract class AbstractAopProxyTests {
}
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().indexOf(pattern) != -1;
return m.getName().contains(pattern);
}
}

View File

@@ -81,7 +81,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
@Test(expected = AopConfigException.class)
public void testNoTarget() {
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(new NopInterceptor());
AopProxy aop = createAopProxy(pc);
aop.getProxy();
@@ -93,7 +93,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
bean.value = "foo";
mockTargetSource.setTarget(bean);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
@@ -110,7 +110,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
bean.value = "foo";
mockTargetSource.setTarget(bean);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
@@ -130,7 +130,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
bean.value = "foo";
mockTargetSource.setTarget(bean);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
@@ -164,7 +164,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
CglibTestBean bean = new CglibTestBean();
bean.setName("Rob Harrop");
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTarget(bean);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
@@ -178,7 +178,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
CglibTestBean target = new CglibTestBean();
target.setName("Rob Harrop");
AdvisedSupport pc = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport pc = new AdvisedSupport();
pc.setFrozen(true);
pc.setTarget(target);
@@ -264,7 +264,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
target.reset();
mockTargetSource.setTarget(target);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport pc = new AdvisedSupport();
pc.setTargetSource(mockTargetSource);
CglibAopProxy aop = new CglibAopProxy(pc);
aop.setConstructorArguments(new Object[] {"Rob Harrop", 22}, new Class<?>[] {String.class, int.class});
@@ -280,7 +280,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
ITestBean target = new TestBean();
mockTargetSource.setTarget(target);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
CglibAopProxy cglib = new CglibAopProxy(as);
@@ -301,7 +301,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
ITestBean target = new TestBean();
mockTargetSource.setTarget(target);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
as.addInterface(Serializable.class);
@@ -324,7 +324,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab
ExceptionThrower bean = new ExceptionThrower();
mockTargetSource.setTarget(bean);
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{});
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);

View File

@@ -63,7 +63,7 @@ public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Seria
public void testProxyIsJustInterface() throws Throwable {
TestBean raw = new TestBean();
raw.setAge(32);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.setTarget(raw);
JdkDynamicAopProxy aop = new JdkDynamicAopProxy(pc);
@@ -78,7 +78,7 @@ public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Seria
final int age = 25;
MethodInterceptor mi = (invocation -> age);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(mi);
AopProxy aop = createAopProxy(pc);
@@ -97,7 +97,7 @@ public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Seria
}
};
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class});
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
@@ -129,7 +129,7 @@ public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Seria
@Test
public void testEqualsAndHashCodeDefined() throws Exception {
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
AdvisedSupport as = new AdvisedSupport(Named.class);
as.setTarget(new Person());
JdkDynamicAopProxy aopProxy = new JdkDynamicAopProxy(as);
Named proxy = (Named) aopProxy.getProxy();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@ public final class ProxyFactoryBeanTests {
private BeanFactory factory;
@Before
public void setUp() throws Exception {
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
@@ -97,6 +98,7 @@ public final class ProxyFactoryBeanTests {
new ClassPathResource(CONTEXT, getClass()));
}
@Test
public void testIsDynamicProxyWhenInterfaceSpecified() {
ITestBean test1 = (ITestBean) factory.getBean("test1");
@@ -157,7 +159,7 @@ public final class ProxyFactoryBeanTests {
catch (BeanCreationException ex) {
// Root cause of the problem must be an AOP exception
AopConfigException aex = (AopConfigException) ex.getCause();
assertTrue(aex.getMessage().indexOf("interceptorNames") != -1);
assertTrue(aex.getMessage().contains("interceptorNames"));
}
}
@@ -332,13 +334,6 @@ public final class ProxyFactoryBeanTests {
}
}
public static class DependsOnITestBean {
public final ITestBean tb;
public DependsOnITestBean(ITestBean tb) {
this.tb = tb;
}
}
/**
* Test that inner bean for target means that we can use
* autowire without ambiguity from target and proxy
@@ -711,6 +706,8 @@ public final class ProxyFactoryBeanTests {
ITestBean proxy = (ITestBean) fb.getObject();
assertTrue(AopUtils.isJdkDynamicProxy(proxy));
}
/**
* Fires only on void methods. Saves list of methods intercepted.
*/
@@ -733,7 +730,7 @@ public final class ProxyFactoryBeanTests {
});
setPointcut(new DynamicMethodMatcherPointcut() {
@Override
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
public boolean matches(Method m, Class<?> targetClass, Object... args) {
return m.getReturnType() == Void.TYPE;
}
});
@@ -741,10 +738,20 @@ public final class ProxyFactoryBeanTests {
}
public static class DependsOnITestBean {
public final ITestBean tb;
public DependsOnITestBean(ITestBean tb) {
this.tb = tb;
}
}
/**
* Aspect interface
*/
public interface AddedGlobalInterface {
int globalsAdded();
}