Java Method Call invoke(final Object instance, final String method, final Object... parameters)

Here you can find the source of invoke(final Object instance, final String method, final Object... parameters)

Description

Execute a method by reflection.

License

Open Source License

Parameter

Parameter Description
instance to inspect
method the name of the method to be invoked
parameters the instances used to fill the parameters

Exception

Parameter Description
Throwable in case anything goes wrong

Return

the result of the method invoked

Declaration

public static Object invoke(final Object instance, final String method, final Object... parameters)
        throws Throwable 

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  ww.j  a v a 2s . c  om*/
     * Execute a method by reflection.
     *
     * @param instance
     *            to inspect
     * @param method
     *            the name of the method to be invoked
     * @param parameters
     *            the instances used to fill the parameters
     * @return the result of the method invoked
     * @throws Throwable
     *             in case anything goes wrong
     */
    public static Object invoke(final Object instance, final String method, final Object... parameters)
            throws Throwable {

        final Class<?> clazz = instance.getClass();

        for (final Method m : clazz.getMethods()) {

            if (m.getName().equals(method)) {

                try {
                    return m.invoke(instance, parameters);
                } catch (final IllegalArgumentException | IllegalAccessException e) {
                    throw new RuntimeException("Can't invoke method", e);
                } catch (final InvocationTargetException e) {
                    throw e.getTargetException();
                }
            }
        }
        throw new RuntimeException("Can't find method");
    }

    /**
     * Execute a method by reflection.
     *
     * @param instance
     *            to inspect
     * @param method
     *            the name of the method to be invoked
     * @param paramTypes
     *            the class types used as parameter
     * @param parameters
     *            the instances used to fill the parameters
     * @return the result of the method invoked
     */
    public static Object invoke(final Object instance, final String method, final Class<?>[] paramTypes,
            final Object... parameters) {

        final Class<?> clazz = instance.getClass();

        for (final Method m : clazz.getMethods()) {

            if (m.getName().equals(method) && Arrays.equals(m.getParameterTypes(), paramTypes)) {

                try {
                    return m.invoke(instance, parameters);
                } catch (final Exception e) {
                    throw new RuntimeException("Can't invoke method", e);
                }
            }
        }
        throw new RuntimeException("Can't find method");
    }
}

Related

  1. invoke(Class returnType, Method m, Object obj, Object... args)
  2. invoke(E e, String methodName)
  3. invoke(final Method m, final Object obj, final Object... args)
  4. invoke(final Method method, final Object obj, final Object... args)
  5. invoke(final Object component, final Method method, final Class type, final String value)
  6. invoke(final Object instance, final String methodName, final Class[] methodParams, final Object[] args)
  7. invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)
  8. invoke(final Object target, final Method method, final Object... parameters)
  9. invoke(final Object target, final Method method, final Object[] params)