Create instance of generic type in Java when parameterized type passes through hierarchies?
问题 I have been reading the answers to the question: Create instance of generic type in Java? I have implemented the approach suggested by Lars Bohl. I adapted his code as follows: import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class ParameterizedTypeEg<E> { public Class<E> getTypeParameterClass() { Type type = getClass().getGenericSuperclass(); ParameterizedType paramType = (ParameterizedType) type; return (Class<E>) paramType.getActualTypeArguments()[0]; }