public static Method getWidenedTarget(Class cls, Method method) { if (Modifier.isStatic(method.getModifiers())) { return method; } Method m = method, best = method; Class[] args = method.getParameterTypes(); String name = method.getName(); Class rt = m.getReturnType(); Class currentCls = cls; while (currentCls != null) { for (Class iface : currentCls.getInterfaces()) { if ((m = getExactMatch(name, args, rt, iface)) != null) { best = m; } } currentCls = currentCls.getSuperclass(); } if (best != method) return best; for (currentCls = cls; currentCls != null; currentCls = currentCls.getSuperclass()) { if ((m = getExactMatch(name, args, rt, currentCls)) != null) { best = m; } } return best; }
public static Method getWidenedTarget(Class cls, Method method) { if (Modifier.isStatic(method.getModifiers())) { return method; } Method m = method, best = method; Class[] args = method.getParameterTypes(); String name = method.getName(); Class rt = m.getReturnType(); Class currentCls = cls; while (currentCls != null) { for (Class iface : currentCls.getInterfaces()) { if ((m = getExactMatch(name, args, rt, iface)) != null) { best = m; } } currentCls = currentCls.getSuperclass(); } if (best != method) return best; for (currentCls = cls; currentCls != null; currentCls = currentCls.getSuperclass()) { if ((m = getExactMatch(name, args, rt, currentCls)) != null) { best = m; } } return best; }
public static Method getWidenedTarget(Method method) { Class cls = method.getDeclaringClass(); Method m = method, best = method; Class[] args = method.getParameterTypes(); String name = method.getName(); Class rt = m.getReturnType(); do { if (cls.getInterfaces().length != 0) { for (Class iface : cls.getInterfaces()) { if ((m = getExactMatch(name, args, rt, iface)) != null) { if ((best = m).getDeclaringClass().getSuperclass() != null) { cls = m.getDeclaringClass(); } } } } if (cls != method.getDeclaringClass()) { if ((m = getExactMatch(name, args, rt, cls)) != null) { if ((best = m).getDeclaringClass().getSuperclass() != null) { cls = m.getDeclaringClass(); } } } } while ((cls = cls.getSuperclass()) != null); return best; }
public void testMethodOverloadMatch() throws Exception { OverloadedClass c = new OverloadedClass(); Method found = ParseTools.getExactMatch("putXX", new Class[]{int.class, String.class}, void.class, OverloadedInterface.class); Method correct = OverloadedInterface.class.getMethod("putXX", new Class[]{int.class, String.class}); assertEquals(correct, found); }