Is it possible to get the type of a generic parameter?
An example:
JavaScript
x
public final class Voodoo {
public static void chill(List<?> aListWithTypeSpiderMan) {
// Here I'd like to get the Class-Object 'SpiderMan'
Class typeOfTheList = ???;
}
public static void main(String args) {
chill(new ArrayList<SpiderMan>());
}
}
Advertisement
Answer
One construct, I once stumbled upon looked like
JavaScript
Class<T> persistentClass = (Class<T>)
((ParameterizedType)getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
So there seems to be some reflection-magic around that I unfortunetly don’t fully understand… Sorry.