Java Method Call invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)

Here you can find the source of invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)

Description

Invokes any method of a class, even private ones.

License

Open Source License

Parameter

Parameter Description
c class to examine
obj object to inspect
method method to invoke
paramClasses parameter types
params parameters

Declaration

public static Object invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException 

Method Source Code

//package com.java2s;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Main {
    /**/* w  w w . j av  a 2 s  . com*/
     * Invokes any method of a class, even private ones.
     *
     * @param c            class to examine
     * @param obj          object to inspect
     * @param method       method to invoke
     * @param paramClasses   parameter types
     * @param params       parameters
     */
    public static Object invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getDeclaredMethod(method, paramClasses);
        m.setAccessible(true);
        return m.invoke(obj, params);
    }

    public static Object invokeDeclared(Class c, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getDeclaredMethod(method, paramClasses);
        m.setAccessible(true);
        return m.invoke(null, params);
    }

    /**
     * Invokes any method of a class suppressing java access checking.
     *
     * @param obj          object to inspect
     * @param method       method to invoke
     * @param paramClasses   parameter types
     * @param params       parameters
     */
    public static Object invokeDeclared(Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = obj.getClass().getDeclaredMethod(method, paramClasses);
        m.setAccessible(true);
        return m.invoke(obj, params);
    }

    public static Object invokeDeclared(Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invokeDeclared(obj, method, paramClass, params);
    }

    public static Object invokeDeclared(Class c, Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invokeDeclared(c, obj, method, paramClass, params);
    }

    public static Object invokeDeclared(Class c, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invokeDeclared(c, null, method, paramClass, params);
    }

    /**
     * Invokes accessible method of an object.
     *
     * @param c            class that contains method
     * @param obj          object to execute
     * @param method       method to invoke
     * @param paramClasses   classes of parameters
     * @param params       parameters
     */
    public static Object invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getMethod(method, paramClasses);
        return m.invoke(obj, params);
    }

    /**
     * Invokes static method.
     */
    public static Object invoke(Class c, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getMethod(method, paramClasses);
        return m.invoke(null, params);
    }

    /**
     * Invokes accessible method of an object.
     *
     * @param obj          object
     * @param method       name of the objects method
     * @param params       method parameters
     * @param paramClasses   method parameter types
     */
    public static Object invoke(Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = obj.getClass().getMethod(method, paramClasses);
        return m.invoke(obj, params);
    }

    /**
     * Invokes accessible method of an object without specifying parameter types.
     * @param obj    object
     * @param method method of an object
     * @param params method parameters
     */
    public static Object invoke(Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(obj, method, paramClass, params);
    }

    public static Object invoke(Class c, Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(c, obj, method, paramClass, params);
    }

    /**
     * Invokes static method.
     */
    public static Object invoke(Class c, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(c, null, method, paramClass, params);
    }

    /**
     * Returns classes from array of specified objects.
     */
    public static Class[] getClasses(Object... objects) {
        if (objects == null) {
            return null;
        }
        Class[] result = new Class[objects.length];
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] != null) {
                result[i] = objects[i].getClass();
            }
        }
        return result;
    }
}

Related

  1. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)
  2. invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)
  3. invokeAnUnwrapException(final Method method, final Object[] args, final Object target)
  4. invokeCallback(String callbackName, String callbackParam)
  5. invokeCmd(String className, String[] args)
  6. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  7. invokeDeclaredMethod(Object object, String methodName, Object... parameters)
  8. invokeDeclaredMethod(String methodName, Object target, Class[] parameterTypes, Object[] parameters)
  9. invokeDeclaredMethodWithAnnotation(Class annotationClass, Object target)