Java Method Call invokeRestrictedMethod(Object obj, Class theClass, String methodName)

Here you can find the source of invokeRestrictedMethod(Object obj, Class theClass, String methodName)

Description

Call a private method without arguments.

License

BSD License

Declaration

public static Object invokeRestrictedMethod(Object obj, Class theClass,
        String methodName) throws NoSuchMethodException,
        IllegalAccessException, IllegalArgumentException,
        InvocationTargetException 

Method Source Code

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

Related

  1. invokeQuietly(MethodHandle handle, Object instance)
  2. invokeQuietly(Object target, Method method, Object[] params)
  3. invokeReadResolveOn(Object object, Class clz)
  4. invokeReal(Object real, Method m, Object[] args)
  5. invokeRemoteMBeanOperation(String remoteURL, String jmxName, Class klass, Function function)
  6. invokeServiceClass(JsonReader jsonReader, Object service, Method operation, Class[] paramClasses, int paramCount)
  7. invokeSet(Object o, String fieldName, Object value)
  8. invokeSetFieldValue(Object bean, Field field, Object value)
  9. invokeSilent(Object obj, String methodName, Class[] parameterTypes, Object[] args)