我有这样的代码
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是一个对象。
我得到的对象不是声明类的实例。??
答案 0 :(得分:4)
您正尝试在方法名称(即字符串)上调用方法。您应该在对XYZClass
:
XYZClass xyz = ...;
final List<Integer> output = (List<Integer>) method.invoke(xyz,
new Object[] {inputRecords, foo});
如果是静态方法,请使用null
作为第一个参数。