This code allow me invoking a method with tests parameter
Method m = aClass.getDeclaredMethod(methodName, paramTypes);
Integer n =10;
Object retobj =m.invoke(o, "test",n);
System.out.println(retobj);
Now I want to invoke the method with the arguments list.
List<Object> arguments =container.getArgs();
Object retobj =m.invoke(o, (Object) arguments);
But I get this error
java.lang.IllegalArgumentException: wrong number of arguments
The invoke
method takes two parameters: the object on which to invoke the method, and a "varargs" parameter to hold the arguments.
Such a parameter (also "variable arity") is interchangeable with an array, and it's treated as an array in the body of such a method.
Convert your List<Object>
to an array, then pass it in.
Object retobj = m.invoke(o, arguments.toArray());
在第一次invoke
,您传递了2个参数,在第二次invoke
,您仅传递了1个。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.