Create a class object out of a generic type

Write a class which looks like the following <code> public class Test<T> { public Test() { Class<T> clazz = ? } } </code> Call this class with some self generated class which contains Annotations and try to read out the Annotations of this class from the clazz variable which has to be defined.
1 answer

Get Class object from generic type T

To achieve to get the Class object from a generic Type a pretty complicated looking command has to be used:

Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

now you have the Class object and can access the annotations with

(([SomeAnnotation]) clazz.getAnnotation([SomeAnnotation].class));

Taggings: