Java Method Call invokePrivateMethod(Object obj, String methodName, Class[] parameterTypes, Object[] args)

Here you can find the source of invokePrivateMethod(Object obj, String methodName, Class[] parameterTypes, Object[] args)

Description

Invoke a private Java method by reflection

License

Open Source License

Parameter

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

Exception

Parameter Description
Exception an exception

Return

the result of dispatching the method represented by this object on obj with parameters

Declaration

public static Object invokePrivateMethod(Object obj, String methodName, Class<?>[] parameterTypes,
        Object[] args) throws Exception 

Method Source Code

//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);
    }
}

Related

  1. invokePrimivite(Object entity, Method writeMethod, Class propertyType, String propertyValue)
  2. invokePrivate(Class clazz, Object instance, String methodName, Class[] argTypes, Object[] args)
  3. invokePrivate(String name, final Object obj, Object[] objects)
  4. invokePrivateMethod(Method method, Object object, Object... args)
  5. invokePrivateMethod(Object instance, String name, Object... args)
  6. invokePrivateMethod(String methodName, Class clazz, Object object)
  7. invokeProperty(Object obj, String property)
  8. invokeProtectedMethod(Class c, String method, Object... args)
  9. invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)