Reputation: 109
I have used all the suggestions I could find on StackOverflow and other sites for this. I am trying to invoke a method using reflection. Here is the code for my method:
public void my_method(String[] args) {
for(int i=0; i<args.length; i++)
{
System.out.println(args);
}
}
Here is the code I used for reflection
Class[] paramStringArray = new Class[1];
paramStringArray[0] = String[].class;
String[] argu = {"hey", "there"};
Method method = cls.getDeclaredMethod("my_method", paramStringArray);
method.invoke(obj, new Object[]{argu});
My issue is that when I run the program, I see the output printed as: [Ljava.lang.String;@70a6aa31 [Ljava.lang.String;@70a6aa31
I have tried all the suggestions I could find. Can someone please help me with this?
Thanks!
Upvotes: 0
Views: 1812
Reputation: 236124
The method my_method()
receives as a parameter a String[]
, not a String
. You're calling a different method. The code should look like this:
paramString[0] = String[].class;
Method method = cls.getDeclaredMethod("my_method", paramString);
To invoke it, pass a String[]
as parameter:
method.invoke(obj, new String[]{"x"});
Also, the body of the loop in my_method()
should refer to each element's position, not to the array itself:
System.out.println(args[i]);
Upvotes: 1
Reputation: 12985
You need to print args[i]
and not what you have.
Also the method should be called with a new String[]
instead of new Object[]
.
Upvotes: 1