简体   繁体   中英

How to invoke private method via Reflection when parameter of method is List?

How can I invoke private method via Reflection API?

My code

public class A {
    private String method(List<Integer> params){
        return "abc";
    }
}

And test

public class B {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<A> clazz = A.class;
        Method met = clazz.getMethod("method", List.class);
        met.setAccessible(true);
        String res = (String) met.invoke("method", new ArrayList<Integer>());
        System.out.println(res);
    }
}

There are two problems in your code

  • you are using getMethod which can only return public methods, to get private ones use getDeclaredMethod on type which declares it.
  • you are invoking your method on "method" String literal instead of instance of A class (String doesn't have this method, so you can't invoke it on its instance. For now your code is equivalent to something like "method".method(yourList) which is not correct).

Your code should look like

Class<A> clazz = A.class;
Method met = clazz.getDeclaredMethod("method", List.class);
//                    ^^^^^^^^
met.setAccessible(true);
String res = (String) met.invoke(new A(), new ArrayList<Integer>());
//                               ^^^^^^^ 

//OR pass already existing instance of A class
//A myA = new A(); //other instance of A on which you want to call the method
//...
//String res = (String) met.invoke(myA, new ArrayList<Integer>());

System.out.println(res);

You can do it in this way if you need to create an Instance of the A by reflection too

 public static void main(String[] args) throws Exception {
    Class<?> aClass = A.class;
    Constructor<?> constructor = aClass.getConstructors()[0];
    Object a = constructor.newInstance(); // create instance of a by reflection
    Method method = a.getClass().getDeclaredMethod("method", List.class); // getDeclaredMethod for private
    method.setAccessible(true); // to enable accessing private method
    String result = (String) method.invoke(a, new ArrayList<Integer>());
    System.out.println(result);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM