BEANUTILS-381 getMatchingAccessibleMethod does not correctly handle inheritance and method overloading - thanks to Todd Nine for the patch
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1037572 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ea623e575e
commit
e8138eafe7
|
@ -136,7 +136,7 @@ abstract class MemberUtils {
|
|||
return getPrimitivePromotionCost(srcClass, destClass);
|
||||
}
|
||||
float cost = 0.0f;
|
||||
while (destClass != null && !destClass.equals(srcClass)) {
|
||||
while (srcClass != null && !destClass.equals(srcClass)) {
|
||||
if (destClass.isInterface() && ClassUtils.isAssignable(srcClass, destClass)) {
|
||||
// slight penalty for interface match.
|
||||
// we still want an exact match to override an interface match,
|
||||
|
@ -147,13 +147,13 @@ abstract class MemberUtils {
|
|||
break;
|
||||
}
|
||||
cost++;
|
||||
destClass = destClass.getSuperclass();
|
||||
srcClass = srcClass.getSuperclass();
|
||||
}
|
||||
/*
|
||||
* If the destination class is null, we've travelled all the way up to
|
||||
* an Object match. We'll penalize this by adding 1.5 to the cost.
|
||||
*/
|
||||
if (destClass == null) {
|
||||
if (srcClass == null) {
|
||||
cost += 1.5f;
|
||||
}
|
||||
return cost;
|
||||
|
|
|
@ -296,6 +296,16 @@ public class MethodUtilsTest extends TestCase {
|
|||
singletonArray(Double.class), singletonArray(Double.TYPE));
|
||||
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
|
||||
singletonArray(Double.TYPE), singletonArray(Double.TYPE));
|
||||
expectMatchingAccessibleMethodParameterTypes(TestBean.class, "foo",
|
||||
singletonArray(Double.TYPE), singletonArray(Double.TYPE));
|
||||
expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testOne",
|
||||
singletonArray(ParentObject.class), singletonArray(ParentObject.class));
|
||||
expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testOne",
|
||||
singletonArray(ChildObject.class), singletonArray(ParentObject.class));
|
||||
expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo",
|
||||
singletonArray(ParentObject.class), singletonArray(GrandParentObject.class));
|
||||
expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo",
|
||||
singletonArray(ChildObject.class), singletonArray(ChildInterface.class));
|
||||
}
|
||||
|
||||
private void expectMatchingAccessibleMethodParameterTypes(Class<?> cls,
|
||||
|
@ -320,4 +330,17 @@ public class MethodUtilsTest extends TestCase {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static class InheritanceBean {
|
||||
public void testOne(Object obj) {}
|
||||
public void testOne(GrandParentObject obj) {}
|
||||
public void testOne(ParentObject obj) {}
|
||||
public void testTwo(Object obj) {}
|
||||
public void testTwo(GrandParentObject obj) {}
|
||||
public void testTwo(ChildInterface obj) {}
|
||||
}
|
||||
interface ChildInterface {}
|
||||
public static class GrandParentObject {}
|
||||
public static class ParentObject extends GrandParentObject {}
|
||||
public static class ChildObject extends ParentObject implements ChildInterface {}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue