0

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

user567
  • 3,712
  • 9
  • 47
  • 80

2 Answers2

1

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());
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

In the first invoke call, you are passing 2 arguments, in the second invoke, you are only passing 1.

Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37