Here you can find the source of invoke(Object target, String methodName)
private static Object invoke(Object target, String methodName) throws Exception
//package com.java2s; // License as published by the Free Software Foundation; either import java.lang.reflect.Method; import java.util.Hashtable; import java.util.Map; public class Main { /** The classloader we should use to load javahelp classes */ private static ClassLoader classloader = null; private static final Object[] NO_ARGS = new Object[0]; private static Map METHOD_ARGS = new Hashtable(); private static Map METHODS = new Hashtable(); private static Object invoke(Object target, String methodName) throws Exception { return invoke(target, methodName, NO_ARGS); }/*from ww w .ja v a2 s . c o m*/ private static Object invoke(Object target, String methodName, Object arg1) throws Exception { return invoke(target, methodName, new Object[] { arg1 }); } private static Object invoke(Object target, String methodName, Object arg1, Object arg2) throws Exception { return invoke(target, methodName, new Object[] { arg1, arg2 }); } private static Object invoke(Object target, String methodName, Object arg1, Object arg2, Object arg3) throws Exception { return invoke(target, methodName, new Object[] { arg1, arg2, arg3 }); } private static Object invoke(Object target, String methodName, Object[] args) throws Exception { try { Method m = getMethod(target, methodName); if (target instanceof String) return m.invoke(null, args); else return m.invoke(target, args); } catch (Exception e) { e.printStackTrace(); throw e; } } private static Method getMethod(Object target, String methodName) throws Exception { Method result = (Method) METHODS.get(methodName); if (result == null) { Object[] argTypes = (Object[]) METHOD_ARGS.get(methodName); result = getMethod(target, methodName, argTypes); METHODS.put(methodName, result); } return result; } private static Method getMethod(Object target, String methodName, Object[] argTypes) throws Exception { Class c = resolveClass(target); Class[] paramTypes = new Class[argTypes.length]; for (int i = 0; i < paramTypes.length; i++) paramTypes[i] = resolveClass(argTypes[i]); return c.getMethod(methodName, paramTypes); } private static Class resolveClass(Object obj) throws Exception { if (obj instanceof Class) return (Class) obj; else if (obj instanceof String) return classloader.loadClass((String) obj); else return obj.getClass(); } }