I have generic classes that look like:
interface X<Input, Output>
{
Output process(Input input);
}
class Y implements X<Integer, Float>
{
Float process(Integer input);
}
I use getDeclaredMethods to find process on Y just with its name (not the arguments, on purpose). When I look at the return Method[], process shows up twice, with Input=Object, Output=Object, and then with the actual instantiation types: Integer and Float.
Please note: I can see 1 function with Object,Object in the Method[] AND 1 function with the actual types I use to instantiate, like Integer,Float. So, the second function, which I’m interested in, is accessible from Method[].
What is the best way to get only the method with the actual types?
That’s not possible. You should see a single method whose parameter types are
Object. You can also look at the method’s generic parameter and return types, and you will find that they are both type variables (InputandOutputare type variables).Subclasses of this class that inherit from it with specific type arguments for the type parameters, will have two methods: a method with the more specific parameter and return types, and a bridge method, with the original class’s method’s parameter and return types, in order to override it. If you are asking how to ignore the bridge method, simply check if it’s a bridge method (
.isBridge())