ReflectionUtils findMethod detects methods on "java.lang.Object" as well

This commit is contained in:
Juergen Hoeller 2009-02-13 09:14:08 +00:00
parent ffb9625abf
commit 796392db1a
2 changed files with 56 additions and 49 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -145,11 +145,11 @@ public abstract class ReflectionUtils {
* (may be <code>null</code> to indicate any signature) * (may be <code>null</code> to indicate any signature)
* @return the Method object, or <code>null</code> if none found * @return the Method object, or <code>null</code> if none found
*/ */
public static Method findMethod(Class clazz, String name, Class[] paramTypes) { public static Method findMethod(Class clazz, String name, Class... paramTypes) {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
Assert.notNull(name, "Method name must not be null"); Assert.notNull(name, "Method name must not be null");
Class searchType = clazz; Class searchType = clazz;
while (!Object.class.equals(searchType) && searchType != null) { while (searchType != null) {
Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods()); Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
for (Method method : methods) { for (Method method : methods) {
if (name.equals(method.getName()) && if (name.equals(method.getName()) &&
@ -173,7 +173,7 @@ public abstract class ReflectionUtils {
* @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
*/ */
public static Object invokeMethod(Method method, Object target) { public static Object invokeMethod(Method method, Object target) {
return invokeMethod(method, target, null); return invokeMethod(method, target, new Object[0]);
} }
/** /**
@ -186,7 +186,7 @@ public abstract class ReflectionUtils {
* @param args the invocation arguments (may be <code>null</code>) * @param args the invocation arguments (may be <code>null</code>)
* @return the invocation result, if any * @return the invocation result, if any
*/ */
public static Object invokeMethod(Method method, Object target, Object[] args) { public static Object invokeMethod(Method method, Object target, Object... args) {
try { try {
return method.invoke(target, args); return method.invoke(target, args);
} }
@ -206,7 +206,7 @@ public abstract class ReflectionUtils {
* @see #invokeJdbcMethod(java.lang.reflect.Method, Object, Object[]) * @see #invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])
*/ */
public static Object invokeJdbcMethod(Method method, Object target) throws SQLException { public static Object invokeJdbcMethod(Method method, Object target) throws SQLException {
return invokeJdbcMethod(method, target, null); return invokeJdbcMethod(method, target, new Object[0]);
} }
/** /**
@ -219,7 +219,7 @@ public abstract class ReflectionUtils {
* @throws SQLException the JDBC API SQLException to rethrow (if any) * @throws SQLException the JDBC API SQLException to rethrow (if any)
* @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
*/ */
public static Object invokeJdbcMethod(Method method, Object target, Object[] args) throws SQLException { public static Object invokeJdbcMethod(Method method, Object target, Object... args) throws SQLException {
try { try {
return method.invoke(target, args); return method.invoke(target, args);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.rmi.ConnectException; import java.rmi.ConnectException;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -31,8 +30,6 @@ import org.junit.Test;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
/** /**
* <p> JUnit 4 based unit tests for {@link ReflectionUtils}. </p>
*
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
@ -206,27 +203,8 @@ public class ReflectionUtilsTests {
assertEquals(src.getDoctor(), dest.getDoctor()); assertEquals(src.getDoctor(), dest.getDoctor());
} }
static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback { @Test
public void doWithProtectedMethods() {
private List<String> methodNames = new LinkedList<String>();
private List<Method> methods = new LinkedList<Method>();
public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException {
this.methodNames.add(m.getName());
this.methods.add(m);
}
public List getMethodNames() {
return this.methodNames;
}
public List getMethods() {
return this.methods;
}
}
public void testDoWithProtectedMethods() {
ListSavingMethodCallback mc = new ListSavingMethodCallback(); ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestBean.class, mc, new ReflectionUtils.MethodFilter() { ReflectionUtils.doWithMethods(TestBean.class, mc, new ReflectionUtils.MethodFilter() {
public boolean matches(Method m) { public boolean matches(Method m) {
@ -240,20 +218,12 @@ public class ReflectionUtilsTests {
assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate")); assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate"));
} }
public static class TestBeanSubclass extends TestBean { @Test
public void duplicatesFound() {
@Override
public void absquatulate() {
throw new UnsupportedOperationException();
}
}
public void testDuplicatesFound() {
ListSavingMethodCallback mc = new ListSavingMethodCallback(); ListSavingMethodCallback mc = new ListSavingMethodCallback();
ReflectionUtils.doWithMethods(TestBeanSubclass.class, mc); ReflectionUtils.doWithMethods(TestBeanSubclass.class, mc);
int absquatulateCount = 0; int absquatulateCount = 0;
for (Iterator it = mc.getMethodNames().iterator(); it.hasNext();) { for (String name : mc.getMethodNames()) {
String name = (String) it.next();
if (name.equals("absquatulate")) { if (name.equals("absquatulate")) {
++absquatulateCount; ++absquatulateCount;
} }
@ -261,34 +231,71 @@ public class ReflectionUtilsTests {
assertEquals("Found 2 absquatulates", 2, absquatulateCount); assertEquals("Found 2 absquatulates", 2, absquatulateCount);
} }
public void testFindMethod() throws Exception { @Test
assertNotNull(ReflectionUtils.findMethod(B.class, "bar", new Class[]{String.class})); public void findMethod() throws Exception {
assertNotNull(ReflectionUtils.findMethod(B.class, "foo", new Class[]{Integer.class})); assertNotNull(ReflectionUtils.findMethod(B.class, "bar", String.class));
assertNotNull(ReflectionUtils.findMethod(B.class, "foo", Integer.class));
assertNotNull(ReflectionUtils.findMethod(B.class, "getClass"));
} }
public static class TestBeanSubclassWithPublicField extends TestBean {
private static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback {
private List<String> methodNames = new LinkedList<String>();
private List<Method> methods = new LinkedList<Method>();
public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException {
this.methodNames.add(m.getName());
this.methods.add(m);
}
public List<String> getMethodNames() {
return this.methodNames;
}
public List<Method> getMethods() {
return this.methods;
}
}
private static class TestBeanSubclass extends TestBean {
@Override
public void absquatulate() {
throw new UnsupportedOperationException();
}
}
private static class TestBeanSubclassWithPublicField extends TestBean {
public String publicField = "foo"; public String publicField = "foo";
} }
public static class TestBeanSubclassWithNewField extends TestBean {
private static class TestBeanSubclassWithNewField extends TestBean {
private int magic; private int magic;
protected String prot = "foo"; protected String prot = "foo";
} }
public static class TestBeanSubclassWithFinalField extends TestBean {
private static class TestBeanSubclassWithFinalField extends TestBean {
private final String foo = "will break naive copy that doesn't exclude statics"; private final String foo = "will break naive copy that doesn't exclude statics";
} }
private static class A { private static class A {
private void foo(Integer i) throws RemoteException { private void foo(Integer i) throws RemoteException {
} }
} }
private static class B extends A { private static class B extends A {
void bar(String s) throws IllegalArgumentException { void bar(String s) throws IllegalArgumentException {