Reputation: 20966
You will get single result when you try to find a method by it's signature (name and parameter types list):
Method java.lang.Class.getMethod(String name, Class<?>... parameterTypes)
Let say I know parameterTypes length and all but one parameter type because I can't derive class of the parameter when it was null passed to me at runtime.
So I want to get all methods by method name and array of known parameter types. If it happens that result will be single method I'll call it otherwise I'll throw an exception.
Is there a lovely way to do that not re-implementing JDK's 'getMethod'?
Upvotes: 1
Views: 89
Reputation: 3685
The only way to get such method would be to use getMethods()
and manually filter it out, this is not that hard, just few simple checks for each method.
but, if you just need call a method you can use more dynamic way (but it will be also slower as you can't cache Method
instance)
public static void main(String[] args) throws Exception {
Expression someMethod = new Expression(Tezd.class, "someMethod", new Object[]{1, 2, null, "huh"});
someMethod.execute();
System.out.println(someMethod.getValue());
}
public static String someMethod(int a, int b, Object object, String str) {
return a + ", " + b + ", " + object + ", " + str;
}
First argument is a target, I used class as this is static method, for normal method just pass object here.
Sadly method used by java.beans.Expression
to get matching method is not public.
Upvotes: 1