Java Method Call invoke(String methodName, Object target, Class targetClass, Object[] args)

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

Description

Invoke the given method.

License

Open Source License

Parameter

Parameter Description
methodName The method name
target The target Object
targetClass The Class of the target object
args An array of argument Objects

Exception

Parameter Description
IllegalAccessException an exception
IllegalArgumentException an exception
InvocationTargetException an exception
NoSuchMethodException an exception
SecurityException an exception

Return

The return value

Declaration


public static Object invoke(String methodName, Object target, Class targetClass, Object[] args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
        NoSuchMethodException, SecurityException 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class Main {
    private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];

    /** Invoke the given method.  This method is a convenience method which 
    creates an array of arguments with the single arg value.  This is 
    necessary because the Method.invoke() method only accepts an array of 
    argument objects.//from   w ww  . j  a  v  a 2s. co  m
        
    @param method The method
    @param target The target Object
    @param arg A single argument
    @return The return object or null
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    */

    public static Object invoke(Method method, Object target, Object arg)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        if (arg == null) {
            return invoke(method, target, EMPTY_OBJECT_ARRAY);
        } else {
            Object[] args = new Object[1];
            args[0] = arg;
            return invoke(method, target, args);
        }
    }

    /** Invoke the given method.  This method actually invokes the target
    method with the given arguments.
        
    @param method The Method
    @param target The target Object
    @param args An array of argument Objects
    @return The return value
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    */

    public static Object invoke(Method method, Object target, Object[] args)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        return method.invoke(target, args);
    }

    /** Invoke the given method.  The target object is used to determine
    the target class and which is then used to get a Method object.
    This method is a convenience method which creates an array of arguments 
    with the single arg value.  This is necessary because the 
    Method.invoke() method only accepts an array of argument objects.
        
    @param methodName The method name
    @param target The target Object
    @param arg A single argument
    @return The return object or null
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    @throws NoSuchMethodException
    @throws SecurityException
    */

    public static Object invoke(String methodName, Object target, Object arg) throws IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        if (arg == null) {
            return invoke(methodName, target, EMPTY_OBJECT_ARRAY);
        } else {
            Object[] args = new Object[1];
            args[0] = arg;
            return invoke(methodName, target, args);
        }
    }

    /** Invoke the given method.  The target object is used to determine
    the target class and which is then used to get a Method object.
        
    @param methodName The Method name
    @param target The target Object
    @param args An array of argument Objects
    @return The return value
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    @throws NoSuchMethodException
    @throws SecurityException
    */

    public static Object invoke(String methodName, Object target, Object[] args) throws IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        return invoke(methodName, target, target.getClass(), args);
    }

    /** Invoke the given method.  The target object may be null in which
    case the target class is used and the method must be a static
    method.
        
    @param methodName The method name
    @param target The target Object
    @param targetClass The Class of the target object
    @param args An array of argument Objects
    @return The return value
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    @throws NoSuchMethodException
    @throws SecurityException
    */

    public static Object invoke(String methodName, Object target, Class targetClass, Object[] args)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
            NoSuchMethodException, SecurityException {
        Class[] paramClasses = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            paramClasses[i] = args[i].getClass();
        }
        Method method = getMethod(targetClass, methodName, paramClasses);
        return invoke(method, target, args);
    }

    /** Invoke the given method.  This method is a convenience method for
    invoking single argument methods.
        
    @param methodName The method name
    @param target The target Object
    @param targetClass The Class of the target object
    @param arg A single argument
    @return The return value
    @throws IllegalAccessException
    @throws IllegalArgumentException
    @throws InvocationTargetException
    @throws NoSuchMethodException
    @throws SecurityException
    */

    public static Object invoke(String methodName, Object target, Class targetClass, Object arg)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
            NoSuchMethodException, SecurityException {
        if (arg == null) {
            return invoke(methodName, target, targetClass, EMPTY_OBJECT_ARRAY);
        } else {
            Object[] args = new Object[1];
            args[0] = arg;
            return invoke(methodName, target, targetClass, args);
        }
    }

    /** Get the method with the given name and with a single argument of the
    type specified by paramClass.  The paramClass may be null in which case
    the method should have no arguments.
        
    @param targetClass The target class
    @param name The method name
    @param paramClass The single parameter class (may be null)
    @return The Method
    @throws SecurityException
    @throws NoSuchMethodException
    */

    public static Method getMethod(Class targetClass, String name, Class paramClass)
            throws NoSuchMethodException, SecurityException {
        if (paramClass == null) {
            return getMethod(targetClass, name, EMPTY_CLASS_ARRAY);
        } else {
            Class[] paramClasses = new Class[1];
            paramClasses[0] = paramClass;
            return getMethod(targetClass, name, paramClasses);
        }
    }

    /** Get the method with the given name and with arguments of the
    types specified by paramClasses.  If the method is not found
    then a method which accepts superclasses of the current arguments
    will be returned, if possible.
        
    @param targetClass The target class
    @param name The method name
    @param paramClasses An array of parameter classes
    @return The Method
    @throws SecurityException
    @throws NoSuchMethodException
    */

    public static Method getMethod(Class targetClass, String name, Class[] paramClasses)
            throws NoSuchMethodException, SecurityException {
        Method method = null;
        try {
            method = targetClass.getMethod(name, paramClasses);
        } catch (NoSuchMethodException nsme) {
            Method[] methods = targetClass.getMethods();

            OUTER: for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().equalsIgnoreCase(name)
                        && methods[i].getParameterTypes().length == paramClasses.length) {
                    Class[] params = methods[i].getParameterTypes();
                    for (int j = 0; j < params.length; j++) {
                        if (!params[j].isAssignableFrom(paramClasses[j])) {
                            continue OUTER;
                        }
                    }
                    method = methods[i];
                    break;
                }
            }
            if (method == null) {
                throw nsme;
            }
        }
        return method;
    }
}

Related

  1. invoke(String className, String methodName, Class[] paramTypes, Object... params)
  2. invoke(String cls_name, String method, Class[] param_cls, Object cls, Object[] params)
  3. invoke(String method)
  4. invoke(String methodName, Object obj)
  5. invoke(String methodName, Object object, Class[] argTypes, Object[] args)
  6. invoke(String name, Object object, Object... args)
  7. invoke_ex(Object obj, String method, Class[] params, Object[] args)
  8. invokeAccessableMethodWithArguments(Object instance, String method, Object... arguments)
  9. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)