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

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

Description

Invokes accessible method of an object.

License

Open Source License

Parameter

Parameter Description
c class that contains method
obj object to execute
method method to invoke
paramClasses classes of parameters
params parameters

Declaration

public static Object invoke(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 {
    /**/*from  w  w  w  .ja v  a  2s  . com*/
     * 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. invoke(@Nonnull Object source, @Nonnull String name, Object... args)
  2. invoke(boolean makeAccessible, T object, Class methodClass, String methodName, Class[] paramTypes, Object... params)
  3. invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
  4. invoke(Class annotationClass, Object instance, Object[] parameters)
  5. invoke(Class clazz, Object obj, String methodName, Object... args)
  6. invoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)