When writing something like
doit(43, 44, "hello");
the compiler knows which overloaded method is to be called. When I want to do the same via reflection, I need to find out myself, that the method is
doit(Integer, double, CharSequence...);
and obtain it via something like
Class[] types = {Integer.class, double.class, CharSequence[].class};
declaringClass.getDeclaredMethod("doit", types);
I wonder if there's already something allowing me to write just
Method m = getMethod(declaringClass, "doit", 43, 44, "hello");
I wonder if somebody did this already, as the JLS is a bit complicated in this respect.
Actually, behaving exactly like the compiler is impossible as in Phase 1 the compiler accepts only methods matching without boxing and unboxing. When calling my hypothetical getMethod
from above, the distinction between primitives and their wrappers is already lost (because of autoboxing when passing arguments via varargs). This problem seems to have no solution, so let's ignore it.
As suggested in an answer, BeanUtils.invokeMethod
comes close. It's supposed to find the best match, whatever it means. Looking at MethodUtils.getMatchingAccessibleMethod
shows that
- it knows nothing about varargs
- it's non-deterministic
so I'm looking for something better.
The
MethodHandle
is a new way to get a overloaded method using a signature (java 7):Example:
Outputs:
Alternatively you could use Bean Utils from Apache Commons:
According documentation:
The implementation get the accessible method and goes up in the hierarchy until it founds a match to it.
Direct to the Invocation
In order to perform invocation directly as you asked, you could use this method from the same API:
or even
that first locates the method using
getAccessibleMethod
and later on invokes it.