Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Main { private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = new HashMap<Class<?>, Class<?>>(); private static Set<Method> findMethodsCompatibleWith(boolean staticMethod, Set<Method> methods, String methodName, Class<?>[] argTypes) { final Set<Method> list = new HashSet<Method>(methods); for (final Iterator<Method> it = list.iterator(); it.hasNext();) { final Method method = it.next(); if (!methodName.equals(method.getName())) { it.remove(); continue; } if (argTypes.length != method.getParameterTypes().length) { it.remove(); continue; } if (Modifier.isAbstract(method.getModifiers()) || Modifier.isStatic(method.getModifiers()) != staticMethod) { it.remove(); continue; } if (!isMethodArgCompatible(method, argTypes)) { it.remove(); continue; } } return list; } private static boolean isMethodArgCompatible(Method method, Class<?>... argTypes) { for (int i = 0; i < argTypes.length; i++) { if (!isCompatible(method.getParameterTypes()[i], argTypes[i])) { return false; } } return true; } private static boolean isCompatible(Class<?> type0, Class<?> type1) { if (type0.getName().equals(type1.getName())) { return true; } if (type0.isPrimitive()) { return isCompatible(PRIMITIVE_TO_WRAPPER.get(type0), type1); } if (type1.isPrimitive()) { return isCompatible(type0, PRIMITIVE_TO_WRAPPER.get(type1)); } return type0.isAssignableFrom(type1); } }