Here you can find the source of invokePrivateMethod(Object obj, String methodName, Class>[] parameterTypes, Object[] args)
Parameter | Description |
---|---|
obj | the object the underlying method is invoked from |
methodName | the name of the method |
parameterTypes | the parameters that define the method |
args | The arguments to the actual method |
Parameter | Description |
---|---|
Exception | an exception |
obj
with parameters
public static Object invokePrivateMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args) throws Exception
//package com.java2s; /**/*from www . ja v a2s . c o m*/ * This program and the accompanying materials * are made available under the terms of the License * which accompanies this distribution in the file LICENSE.txt */ import java.lang.reflect.Method; public class Main { /** * Invoke a private Java method by reflection * * @param obj the object the underlying method is invoked from * @param methodName the name of the method * @param parameterTypes the parameters that define the method * @param args The arguments to the actual method * @return the result of dispatching the method represented by this object on <code>obj</code> with parameters * @throws Exception */ public static Object invokePrivateMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args) throws Exception { Method m = obj.getClass().getDeclaredMethod(methodName, parameterTypes); m.setAccessible(true); return m.invoke(obj, args); } }