I have a Java method:
public void setPri(List<Integer> pri) { this.pri = pri; }
What I want to do is call the method and pass a List. What is the correct approach using reflection?
I was trying the following:
method = object.getClass().getDeclaredMethod("setPri", List.class);
method.invoke(null,new Object[] { pri });
Your code won't primarily work, because you are not passing an instancs as first parameter:
method.invoke(null,new Object[] { pri });
You need to pass object as first parameter, not null.
This should work:
List<Integer> pri = Arrays.asList(1,2,3,4);
Method method = object.getClass().getDeclaredMethod("setPri", List.class);
method.invoke(object, new Object[] { pri });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With