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:
Niall Pemberton 2010-11-21 22:09:04 +00:00
parent ea623e575e
commit e8138eafe7
2 changed files with 26 additions and 3 deletions

View File

@ -136,7 +136,7 @@ private static float getObjectTransformationCost(Class<?> srcClass, Class<?> des
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 @@ private static float getObjectTransformationCost(Class<?> srcClass, Class<?> des
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;

View File

@ -296,6 +296,16 @@ public void testGetMatchingAccessibleMethod() throws Exception {
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 @@ private Class<?>[] singletonArray(Class<?> c) {
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 {}
}