Here you can find the source of invokeMethod(final Method method, final Object instance, final Object... args)
Parameter | Description |
---|---|
method | method to call |
instance | object instance to call method on |
args | optional arguments |
T | expected type of execution result |
@SuppressWarnings("unchecked") public static <T> T invokeMethod(final Method method, final Object instance, final Object... args)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Method; import java.util.Arrays; public class Main { /**/* ww w . ja v a 2 s . c o m*/ * @param method method to call * @param instance object instance to call method on * @param args optional arguments * @param <T> expected type of execution result * @return method execution result */ @SuppressWarnings("unchecked") public static <T> T invokeMethod(final Method method, final Object instance, final Object... args) { final boolean acc = method.isAccessible(); method.setAccessible(true); try { return (T) method.invoke(instance, args); } catch (Exception e) { throw new IllegalStateException(String.format("Failed to invoke method '%s#%s(%s)'", method.getDeclaringClass(), method.getName(), Arrays.toString(args)), e); } finally { method.setAccessible(acc); } } }