I have class AClass
and a method someMethod
that gets an Object
array as parameter.
public class AClass {
public void someMethod(Object[] parameters) {
}
}
In main, when I try to invoke this method on the the object that I have created and give an object array as a parameter to this method
Object[] parameters; // lets say this object array is null
Class class = Class.forName("AClass");
Object anObject = class.newInstance();
Method someMethod = class.getDeclaredMethod("someMethod", parameters.getClass());
someMethod.invoke(anObject, parameters);
I get "wrong number of arguments error". What am i missing?
That will be all right.
Be careful about the second parameter of the invoke method. It's Object[] itself, and the argument type of your method is Object[] too.
The parameters to
invoke
is an array ofObject
; your parameters should be anObject[]
containing theObject[]
you're passing tosomeMethod
.You don't need to create an immediate array to do this, since the
invoke
signature isinvoke(Object, Object...)
, but in your case you're trying to pass in an empty array. If you want to pass in null:Ultimately, however, the other answers are correct: you need to pass in an
Object[]
containing an entry for each of the methods parameters.To expand a bit on what orien and biaobiaoqi are saying . . .
What's probably confusing you here is that
Method.invoke(Object, Object...)
can usually just take the arguments "inline", so to speak; when the compiler sees something likesomeMethod.invoke(someObject, arg1, arg2)
, it implicitly creates an arraynew Object[]{arg1, arg2}
and then passes that array toMethod.invoke
.Method.invoke
then passes the elements of that array as arguments to the method you're invoking. So far, so good.But when the compiler sees something like
someMethod.invoke(someObject, someArray)
, it assumes that you've already packaged the arguments into an array; so it won't repackage them again. So thenMethod.invoke
will try to pass the elements ofsomeArray
as arguments to the method you're invoking, rather than passingsomeArray
itself as an argument.(This is always how the
...
notation works; it accepts either an array containing elements of the appropriate type, or zero or more arguments of the appropriate type.)So, as orien and biaobiaoqi have said, you need to rewrap your
parameters
into an additional array,new Object[] {parameters}
, so thatparameters
itself ends up being passed into your method.Does that make sense?
try this:
I got by automatically-generating a Reflection API version of your code with dp4j:
The Method.invoke method takes the object to receive the method call and an array of the arguments to the method. As your method takes one argument, the array given must have a size of one.
try creating a new array with size 1:
Note that the one value in this array can be null. This would simulate
anObject.someMethod(null)