Java Method Call invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)

Here you can find the source of invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)

Description

Convenience wrapper to reflection method invoke API.

License

Open Source License

Parameter

Parameter Description
target object to invoke the method on (or null for static methods)
clazz class name
methodName method name
parameterTypes parameter types to resolve method name
args actual arguments

Exception

Parameter Description
IllegalArgumentException if method not found
IllegalStateException for InvocationTargetException (exception in invoked method)

Return

invocation result or null

Declaration

public static Object invoke(Object target, Class<?> clazz, String methodName, Class[] parameterTypes,
        Object... args) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Main {
    /**/* w w  w.  j  a  va  2s.c  o m*/
     * Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.
     *
     * @param target         object to invoke the method on (or null for static methods)
     * @param clazz          class name
     * @param methodName     method name
     * @param parameterTypes parameter types to resolve method name
     * @param args           actual arguments
     * @return invocation result or null
     * @throws IllegalArgumentException if method not found
     * @throws IllegalStateException    for InvocationTargetException (exception in invoked method)
     */
    public static Object invoke(Object target, Class<?> clazz, String methodName, Class[] parameterTypes,
            Object... args) {
        try {
            Method method = null;
            try {
                method = clazz.getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                method = clazz.getDeclaredMethod(methodName, parameterTypes);
            }
            method.setAccessible(true);

            return method.invoke(target, args);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Illegal arguments method %s.%s(%s) on %s, params %s",
                    clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
                    e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException(String.format("Error invoking method %s.%s(%s) on %s, params %s",
                    clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
                    e);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(String.format("No such method %s.%s(%s) on %s, params %s",
                    clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
                    e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(String.format("No such method %s.%s(%s) on %s, params %s",
                    clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
                    e);
        }
    }
}

Related

  1. invoke(Object object, String methodName, Object[] args)
  2. invoke(Object objToInvoke, Class classToInvoke, String method, Class[] argumentClasses, Object[] arguments)
  3. invoke(Object owner, String methodName)
  4. invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
  5. invoke(Object source, String key)
  6. invoke(Object target, Class clazz, String method)
  7. invoke(Object target, Method method)
  8. invoke(Object target, String methodName)
  9. invoke(Object target, String methodName, Object... args)