Here you can find the source of getMethod(Class target, String methodName, Class[] parameterTypes)
public static Method getMethod(Class target, String methodName, Class[] parameterTypes)
//package com.java2s; /**/*from w w w . j a v a 2 s . c o m*/ * * Licensed under the GNU General Public License (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.gnu.org/licenses/gpl.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * @author Vadim Kisen * * copyright 2010 by uTest */ import java.lang.reflect.Method; public class Main { public static Method getMethod(Class target, String methodName, Class[] parameterTypes) { return findMethod(target.getClass().getMethods(), methodName, parameterTypes); } public static Method getMethod(Class target, String methodName, Object[] parameters) { return getMethod(target, methodName, fromObjectsToClasses(parameters)); } public static Method findMethod(Method[] methods, String methodName, Class[] parameterTypes) { Method method = null; for (Method methodCandidate : methods) { if (!methodCandidate.getName().equals(methodName)) { continue; } Class[] params = methodCandidate.getParameterTypes(); if (params.length != parameterTypes.length) { continue; } boolean isOk = true; for (int i = 0; i < params.length; i++) { if (parameterTypes[i].isPrimitive()) { if (parameterTypes[i].equals(int.class) && params[i].equals(Integer.class)) { continue; } if (parameterTypes[i].equals(double.class) && params[i].equals(Double.class)) { continue; } if (parameterTypes[i].equals(float.class) && params[i].equals(Float.class)) { continue; } if (parameterTypes[i].equals(long.class) && params[i].equals(Long.class)) { continue; } if (parameterTypes[i].equals(boolean.class) && params[i].equals(Boolean.class)) { continue; } if (parameterTypes[i].equals(byte.class) && params[i].equals(Byte.class)) { continue; } if (parameterTypes[i].equals(short.class) && params[i].equals(Short.class)) { continue; } } else if (!params[i].isAssignableFrom(parameterTypes[i])) { isOk = false; break; } } if (isOk) { method = methodCandidate; break; } } return method; } public static Method findMethod(Method[] methods, String methodName, Object[] parameters) { return findMethod(methods, methodName, fromObjectsToClasses(parameters)); } public static Class[] fromObjectsToClasses(Object[] parameters) { Class[] parameterTypes = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { if (parameters[i] != null) { parameterTypes[i] = parameters[i].getClass(); } else { parameterTypes[i] = Object.class; } } return parameterTypes; } }