Here you can find the source of invoke(Object o, String name, Object[] args)
public static Object invoke(Object o, String name, Object[] args)
//package com.java2s; /**/*from ww w . j av a 2s .co m*/ * Copyright 2008, 2009 Mark Hooijkaas This file is part of the Caas tool. The Caas tool is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version * 3 of the License, or (at your option) any later version. The Caas tool is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. You should have received a copy of the GNU General Public License along with the Caas * tool. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static Object invoke(Object o, Method m, Object[] args) { try { m.setAccessible(true); return m.invoke(o, args); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { e.getCause().printStackTrace(); throw new RuntimeException(e.getCause()); } } public static Object invoke(Object o, String name, Object[] args) { return invoke(o.getClass(), o, name, args); } public static Object invoke(Class<?> c, Object o, String name, Object[] args) { try { return invoke(o, c.getDeclaredMethod(name, getSignature(args)), args); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } private static Class<?>[] getSignature(Object[] args) { Class<?>[] signature = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) signature[i] = args[i].getClass(); return signature; } }