This is my code, shared here, so you can run it.
public interface GenericRepository<T> {
default Class<T> getEntityClazz() {
ParameterizedType pType = (ParameterizedType)this.getClass().getGenericInterfaces()[0];
Type tT = pType.getActualTypeArguments()[0];
Class<T> clazz = (Class<T>) tT.getClass();
return clazz;
}
}
public class GenericRepositoryImpl<T> implements GenericRepository<T> {
}
My main is:
public class Main
{
public static void main(String[] args) {
GenericRepository<Example> genericObject = new GenericRepositoryImpl();
Class<Example> clazz = genericObject.getEntityClazz();
System.out.println("Generic class: " + clazz.toString());
}
}
And output is:
Generic class: class sun.reflect.generics.reflectiveObjects.TypeVariableImpl
I was expecting to get Generic class: class Example
.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire