Here you can find the source of invoke(Object targetObject, String methodName, Object... params)
public static Object invoke(Object targetObject, String methodName, Object... params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Main { private static Map<Class, Class> wrappers = new HashMap<Class, Class>(); public static Object invoke(Object targetObject, String methodName, Object... params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { return findMethod(targetObject.getClass(), methodName, params).invoke(targetObject, params); }/*w w w .j ava 2 s . com*/ private static Method findMethod(Class<?> clazz, String methodName, Object... params) throws NoSuchMethodException { for (Method c : clazz.getMethods()) { if (c.getName().equals(methodName) && match(c.getParameterTypes(), params)) { return c; } } throw new NoSuchMethodException(clazz.getName() + "." + methodName + argumentTypesToString(params)); } private static boolean match(Class<?>[] paramTypes, Object[] params) { if (params.length != paramTypes.length) { return false; } for (int i = 0; i < paramTypes.length; i++) { Class<?> type = paramTypes[i]; if (type.isPrimitive()) { type = wrappers.get(type); } if (params[i] != null && !type.isInstance(params[i])) { return false; } } return true; } private static String argumentTypesToString(Object[] args) { StringBuilder buf = new StringBuilder(); buf.append("("); if (args != null) { for (Object arg : args) { if (buf.length() > 1) { buf.append(", "); } buf.append(arg == null ? "null" : arg.getClass().getName()); } } buf.append(")"); return buf.toString(); } }