I have the following code:
public void myMethod(Object... args) {
System.out.println(\"this is myMethod\");
}
public void invokeMyMethod() {
Method
The problem here is with the variable arguments (the Object...
) that Method.invoke
takes.
Changing this line
s.invoke(this, ex);
to this
s.invoke(this, (Object)ex);
will work.
In the background, Object...
is passed as an Object[]
. Java's seeing your Object[]
and choosing not to wrap it up in another Object[]
. By casting to Object
, it now just sees that and reverts to its normal wrap-it-up behaviour - the same as what other answers are doing manually.