I'm just starting to learn reflection and to do so I have a little Math app. The program wraps a logic class, and at runtime looks through the logic class it wraps for methods (in this case, add, subtract, multiply, and divide). The methods look like the one below.
public int add(int... args){
int result = 0;
for(int i=0;i<args.length;i++){
result+=args[i];
}
return result;
}
As you can see, this method (all the other ones do as well) takes in int... args, so I can pass in any number of ints to add together.
I then attempt to solve the Math question like so
int[] params = new int[numinputs];
//populate params with what numbers the user types in.
int result = (Integer) methodToCall.invoke(logicInstance, params);
If i call it this way, I get an IllegalArguementException on invoke().
So I guess the tl:dr is :
How do I call invoke() if the method being invoked takes in a params instead of a hardcoded number of parameters.
Thanks :)