Reputation: 8231
Is it possibly to access the generic argument in the following line ?
public List<StoryLikeRef> getLikes() throws IOException
I mean by reflection get the StoryLikeRef from out the return type ?
Thanks
Upvotes: 2
Views: 142
Reputation: 1503759
Yes, you can, assuming StoryLikeRef
is a concrete type (rather than a type parameter itself). Use Method.getGenericReturnType
to get a Type
. Sample code:
import java.lang.reflect.*;
import java.util.*;
public class Test {
public List<String> getStringList() {
return null;
}
public List<Integer> getIntegerList() {
return null;
}
public static void main(String[] args) throws Exception {
showTypeParameters("getStringList");
showTypeParameters("getIntegerList");
}
// Only using throws Exception for sample code. Don't do
// this in real life.
private static void showTypeParameters(String methodName)
throws Exception {
Method method = Test.class.getMethod(methodName);
Type returnType = method.getGenericReturnType();
System.out.println("Overall return type: " + returnType);
if (returnType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) returnType;
for (Type t: type.getActualTypeArguments()) {
System.out.println(" Type parameter: " + t);
}
} else {
System.out.println("Not a generic type");
}
}
}
Upvotes: 10