Java Method Call invoke(Object instance, String method, Class[] paramTypes, Object... parameters)

Here you can find the source of invoke(Object instance, String method, Class[] paramTypes, Object... parameters)

Description

invoke

License

Apache License

Declaration

public static Object invoke(Object instance, String method, Class[] paramTypes, Object... parameters) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Object invoke(Object instance, String method, Object... parameters) throws Throwable {
        Class clazz = instance.getClass();
        for (Method m : clazz.getMethods()) {
            if (m.getName().equals(method)) {
                try {
                    return m.invoke(instance, parameters);
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException("Can't invoke method", e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Can't invoke method", e);
                } catch (InvocationTargetException e) {
                    throw e.getTargetException();
                }/* w  w  w.  j  av  a 2 s  .co m*/
            }
        }
        throw new RuntimeException("Can't find method");
    }

    public static Object invoke(Object instance, String method, Class[] paramTypes, Object... parameters) {
        Class clazz = instance.getClass();
        for (Method m : clazz.getMethods()) {
            if (m.getName().equals(method) && Arrays.equals(m.getParameterTypes(), paramTypes)) {
                try {
                    return m.invoke(instance, parameters);
                } catch (Exception e) {
                    throw new RuntimeException("Can't invoke method", e);
                }
            }
        }
        throw new RuntimeException("Can't find method");
    }
}

Related

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