Consider the following complete Java program.
public class Reflection
{
static class A
{
public void useArray( String[] args )
{
System.out.println( "Args are " + Arrays.toString( args ) );
}
}
public static void main( String[] args ) throws Exception
{
Method m = A.class.getMethod( "useArray", String[].class );
m.invoke( new A(), new String[] { "Hi" } );
}
}
Trying to run this, you get an error:
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
If instead of using only one String in the array passed to invoke you pass more than one, then you get:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
So, clearly, because invoke's signature is:
invoke(java.lang.Object obj, java.lang.Object... args)
when I call invoke using an array, the call is getting "translated" to:
m.invoke( new A(), "Hi" );
How to make Java understand that I do want my array to be passed into invoke intact??