Here you can find the source of invokeRestrictedMethod(Object obj, Class theClass, String methodName)
public static Object invokeRestrictedMethod(Object obj, Class theClass, String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
//package com.java2s; // LICENSE: This file is distributed under the BSD license. import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /**// w w w .j av a 2s . c o m * Call a private method without arguments. */ public static Object invokeRestrictedMethod(Object obj, Class theClass, String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { return invokeRestrictedMethod(obj, theClass, methodName, (Object) null); } /** * Call a private method using reflection. Use looks like * invokeRestrictedMethod(Object obj, Class theClass, String methodName, Object param1, Class paramType1, Object param2, Class paramType2, ...) */ public static Object invokeRestrictedMethod(Object obj, Class theClass, String methodName, Object... paramsAndTypes) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Object[] params; Class[] paramTypes; int l; if (paramsAndTypes != null) { l = paramsAndTypes.length; params = new Object[l / 2]; paramTypes = new Class[l / 2]; } else { l = 0; params = null; paramTypes = null; } for (int i = 0; i < l / 2; ++i) { params[i] = paramsAndTypes[i * 2]; paramTypes[i] = (Class) paramsAndTypes[i * 2 + 1]; } return invokeRestrictedMethod(obj, theClass, methodName, params, paramTypes); } public static Object invokeRestrictedMethod(Object obj, Class<?> theClass, String methodName, Object[] params, Class[] paramTypes) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method method = theClass.getDeclaredMethod(methodName, paramTypes); Object result; boolean wasAccessible = method.isAccessible(); method.setAccessible(true); if (params == null) { result = method.invoke(obj); } else { result = method.invoke(obj, params); } method.setAccessible(wasAccessible); return result; } }