Back
How can I achieve this?
public class GenericClass<T>{ public Type getMyType() { //How do I return the type of T? }}
public class GenericClass<T>
{
public Type getMyType()
//How do I return the type of T?
}
Everything I have tried so far always returns the type Object rather than the specific type used.
Try this:
private Class<T> persistentClass;public Constructor() { this.persistentClass = (Class<T>) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; }
private Class<T> persistentClass;
public Constructor() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
To learn more about it click here.
31k questions
32.8k answers
501 comments
693 users