我有这样Java反射不正常
final Method method = XYZClass.class.getDeclaredMethod(METHOD_NAME, new Class[] {List.class, List.class});
method.setAccessible(true);
final List<Integer> output = (List<Integer>) method.invoke(METHOD_NAME, new Object[] {inputRecords, foo});
inputRecords代码为对象的列表和foo是一个对象。
我得到的对象不是一个声明类的实例。
如果foo是一个对象(即不是List的一个实例),方法调用(一旦用Jon Skeet的建议修复)将引发异常。 –
反射*是*正常工作,您错误地使用它,您的method.invoke()语句具有错误的第一个参数。 –