문제

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 });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top