Here you can find the source of invokePrivateMethod(Object instance, String name, Object... args)
public static final <T> T invokePrivateMethod(Object instance, String name, Object... args)
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static final <T> T invokePrivateMethod(Object instance, String name, Object... args) { Class<?>[] clazArray = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { clazArray[i] = args[i].getClass(); }// w w w . ja v a 2 s. c o m try { Method method = instance.getClass().getDeclaredMethod(name, clazArray); method.setAccessible(true); T result = (T) method.invoke(instance, args); return result; } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } public static final <T> T invokePrivateMethod(Object instance, String name) { try { Method method = instance.getClass().getDeclaredMethod(name); method.setAccessible(true); T result = (T) method.invoke(instance); return result; } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } }