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

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

Description

Invokes method of a given obj and set of parameters

License

Open Source License

Parameter

Parameter Description
obj - Object for which the method is going to be executed. Special if obj is of type java.lang.Class, method is executed as static method of class given as obj parameter
method - name of the object
params - actual parameters for the method

Return

- result of the method execution

If there is any problem with method invocation, a RuntimeException is thrown

Declaration

public static Object invoke(Object obj, String method, Class[] params, Object[] args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    /**/*from www  .  j a  v a  2  s  .co m*/
     * <p>Invokes method of a given obj and set of parameters</p>
     * 
     * @param obj - Object for which the method is going to be executed. Special
     *          if obj is of type java.lang.Class, method is executed as static method
     *          of class given as obj parameter
     * @param method - name of the object
     * @param params - actual parameters for the method
     * @return - result of the method execution
     * 
     * <p>If there is any problem with method invocation, a RuntimeException is thrown</p>
     */
    public static Object invoke(Object obj, String method, Class[] params, Object[] args) {
        try {
            Class<?> clazz = null;
            if (obj instanceof Class) {
                clazz = (Class) obj;
            } else {
                clazz = obj.getClass();
            }
            Method methods = clazz.getMethod(method, params);
            if (obj instanceof Class) {
                return methods.invoke(null, args);
            } else {
                return methods.invoke(obj, args);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Gets method from class <code>clz</code> or any of its superclasses. If no method can be found 
     * <code>NoSuchMethodException</code> is raised.
     * 
     * @param clz - declaring class of the method
     * @param methodName - name of the method
     * @param methodArgs - method arguments
     * @return requested method
     * @throws NoSuchMethodException
     */
    public static Method getMethod(Class<? extends Object> clz, String methodName, Class[] methodArgs)
            throws NoSuchMethodException {

        if (clz == null)
            throw new NoSuchMethodException(methodName + "(" + methodArgs + ") method does not exist ");

        try {

            return clz.getDeclaredMethod(methodName, methodArgs);

        } catch (NoSuchMethodException e) {

            return getMethod(clz.getSuperclass(), methodName, methodArgs);

        }
    }
}

Related

  1. invoke(Object o, String name, Object[] args)
  2. invoke(Object o, String name, Object[] args)
  3. invoke(Object obj, Method method, Object[] args)
  4. invoke(Object obj, Method method, Object[] params)
  5. invoke(Object obj, String method, Class[] params, Object[] args)
  6. invoke(Object obj, String method, Object param, Object paramType)
  7. invoke(Object obj, String methodName)
  8. invoke(Object obj, String methodName, boolean newValue)
  9. invoke(Object obj, String methodName, Class argType, Object arg)