Getting the best fit Instance Method in Java

蹲街弑〆低调 提交于 2021-02-16 20:22:17

问题


If I run the following program:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { new Object().getClass() };
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};

I get the following on the command line:

true

On the other hand, if I modify the parameters line a little:

class Runit{
    public static void main(String[] argsWut) throws Exception {
        String arg = "what?";
        Class[] parameters = { arg.getClass() }; // changed a little here so it's a bit more dynamic --
        Object[] args = { arg };
        System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args));
    }
};

I get:

Exception in thread "main" java.lang.NoSuchMethodException: java.lang.String.equals(java.lang.String)
    at java.lang.Class.getMethod(Class.java:1605)
    at test.Runit.main(Runit.java:7)

From this one example it looks to me as though the getMethod method only works with exact parameters. Is there a way to get some form of a "best fit" method? e.g. If an exact match exists, it would return that method, but if no exact match exists, it can return any method that could accept my given arguments.


回答1:


From the documentation for getMethod():

To find a matching method in a class C: If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily.

(Emphasis mine.)

What you are asking for is to have reflection perform overload resolution for you. And apparently it won't. If you really need this functionality, you can either 1) give up on using reflection and invoke the method directly, or 2) if that's not possible, look up the rules for overload resolution in Java (you could start here), use getMethods() to determine the available methods, and then perform overload resolution manually. Fun times, I know.

Edit: As other answerers have pointed out, someone has already taken the time to do that for you. Cool!




回答2:


You may have better luck with the Apache Commons Lang MethodUtils class, which has a method "invokeMethod" that uses the target arguments for the method to narrow down the appropriate type (i.e., you don't have to tell it the parameter type).

This seems to work:

 System.out.println(MethodUtils.invokeMethod("what?", "equals", new Object[] {"what?"}));

See javadocs for more details: http://commons.apache.org/lang/api/org/apache/commons/lang3/reflect/MethodUtils.html#invokeMethod(java.lang.Object,%20java.lang.String,%20java.lang.Object...)




回答3:


You may be interested in Commons BeanUtils Apache library, specifically this method: http://commons.apache.org/beanutils/v1.8.0/apidocs/org/apache/commons/beanutils/MethodUtils.html#getMatchingAccessibleMethod%28java.lang.Class,%20java.lang.String,%20java.lang.Class%5b%5d%29

Hope this helps



来源:https://stackoverflow.com/questions/11110579/getting-the-best-fit-instance-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!