Java Reflection Method Invoke invokeMethod(Method method, Object instance, Object... parameters)

Here you can find the source of invokeMethod(Method method, Object instance, Object... parameters)

Description

A helper method to invoke a method via reflection and wrap any exceptions as RuntimeException instances

License

Apache License

Parameter

Parameter Description
method the method to invoke
instance the object instance (or null for static methods)
parameters the parameters to the method

Return

the result of the method invocation

Declaration

public static Object invokeMethod(Method method, Object instance, Object... parameters) 

Method Source Code

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

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

public class Main {
    /**/*from ww  w  . ja v a  2 s . c o m*/
     * A helper method to invoke a method via reflection and wrap any exceptions
     * as {@link RuntimeException} instances
     *
     * @param method     the method to invoke
     * @param instance   the object instance (or null for static methods)
     * @param parameters the parameters to the method
     * @return the result of the method invocation
     */
    public static Object invokeMethod(Method method, Object instance, Object... parameters) {
        if (method == null) {
            return null;
        }
        try {
            return method.invoke(instance, parameters);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        }
    }
}

Related

  1. invokeMethod(Method m, Object instance, Object[] args)
  2. invokeMethod(Method m, Object[] o)
  3. invokeMethod(Method meth, String str)
  4. invokeMethod(Method method, Class beanClass, Object element)
  5. invokeMethod(Method method, Object bean, Object[] values)
  6. invokeMethod(Method method, Object object)
  7. invokeMethod(Method method, Object object, Object... args)
  8. invokeMethod(Method method, Object object, Object... arguments)
  9. invokeMethod(Method method, Object target)