2

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?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Frank
  • 4,341
  • 8
  • 41
  • 57

2 Answers2

5

and then with the actual instantiation 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 (Input and Output are 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())

newacct
  • 119,665
  • 29
  • 163
  • 224
0

Generic types are known only at compilation time. Object is the actual type of generics at runtime, so you can't get them while running the program.

user1871166
  • 258
  • 2
  • 9