我试图运行下面的代码:抛出:IllegalArgumentException:错误的参数数目
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Reflection {
/**
* @param args
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException,
InvocationTargetException, IllegalArgumentException {
Class<Cls> cls = Cls.class;
Method[] methods = cls.getMethods();
for (Method m : methods) {
m.invoke(cls);
}
}
}
class Cls {
public static void method1() {
System.out.println("Method1");
}
public static void method2() {
System.out.println("Method2");
}
}
我不断收到一个IllegalArgumentException:错误的参数数目,即使这两个方法都没有参数。
我试着将null
传递给invoke
方法,但是会抛出一个NPE。
谢谢'getDeclaredMethods()'定了! –