Commons BeanUtils getMatchingAccessibleMethod finds a match, but not the best possible match.
Consider this simple example:
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();
}
}
}
(Hint: invokeMethod uses getMatchingAccessibleMethod)
The above code outputs
--- setters:
setData x
setData y
setData y
--- invokeMethod
setData x
The last line shoud say "setData y" because the best match for calling "setData" with a DataZ object should be the one with DataY in the interface (just like setData(new DataZ()) does).
Is there a way to find the best possible match or do I have to code that myself?