共享BeanUtils的getMatchingAccessibleMethod找到匹配,但不是最好的比赛。
考虑一个简单的例子:
public class TestReflection extends TestCase {
public static class BeanA {
private DataX data;
public BeanA setData(DataX x) {
System.out.println("setData x");
return this;
}
public BeanA setData(DataY y) {
System.out.println("setData y");
return this;
}
}
static class DataX {
}
static class DataY extends DataX {
}
static class DataZ extends DataY {
}
public void testPropertyUtils() {
try {
BeanA a = new BeanA();
System.out.println("--- setters:");
a.setData(new DataX());
a.setData(new DataY());
a.setData(new DataZ());
System.out.println("--- invokeMethod");
MethodUtils.invokeMethod(a, "setData", new DataZ());
} catch (Exception e) {
e.printStackTrace();
}
}
}
(提示:invokeMethod中使用getMatchingAccessibleMethod)
上面的代码输出
--- setters:
setData x
setData y
setData y
--- invokeMethod
setData x
最后一行768,16说“使用setData Y”,因为用于调用“使用setData”与DATAZ对象的最佳匹配应该是在接口(就像使用setData(新DATAZ())做)的一个与DATAY。
有没有办法找到的最佳匹配或者我必须代码自己?
我只是好奇,怎么做的工作在MethodUtils.java,所以我看了看里面。 为了确定哪种方法应作为最佳匹配,每个方法都将获得一个成本。 要计算有一种方法的成本(与一些额外的DBG输出):
/**
* Gets the number of steps required needed to turn the source class into the
* destination class. This represents the number of steps in the object hierarchy
* graph.
* @param srcClass The source class
* @param destClass The destination class
* @return The cost of transforming an object
*/
private static float getObjectTransformationCost(Class srcClass, Class destClass) {
System.out.println("----------- start calculate cost from " + srcClass + " to " + destClass + "------------");
float cost = 0.0f;
while (destClass != null && !destClass.equals(srcClass)) {
System.out.println(srcClass + " and " + destClass + " are " + (destClass.equals(srcClass)? " equal" : " not equal"));
if (destClass.isInterface() && isAssignmentCompatible(destClass,srcClass)) {
// slight penalty for interface match.
// we still want an exact match to override an interface match, but
// an interface match should override anything where we have to get a
// superclass.
cost += 0.25f;
break;
}
cost++;
destClass = destClass.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) {
cost += 1.5f;
}
System.out.println("COST IS " + cost);
return cost;
}
所以输出
--- setters:
setData x
setData y
setData y
--- invokeMethod
----------- start calculate cost from class Lolka$DataZ to class Lolka$DataX------------
class Lolka$DataZ and class Lolka$DataX are not equal
class Lolka$DataZ and class java.lang.Object are not equal
COST IS 3.5
----------- start calculate cost from class Lolka$DataZ to class Lolka$DataY------------
class Lolka$DataZ and class Lolka$DataY are not equal
class Lolka$DataZ and class Lolka$DataX are not equal
class Lolka$DataZ and class java.lang.Object are not equal
COST IS 4.5
setData x
所以invokeMethode假定转换DATAX只有一个继承层次形式对象,DATAY是2。因此DATAX-方法是“便宜”。 那背后的逻辑。
UPD:改变DEST为src正常工作,所以如果我使用
private static float getObjectTransformationCost(Class srcClass, Class destClass) {
float cost = 0.0f;
while (srcClass != null && !destClass.equals(srcClass)) {
if (destClass.isInterface() && isAssignmentCompatible(destClass,srcClass)) {
cost += 0.25f;
break;
}
cost++;
srcClass = srcClass.getSuperclass();
}
if (srcClass == null) {
cost += 1.5f;
}
return cost;
}
输出
--- setters:
setData x
setData y
setData y
--- invokeMethod
setData y