I need to be able to retrieve the class/type of Object
present in ArrayList<TYPE>
returned by Method method
via reflection.
When calling:
Class<?> methodReturnType = method.getReturnType();
System.out.println("Method return type: " + methodReturnType);
on a method that returns ArrayList<String>
I get:
class java.util.ArrayList
which does not include information on the generic type: String
.
I believe this question may have been addressed here and here however I do see that if I call:
Type genericReturnType = method.getGenericReturnType();
System.out.println("Generic return type (name): " + genericReturnType.getTypeName());
This prints:
java.util.ArrayList<java.lang.String>
Which suggests that it is possible to know that the ArrayList<>
holds Object
of type String
.
Obviously, I might be able to extract String
from text: <java.lang.String>
via regex but this is not ideal.
What am I missing? Is it possible to obtain the type of ArrayList<>
of a method that returns ArrayList<TYPE>
at run-time via reflection? If not, how does genericReturnType.getTypeName()
print the right type (String)?
Aucun commentaire:
Enregistrer un commentaire