This commit is contained in:
Chris Beams 2008-12-12 18:22:29 +00:00
parent 3c0015c1ec
commit 56908e32cd
4 changed files with 46 additions and 35 deletions

View File

@ -43,6 +43,6 @@ public interface DynamicIntroductionAdvice extends Advice {
* @param intf the interface to check * @param intf the interface to check
* @return whether the advice implements the specified interface * @return whether the advice implements the specified interface
*/ */
boolean implementsInterface(Class intf); boolean implementsInterface(Class<?> intf);
} }

View File

@ -57,7 +57,7 @@ public interface MethodMatcher {
* the candidate class must be taken to be the method's declaring class) * the candidate class must be taken to be the method's declaring class)
* @return whether or not this method matches statically * @return whether or not this method matches statically
*/ */
boolean matches(Method method, Class targetClass); boolean matches(Method method, Class<?> targetClass);
/** /**
* Is this MethodMatcher dynamic, that is, must a final call be made on the * Is this MethodMatcher dynamic, that is, must a final call be made on the
@ -86,7 +86,7 @@ public interface MethodMatcher {
* @return whether there's a runtime match * @return whether there's a runtime match
* @see MethodMatcher#matches(Method, Class) * @see MethodMatcher#matches(Method, Class)
*/ */
boolean matches(Method method, Class targetClass, Object[] args); boolean matches(Method method, Class<?> targetClass, Object[] args);
/** /**

View File

@ -34,7 +34,7 @@ public abstract class DynamicMethodMatcher implements MethodMatcher {
* Can override to add preconditions for dynamic matching. This implementation * Can override to add preconditions for dynamic matching. This implementation
* always returns true. * always returns true.
*/ */
public boolean matches(Method method, Class targetClass) { public boolean matches(Method method, Class<?> targetClass) {
return true; return true;
} }

View File

@ -930,7 +930,7 @@ public abstract class AbstractAopProxyTests {
pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor())); pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
ITestBean proxy2 = (ITestBean) createProxy(pf2); ITestBean proxy2 = (ITestBean) createProxy(pf2);
HashMap h = new HashMap(); HashMap<ITestBean, Object> h = new HashMap<ITestBean, Object>();
Object value1 = "foo"; Object value1 = "foo";
Object value2 = "bar"; Object value2 = "bar";
assertNull(h.get(proxy1)); assertNull(h.get(proxy1));
@ -1070,7 +1070,7 @@ public abstract class AbstractAopProxyTests {
pc.setTarget(tb); pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc); ITestBean it = (ITestBean) createProxy(pc);
assertEquals(dp.count, 0); assertEquals(dp.count, 0);
int age = it.getAge(); it.getAge();
assertEquals(dp.count, 1); assertEquals(dp.count, 1);
it.setAge(11); it.setAge(11);
assertEquals(it.getAge(), 11); assertEquals(it.getAge(), 11);
@ -1088,7 +1088,7 @@ public abstract class AbstractAopProxyTests {
pc.setTargetSource(mockTargetSource); pc.setTargetSource(mockTargetSource);
ITestBean it = (ITestBean) createProxy(pc); ITestBean it = (ITestBean) createProxy(pc);
assertEquals(dp.count, 0); assertEquals(dp.count, 0);
int age = it.getAge(); it.getAge();
// Statically vetoed // Statically vetoed
assertEquals(0, dp.count); assertEquals(0, dp.count);
it.setAge(11); it.setAge(11);
@ -1109,7 +1109,7 @@ public abstract class AbstractAopProxyTests {
pc.setTarget(tb); pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc); ITestBean it = (ITestBean) createProxy(pc);
assertEquals(di.getCount(), 0); assertEquals(di.getCount(), 0);
int age = it.getAge(); it.getAge();
assertEquals(di.getCount(), 1); assertEquals(di.getCount(), 1);
it.setAge(11); it.setAge(11);
assertEquals(it.getAge(), 11); assertEquals(it.getAge(), 11);
@ -1137,8 +1137,9 @@ public abstract class AbstractAopProxyTests {
return mi.proceed(); return mi.proceed();
} }
}; };
@SuppressWarnings("serial")
StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) { StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return "haveBirthday".equals(m.getName()); return "haveBirthday".equals(m.getName());
} }
}; };
@ -1178,7 +1179,7 @@ public abstract class AbstractAopProxyTests {
}; };
class NameSaver implements MethodInterceptor { class NameSaver implements MethodInterceptor {
private List names = new LinkedList(); private List<Object> names = new LinkedList<Object>();
public Object invoke(MethodInvocation mi) throws Throwable { public Object invoke(MethodInvocation mi) throws Throwable {
names.add(mi.getArguments()[0]); names.add(mi.getArguments()[0]);
@ -1206,19 +1207,20 @@ public abstract class AbstractAopProxyTests {
assertEquals(name1, saver.names.get(1)); assertEquals(name1, saver.names.get(1));
} }
@SuppressWarnings("serial")
@Test @Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable { public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads(); Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target); ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor(); NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) { pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterTypes().length == 0; return m.getName().equals("overload") && m.getParameterTypes().length == 0;
} }
}); });
NopInterceptor overLoadInts = new NopInterceptor(); NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) { pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterTypes().length == 1 && return m.getName().equals("overload") && m.getParameterTypes().length == 1 &&
m.getParameterTypes()[0].equals(int.class); m.getParameterTypes()[0].equals(int.class);
} }
@ -1248,7 +1250,7 @@ public abstract class AbstractAopProxyTests {
Advised config = (Advised) proxy; Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call // This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() { config.setTargetSource(new TargetSource() {
public Class getTargetClass() { public Class<?> getTargetClass() {
return TestBean.class; return TestBean.class;
} }
@ -1302,8 +1304,9 @@ public abstract class AbstractAopProxyTests {
@Test @Test
public void testBeforeAdvisorIsInvoked() { public void testBeforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice(); CountingBeforeAdvice cba = new CountingBeforeAdvice();
@SuppressWarnings("serial")
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) { Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterTypes().length == 0; return m.getParameterTypes().length == 0;
} }
}; };
@ -1329,15 +1332,15 @@ public abstract class AbstractAopProxyTests {
@Test @Test
public void testUserAttributes() throws Throwable { public void testUserAttributes() throws Throwable {
class MapAwareMethodInterceptor implements MethodInterceptor { class MapAwareMethodInterceptor implements MethodInterceptor {
private final Map expectedValues; private final Map<String, String> expectedValues;
private final Map valuesToAdd; private final Map<String, String> valuesToAdd;
public MapAwareMethodInterceptor(Map expectedValues, Map valuesToAdd) { public MapAwareMethodInterceptor(Map<String, String> expectedValues, Map<String, String> valuesToAdd) {
this.expectedValues = expectedValues; this.expectedValues = expectedValues;
this.valuesToAdd = valuesToAdd; this.valuesToAdd = valuesToAdd;
} }
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(MethodInvocation invocation) throws Throwable {
ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation) invocation; ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation) invocation;
for (Iterator it = rmi.getUserAttributes().keySet().iterator(); it.hasNext(); ){ for (Iterator<String> it = rmi.getUserAttributes().keySet().iterator(); it.hasNext(); ){
Object key = it.next(); Object key = it.next();
assertEquals(expectedValues.get(key), rmi.getUserAttributes().get(key)); assertEquals(expectedValues.get(key), rmi.getUserAttributes().get(key));
} }
@ -1346,17 +1349,17 @@ public abstract class AbstractAopProxyTests {
} }
}; };
AdvisedSupport pc = new AdvisedSupport(new Class[] {ITestBean.class}); AdvisedSupport pc = new AdvisedSupport(new Class[] {ITestBean.class});
MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap(), new HashMap()); MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<String, String>(), new HashMap<String, String>());
Map firstValuesToAdd = new HashMap(); Map<String, String> firstValuesToAdd = new HashMap<String, String>();
firstValuesToAdd.put("test", ""); firstValuesToAdd.put("test", "");
MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap(), firstValuesToAdd); MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap<String, String>(), firstValuesToAdd);
MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap()); MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<String, String>());
MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap()); MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<String, String>());
Map secondValuesToAdd = new HashMap(); Map<String, String> secondValuesToAdd = new HashMap<String, String>();
secondValuesToAdd.put("foo", "bar"); secondValuesToAdd.put("foo", "bar");
secondValuesToAdd.put("cat", "dog"); secondValuesToAdd.put("cat", "dog");
MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd); MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
Map finalExpected = new HashMap(firstValuesToAdd); Map<String, String> finalExpected = new HashMap<String, String>(firstValuesToAdd);
finalExpected.putAll(secondValuesToAdd); finalExpected.putAll(secondValuesToAdd);
MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd); MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
@ -1380,8 +1383,9 @@ public abstract class AbstractAopProxyTests {
@Test @Test
public void testMultiAdvice() throws Throwable { public void testMultiAdvice() throws Throwable {
CountingMultiAdvice cca = new CountingMultiAdvice(); CountingMultiAdvice cca = new CountingMultiAdvice();
@SuppressWarnings("serial")
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) { Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterTypes().length == 0 || "exceptional".equals(m.getName()); return m.getParameterTypes().length == 0 || "exceptional".equals(m.getName());
} }
}; };
@ -1417,6 +1421,7 @@ public abstract class AbstractAopProxyTests {
@Test @Test
public void testBeforeAdviceThrowsException() { public void testBeforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException(); final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial")
CountingBeforeAdvice ba = new CountingBeforeAdvice() { CountingBeforeAdvice ba = new CountingBeforeAdvice() {
public void before(Method m, Object[] args, Object target) throws Throwable { public void before(Method m, Object[] args, Object target) throws Throwable {
super.before(m, args, target); super.before(m, args, target);
@ -1466,8 +1471,9 @@ public abstract class AbstractAopProxyTests {
} }
} }
SummingAfterAdvice aa = new SummingAfterAdvice(); SummingAfterAdvice aa = new SummingAfterAdvice();
@SuppressWarnings("serial")
Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) { Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getReturnType() == int.class; return m.getReturnType() == int.class;
} }
}; };
@ -1522,8 +1528,9 @@ public abstract class AbstractAopProxyTests {
public void testThrowsAdvisorIsInvoked() throws Throwable { public void testThrowsAdvisorIsInvoked() throws Throwable {
// Reacts to ServletException and RemoteException // Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler(); MyThrowsHandler th = new MyThrowsHandler();
@SuppressWarnings("serial")
Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) { Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("echo"); return m.getName().startsWith("echo");
} }
}; };
@ -1651,6 +1658,7 @@ public abstract class AbstractAopProxyTests {
/** /**
* Fires on setter methods that take a string. Replaces null arg with "". * Fires on setter methods that take a string. Replaces null arg with "".
*/ */
@SuppressWarnings("serial")
protected static class StringSetterNullReplacementAdvice extends DefaultPointcutAdvisor { protected static class StringSetterNullReplacementAdvice extends DefaultPointcutAdvisor {
private static MethodInterceptor cleaner = new MethodInterceptor() { private static MethodInterceptor cleaner = new MethodInterceptor() {
@ -1664,10 +1672,10 @@ public abstract class AbstractAopProxyTests {
public StringSetterNullReplacementAdvice() { public StringSetterNullReplacementAdvice() {
super(cleaner); super(cleaner);
setPointcut(new DynamicMethodMatcherPointcut() { setPointcut(new DynamicMethodMatcherPointcut() {
public boolean matches(Method m, Class targetClass, Object[] args) { public boolean matches(Method m, Class<?> targetClass, Object[] args) {
return args[0] == null; return args[0] == null;
} }
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("set") && return m.getName().startsWith("set") &&
m.getParameterTypes().length == 1 && m.getParameterTypes().length == 1 &&
m.getParameterTypes()[0].equals(String.class); m.getParameterTypes()[0].equals(String.class);
@ -1677,6 +1685,7 @@ public abstract class AbstractAopProxyTests {
} }
@SuppressWarnings("serial")
protected static class TestDynamicPointcutAdvice extends DefaultPointcutAdvisor { protected static class TestDynamicPointcutAdvice extends DefaultPointcutAdvisor {
public int count; public int count;
@ -1684,7 +1693,7 @@ public abstract class AbstractAopProxyTests {
public TestDynamicPointcutAdvice(MethodInterceptor mi, final String pattern) { public TestDynamicPointcutAdvice(MethodInterceptor mi, final String pattern) {
super(mi); super(mi);
setPointcut(new DynamicMethodMatcherPointcut() { setPointcut(new DynamicMethodMatcherPointcut() {
public boolean matches(Method m, Class targetClass, Object[] args) { public boolean matches(Method m, Class<?> targetClass, Object[] args) {
boolean run = m.getName().indexOf(pattern) != -1; boolean run = m.getName().indexOf(pattern) != -1;
if (run) ++count; if (run) ++count;
return run; return run;
@ -1694,6 +1703,7 @@ public abstract class AbstractAopProxyTests {
} }
@SuppressWarnings("serial")
protected static class TestDynamicPointcutForSettersOnly extends DefaultPointcutAdvisor { protected static class TestDynamicPointcutForSettersOnly extends DefaultPointcutAdvisor {
public int count; public int count;
@ -1701,12 +1711,12 @@ public abstract class AbstractAopProxyTests {
public TestDynamicPointcutForSettersOnly(MethodInterceptor mi, final String pattern) { public TestDynamicPointcutForSettersOnly(MethodInterceptor mi, final String pattern) {
super(mi); super(mi);
setPointcut(new DynamicMethodMatcherPointcut() { setPointcut(new DynamicMethodMatcherPointcut() {
public boolean matches(Method m, Class targetClass, Object[] args) { public boolean matches(Method m, Class<?> targetClass, Object[] args) {
boolean run = m.getName().indexOf(pattern) != -1; boolean run = m.getName().indexOf(pattern) != -1;
if (run) ++count; if (run) ++count;
return run; return run;
} }
public boolean matches(Method m, Class clazz) { public boolean matches(Method m, Class<?> clazz) {
return m.getName().startsWith("set"); return m.getName().startsWith("set");
} }
}); });
@ -1714,6 +1724,7 @@ public abstract class AbstractAopProxyTests {
} }
@SuppressWarnings("serial")
protected static class TestStaticPointcutAdvice extends StaticMethodMatcherPointcutAdvisor { protected static class TestStaticPointcutAdvice extends StaticMethodMatcherPointcutAdvisor {
private String pattern; private String pattern;
@ -1723,7 +1734,7 @@ public abstract class AbstractAopProxyTests {
super(mi); super(mi);
this.pattern = pattern; this.pattern = pattern;
} }
public boolean matches(Method m, Class targetClass) { public boolean matches(Method m, Class<?> targetClass) {
boolean run = m.getName().indexOf(pattern) != -1; boolean run = m.getName().indexOf(pattern) != -1;
if (run) ++count; if (run) ++count;
return run; return run;
@ -1750,7 +1761,7 @@ public abstract class AbstractAopProxyTests {
private static class DummyIntroductionAdviceImpl implements DynamicIntroductionAdvice { private static class DummyIntroductionAdviceImpl implements DynamicIntroductionAdvice {
public boolean implementsInterface(Class intf) { public boolean implementsInterface(Class<?> intf) {
return true; return true;
} }
} }