Here you can find the source of invokeMethodAndGet(Object object, String methodName, Object... args)
Parameter | Description |
---|---|
object | target object. |
methodName | name of the method. |
args | arguments. |
Parameter | Description |
---|---|
Exception | an exception |
public static Object invokeMethodAndGet(Object object, String methodName, Object... args) throws Exception
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Method; public class Main { /**/* w w w.j av a 2s .c om*/ * Invokes and returns result of method with given name and arguments of target object's class. * @param object target object. * @param methodName name of the method. * @param args arguments. * @return result of method invokation. * @throws Exception */ public static Object invokeMethodAndGet(Object object, String methodName, Object... args) throws Exception { Class[] params = new Class[args.length]; for (int i = 0; i < args.length; ++i) params[i] = args[i].getClass(); Method m = object.getClass().getDeclaredMethod(methodName, params); m.setAccessible(true); try { return m.invoke(object, args); } finally { m.setAccessible(false); } } }