I have a method with parameter Function<Entity, Integer>
. I pass a static method reference to that parameter Entity::getId
.
Is it possible to, during runtime, extract the return class Class<Integer>
from the function object?
I have a method with parameter Function<Entity, Integer>
. I pass a static method reference to that parameter Entity::getId
.
Is it possible to, during runtime, extract the return class Class<Integer>
from the function object?
as GenericDeclaration says:
getTypeParameters() Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
so supposing you have your class declared as:
public class YourParametizedClass<Entity, Integer> {
...
}
then you would get the value of the second parameter by doing:
YourParametizedClass function=new YourParametizedClass();
function.getClass().getTypeParameters()[1].getName();
this will return you "Integer".