Here you can find the source of invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues)
@SuppressWarnings("rawtypes") public static Object invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues) throws Exception
//package com.java2s; //License from project: Apache License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { @SuppressWarnings("rawtypes") public static Object invoke(final Object object, final String methodName, final Class[] methodParamTypes, final Object[] methodParamValues) throws Exception { Object result = null;/*from w ww . jav a2 s . c o m*/ try { result = object.getClass().getMethod(methodName, methodParamTypes).invoke(object, methodParamValues); } catch (InvocationTargetException ite) { Throwable t = ite.getTargetException(); if (t instanceof Exception) { throw (Exception) t; } throw ite; } catch (Exception t) { t.printStackTrace(); } return result; } @SuppressWarnings("rawtypes") public static Object invoke(Object obj, String method, Object[] params, Class[] param_types) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = obj.getClass().getMethod(method, param_types); return m.invoke(obj, params); } @SuppressWarnings("rawtypes") public static Object invoke(Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method[] all_methods = obj.getClass().getMethods(); Method method2invoke = null; // find method with the same name and matching argument types for (int i = 0; i < all_methods.length; i++) { Method m = all_methods[i]; if (m.getName().equals(method)) { Class[] pt = m.getParameterTypes(); boolean match = true; int match_loops = pt.length; if (match_loops != params.length) { continue; } for (int j = 0; j < match_loops; j++) { if (pt[i].isInstance(params[i]) == false) { match = false; break; } } if (match == true) { method2invoke = m; } } } // throw an exception if no method to invoke if (method2invoke == null) { String t = "("; for (int i = 0; i < params.length; i++) { if (i != 0) { t += ", "; } t += params[i].getClass().getName(); } t += ")"; throw new NoSuchMethodException(obj.getClass().getName() + "." + method + t); } // finally, invoke founded method return method2invoke.invoke(obj, params); } public static Method getMethod(Object o, String methodName) { if ((methodName == null) || (o == null)) { return null; } Method[] ms = o.getClass().getMethods(); for (int i = 0; i < ms.length; i++) { Method m = ms[i]; if (m.getName().equals(methodName)) { return m; } } return null; } }