Here you can find the source of invokeMethod(Method method, Object instance, Object... parameters)
Parameter | Description |
---|---|
method | the method to invoke |
instance | the object instance (or null for static methods) |
parameters | the parameters to the method |
public static Object invokeMethod(Method method, Object instance, Object... parameters)
//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()); } } }