mardi 8 août 2017

How do I use reflection to invoke a private method with array of String as parameter [duplicate]

This question already has an answer here:

I try to call a private method with the following code. but I get an IllegalArgumentException.

java.lang.IllegalArgumentException: wrong number of arguments

The code is :

Method method = MyClass.class.getDeclaredMethod("myMethod", String[].class);
method.setAccessible(true);
method.invoke(myClassInstance, new String[] { "a", "b" });

The signature of the method being called is :

private void myMethod(String[] args)

I tried the same example but with a method taking a String as parameter and it worked fine.

Many thanks





Aucun commentaire:

Enregistrer un commentaire