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

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

Description

invokex

License

Open Source License

Declaration

public static Object invoke_ex(Object obj, String method, Class<?>[] params, Object[] args)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Method Source Code

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

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

public class Main {
    public static Object invoke_ex(Object obj, String method, Class<?>[] params, Object[] args)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Class<?> clazz = null;
        if (obj instanceof Class<?>) {
            clazz = (Class<?>) obj;
        } else {//from   ww w  . ja v  a2s .  c om
            clazz = obj.getClass();
        }
        Method methods = clazz.getDeclaredMethod(method, params);
        methods.setAccessible(true);
        if (obj instanceof Class<?>) {
            return methods.invoke(null, args);
        } else {
            return methods.invoke(obj, args);
        }
    }

    /**
     * <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 {
            return invoke_ex(obj, method, params, args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. invoke(String method)
  2. invoke(String methodName, Object obj)
  3. invoke(String methodName, Object object, Class[] argTypes, Object[] args)
  4. invoke(String methodName, Object target, Class targetClass, Object[] args)
  5. invoke(String name, Object object, Object... args)
  6. invokeAccessableMethodWithArguments(Object instance, String method, Object... arguments)
  7. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)
  8. invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)
  9. invokeAnUnwrapException(final Method method, final Object[] args, final Object target)