getMethod 抛出方法未找到异常

getMethod throws method not found exception

我正在使用 getMethod(String name, Class[] types) 方法来获取一个方法,但是当有一个 int 参数时我得到了一个找不到的方法。我想我明白了,因为在我的 Class 数组中我有 java.lang.Integer class (包装器)而不是 int。我通过使用通用 Object.getClass() 得到 class,所以我认为我不能轻易改变它。这是执行此操作的代码部分:

for (int i = 0; i < parameterTypes.length; i++) {
        parameterTypes[i] = arguments[i].getClass();
}

try {
    Method mmethod = mclass.getMethod(contractName, parameterTypes);
} catch (NoSuchMethodException e) {}

我能以某种方式解决这个问题吗?

根据 this question,您应该使用 Integer.TYPE 来引用基元 int 以进行反射。

假设你有这个 class

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 中只有其中一个,并依靠自动装箱通过 [=18= 获取方法对象] 并传递一个或另一个参数类型。

@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 的方法。如果删除它,您将得到 NoSuchMethodException.

因此,为了解决您的问题,请更改您尝试通过反射调用的方法以采用包装类型或更好的方法,传递正确的参数类型而不是从某些值派生它们。

最后,当方法不可访问时也会抛出 NoSuchMethodException,即不是 public,确保方法是 public.