My project include reflection and i'm currently handling with generic types. I've managed to get the generic type from a List(java.util.List) with:
if(originalField.getGenericType() instanceof ParameterizedType){
ParameterizedType pt = (ParameterizedType) originalField.getGenericType();
Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
}
I also have a Collection<String>
and it turns out that Collection can NOT be cast to ParameterizedType.
Someone have an idea how to get the String (or whatever type that will be) out of the collection?
I've been reading about java erasure so i'm aware of that, but after getting the generic out of the List, i thought that maybe someone knows another 'trick'.
Thanks.
EDITED: I'm iterating over all of the fields of the class with:
for (Field originalField : someClass.getDeclaredFields())
And then when i'm facing some List<String>
Field, with the above code i getting the String
as the subClass variable.