Java Method Call invoke(Object instance, Method method, Object... params)

Here you can find the source of invoke(Object instance, Method method, Object... params)

Description

Invokes the given object Method .

License

Open Source License

Declaration

public static Object invoke(Object instance, Method method, Object... params)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*//w  ww. jav a  2s . com
 *  This file is part of Cotopaxi.
 *
 *  Cotopaxi is free software: you can redistribute it and/or modify
 *  it under the terms of the Lesser GNU General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  Cotopaxi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  Lesser GNU General Public License for more details.
 *
 *  You should have received a copy of the Lesser GNU General Public License
 *  along with Cotopaxi. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    private static final Class<?>[] EMPTY_CLASS_PARAMS = new Class[0];

    /**
     * Invokes the given object {@link Method}.
     */
    public static Object invoke(Object instance, Method method, Object... params)
            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        return method.invoke(instance, params);
    }

    public static Object invoke(Object facade, String method, Object... params) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Method m = getMethod(facade.getClass(), method, params);
        return invoke(facade, m, params);
    }

    public static Method getMethod(Class<?> klass, String methodName, Object... params)
            throws SecurityException, NoSuchMethodException {
        Class<?>[] classes = (params.length != 0) ? new Class<?>[params.length] : EMPTY_CLASS_PARAMS;

        for (int i = 0; i < params.length; i++) {
            classes[i] = params[i].getClass();
        }
        return klass.getMethod(methodName, classes);
    }

    /**
     * Gets an {@link Class} by name.
     */
    public static Class<?> getClass(String className) throws ClassNotFoundException {
        return Class.forName(className);
    }
}

Related

  1. invoke(Object buffer, String methodName, Class[] paramTypes, Object... args)
  2. invoke(Object clazzInstance, String method, Class[] paramClasses, Object[] params)
  3. invoke(Object context, String methodName, Object parameter)
  4. invoke(Object host, String method, Object[] args)
  5. invoke(Object instance, java.lang.reflect.Method method, Object... args)
  6. invoke(Object instance, String method, Class[] paramTypes, Object... parameters)
  7. invoke(Object invoker, String cmd, Map params)
  8. invoke(Object methodHostInstance, String methodName, Class[] parameterTypes, Object[] args)
  9. invoke(Object o, Method m)