|
|
@ -17,7 +17,13 @@ |
|
|
|
package org.springframework.util; |
|
|
|
package org.springframework.util; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.is; |
|
|
|
import static org.hamcrest.CoreMatchers.is; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertNull; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertThat; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
import static org.junit.Assert.fail; |
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Method; |
|
|
@ -27,8 +33,8 @@ import java.rmi.RemoteException; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Ignore; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.TestBean; |
|
|
|
import org.springframework.beans.TestBean; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -90,25 +96,25 @@ public class ReflectionUtilsTests { |
|
|
|
bean.setName(rob); |
|
|
|
bean.setName(rob); |
|
|
|
|
|
|
|
|
|
|
|
Method getName = TestBean.class.getMethod("getName", (Class[]) null); |
|
|
|
Method getName = TestBean.class.getMethod("getName", (Class[]) null); |
|
|
|
Method setName = TestBean.class.getMethod("setName", new Class[]{String.class}); |
|
|
|
Method setName = TestBean.class.getMethod("setName", new Class[] { String.class }); |
|
|
|
|
|
|
|
|
|
|
|
Object name = ReflectionUtils.invokeMethod(getName, bean); |
|
|
|
Object name = ReflectionUtils.invokeMethod(getName, bean); |
|
|
|
assertEquals("Incorrect name returned", rob, name); |
|
|
|
assertEquals("Incorrect name returned", rob, name); |
|
|
|
|
|
|
|
|
|
|
|
String juergen = "Juergen Hoeller"; |
|
|
|
String juergen = "Juergen Hoeller"; |
|
|
|
ReflectionUtils.invokeMethod(setName, bean, new Object[]{juergen}); |
|
|
|
ReflectionUtils.invokeMethod(setName, bean, new Object[] { juergen }); |
|
|
|
assertEquals("Incorrect name set", juergen, bean.getName()); |
|
|
|
assertEquals("Incorrect name set", juergen, bean.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void declaresException() throws Exception { |
|
|
|
public void declaresException() throws Exception { |
|
|
|
Method remoteExMethod = A.class.getDeclaredMethod("foo", new Class[]{Integer.class}); |
|
|
|
Method remoteExMethod = A.class.getDeclaredMethod("foo", new Class[] { Integer.class }); |
|
|
|
assertTrue(ReflectionUtils.declaresException(remoteExMethod, RemoteException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(remoteExMethod, RemoteException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(remoteExMethod, ConnectException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(remoteExMethod, ConnectException.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(remoteExMethod, NoSuchMethodException.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(remoteExMethod, NoSuchMethodException.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(remoteExMethod, Exception.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(remoteExMethod, Exception.class)); |
|
|
|
|
|
|
|
|
|
|
|
Method illegalExMethod = B.class.getDeclaredMethod("bar", new Class[]{String.class}); |
|
|
|
Method illegalExMethod = B.class.getDeclaredMethod("bar", new Class[] { String.class }); |
|
|
|
assertTrue(ReflectionUtils.declaresException(illegalExMethod, IllegalArgumentException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(illegalExMethod, IllegalArgumentException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(illegalExMethod, NumberFormatException.class)); |
|
|
|
assertTrue(ReflectionUtils.declaresException(illegalExMethod, NumberFormatException.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(illegalExMethod, IllegalStateException.class)); |
|
|
|
assertFalse(ReflectionUtils.declaresException(illegalExMethod, IllegalStateException.class)); |
|
|
@ -122,8 +128,7 @@ public class ReflectionUtilsTests { |
|
|
|
try { |
|
|
|
try { |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
fail(); |
|
|
|
fail(); |
|
|
|
} |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
catch (IllegalArgumentException ex) { |
|
|
|
|
|
|
|
// Ok
|
|
|
|
// Ok
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -135,8 +140,7 @@ public class ReflectionUtilsTests { |
|
|
|
try { |
|
|
|
try { |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
fail(); |
|
|
|
fail(); |
|
|
|
} |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
catch (IllegalArgumentException ex) { |
|
|
|
|
|
|
|
// Ok
|
|
|
|
// Ok
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -148,8 +152,7 @@ public class ReflectionUtilsTests { |
|
|
|
try { |
|
|
|
try { |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
ReflectionUtils.shallowCopyFieldState(src, dest); |
|
|
|
fail(); |
|
|
|
fail(); |
|
|
|
} |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
catch (IllegalArgumentException ex) { |
|
|
|
|
|
|
|
// Ok
|
|
|
|
// Ok
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -240,18 +243,39 @@ public class ReflectionUtilsTests { |
|
|
|
assertNotNull(ReflectionUtils.findMethod(B.class, "getClass")); |
|
|
|
assertNotNull(ReflectionUtils.findMethod(B.class, "getClass")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Ignore("[SPR-8644] findMethod() does not currently support var-args") |
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void findMethodWithVarArgs() throws Exception { |
|
|
|
|
|
|
|
assertNotNull(ReflectionUtils.findMethod(B.class, "add", int.class, int.class, int.class)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void isCglibRenamedMethod() throws SecurityException, NoSuchMethodException { |
|
|
|
public void isCglibRenamedMethod() throws SecurityException, NoSuchMethodException { |
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
class C { |
|
|
|
class C { |
|
|
|
public void CGLIB$m1$123() { } |
|
|
|
public void CGLIB$m1$123() { |
|
|
|
public void CGLIB$m1$0() { } |
|
|
|
} |
|
|
|
public void CGLIB$$0() { } |
|
|
|
|
|
|
|
public void CGLIB$m1$() { } |
|
|
|
public void CGLIB$m1$0() { |
|
|
|
public void CGLIB$m1() { } |
|
|
|
} |
|
|
|
public void m1() { } |
|
|
|
|
|
|
|
public void m1$() { } |
|
|
|
public void CGLIB$$0() { |
|
|
|
public void m1$1() { } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void CGLIB$m1$() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void CGLIB$m1() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void m1() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void m1$() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void m1$1() { |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$123"))); |
|
|
|
assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$123"))); |
|
|
|
assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$0"))); |
|
|
|
assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$0"))); |
|
|
@ -300,6 +324,7 @@ public class ReflectionUtilsTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getUniqueDeclaredMethods_withCovariantReturnType() throws Exception { |
|
|
|
public void getUniqueDeclaredMethods_withCovariantReturnType() throws Exception { |
|
|
|
class Parent { |
|
|
|
class Parent { |
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
public Number m1() { |
|
|
|
public Number m1() { |
|
|
|
return new Integer(42); |
|
|
|
return new Integer(42); |
|
|
|
} |
|
|
|
} |
|
|
@ -322,7 +347,6 @@ public class ReflectionUtilsTests { |
|
|
|
assertFalse(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1"))); |
|
|
|
assertFalse(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1"))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback { |
|
|
|
private static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback { |
|
|
|
|
|
|
|
|
|
|
|
private List<String> methodNames = new LinkedList<String>(); |
|
|
|
private List<String> methodNames = new LinkedList<String>(); |
|
|
@ -338,12 +362,12 @@ public class ReflectionUtilsTests { |
|
|
|
return this.methodNames; |
|
|
|
return this.methodNames; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
public List<Method> getMethods() { |
|
|
|
public List<Method> getMethods() { |
|
|
|
return this.methods; |
|
|
|
return this.methods; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class TestBeanSubclass extends TestBean { |
|
|
|
private static class TestBeanSubclass extends TestBean { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -352,13 +376,12 @@ public class ReflectionUtilsTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class TestBeanSubclassWithPublicField extends TestBean { |
|
|
|
private static class TestBeanSubclassWithPublicField extends TestBean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
public String publicField = "foo"; |
|
|
|
public String publicField = "foo"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class TestBeanSubclassWithNewField extends TestBean { |
|
|
|
private static class TestBeanSubclassWithNewField extends TestBean { |
|
|
|
|
|
|
|
|
|
|
|
private int magic; |
|
|
|
private int magic; |
|
|
@ -366,24 +389,32 @@ public class ReflectionUtilsTests { |
|
|
|
protected String prot = "foo"; |
|
|
|
protected String prot = "foo"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class TestBeanSubclassWithFinalField extends TestBean { |
|
|
|
private static class TestBeanSubclassWithFinalField extends TestBean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
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 { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
private void foo(Integer i) throws RemoteException { |
|
|
|
private void foo(Integer i) throws RemoteException { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
private static class B extends A { |
|
|
|
private static class B extends A { |
|
|
|
|
|
|
|
|
|
|
|
void bar(String s) throws IllegalArgumentException { |
|
|
|
void bar(String s) throws IllegalArgumentException { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int add(int... args) { |
|
|
|
|
|
|
|
int sum = 0; |
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i++) { |
|
|
|
|
|
|
|
sum += args[i]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return sum; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|