我使用getMethod(String name, Class[]类型)方法来获取方法,但当有int参数时,我找不到方法。我想我明白了,因为在我的Class数组中,我有java.lang.Integer类(包装器)而不是int。我通过使用泛型Object.getClass()来获取该类,所以我认为我不能那么容易地更改。以下是执行此操作的代码部分:
for (int i = 0; i < parameterTypes.length; i++) {
parameterTypes[i] = arguments[i].getClass();
}
try {
Method mmethod = mclass.getMethod(contractName, parameterTypes);
} catch (NoSuchMethodException e) {}
我能以某种方式解决这个问题吗?
假设你有这门课
class ReflectTest {
Object o = null;
public void setO(int i) {
System.out.println("set int");
o = i;
}
public void setO(Integer i) {
System.out.println("set Integer");
o = i;
}
}
setO(int i)
和setO(Integer i)
是两个不同的方法,所以你不能在你的类中只有一个,而是依靠自动装箱通过Class#getMethod(Class
@Test
public void invoke() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method method = ReflectTest.class.getMethod("setO", int.class);
method.invoke(new ReflectTest(), 3);
method.invoke(new ReflectTest(), Integer.valueOf(3));
method = ReflectTest.class.getMethod("setO", Integer.class);
method.invoke(new ReflectTest(), 3);
method.invoke(new ReflectTest(), Integer.valueOf(3));
}
都会打印
set int
set int
和
set Integer
set Integer
这里自动装箱适用于调用。
但是在你的情况下,你从存储为Object
的值中提取参数的类型。在这种情况下,原始类型被自动装箱到各自的包装类型中,因此你找不到对应于int. class
作为参数的方法。
@Test
public void invoke() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
invoke(new ReflectTest(), "setO", 3);
invoke(new ReflectTest(), "setO", Integer.valueOf(3));
}
private void invoke(Object instance, String methodeName, Object argValue) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println(argValue.getClass().isPrimitive());
Method method = ReflectTest.class.getMethod("setO", argValue.getClass());
method.invoke(new ReflectTest(), argValue);
method.invoke(new ReflectTest(), Integer.valueOf(3));
}
这里的输出是:
false
set Integer
false
set Integer
如您所见,没有原语,只有带有Integer. class
的方法被找到和调用。如果删除它,您将获得NoSuchControlodException
。
因此,为了解决您的问题,请更改您尝试通过反射调用的方法以采用包装类型,或者更好的是,传递正确的参数类型,而不是从某些值派生它们。
最后,当方法不可访问时也会抛出NoSuchControlodException
,即不是public
,请确保方法是公共的。
根据这个问题,您应该使用Integer. TYPE
来引用原始int
以进行反射。