Finding best Method in Class for param list

I have a Class I want to query for a Method, the name of the method I want and the set of parameters I wish to pass to the method. Because of inheritnce, my parameter list may not exactly match any of the class's published methods, even though a java compiler would be able to use inheritance to find a best match.

For example,

abstractpublicclass House

{

abstractboolean isWindowOpen(int windowIndex);

abstractvoid setYardBounds(Rectangle bounds);

}

If I were to call House.class.getMethod("setYardBounds", Rectangle2D.Float.class)

a NoSuchMethodException is thrown. Similarly, House.class.getMethod("isWindow", Integer.class)

will fail.

I would like to determine the 'closest match' method; that is, the method the Java compiler would pick if it were to find the matching method for my parameter list. Is there an easy way to do this?

[1279 byte] By [Mark_McKaya] at [2007-10-3 4:53:22]
# 1
Did you consider trying Class.getMethods() instead, which returns an array of all the methods? Then you could iterate over that array looking for the method name, as long as you don't also have to consider overloaded methods (multiple methods with the same name but different parm types).
warnerjaa at 2007-7-14 22:58:13 > top of Java-index,Java Essentials,Java Programming...
# 2
That's what I'm doing now. However, there are several special cases, and if there already exists a method in the JDK that can find the best match, I'd prefer to use that. My code hasn't failed yet, but I'm not 100% confident it will correctly match in all cases.
Mark_McKaya at 2007-7-14 22:58:13 > top of Java-index,Java Essentials,Java Programming...
# 3

> That's what I'm doing now.

I don't see a usage of getMethods() in the code you posted - it looks like you're only calling getMethod(args). So I don't know if you misread my post or not.

I believe there is no "find the best match" method. But if you call getMethods() you'll already have all the methods, and you can iterate over the collection of them and find it yourself.

warnerjaa at 2007-7-14 22:58:13 > top of Java-index,Java Essentials,Java Programming...