fork(1) download
  1.  
  2. /* package whatever; // don't place package name! */
  3.  
  4. import java.lang.reflect.ParameterizedType;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. abstract class BaseDao<T> {
  8.  
  9. @SuppressWarnings("unchecked")
  10. protected Class<T> getClazz() {
  11.  
  12. final ParameterizedType type = (ParameterizedType) this.getClass()
  13. .getGenericSuperclass();
  14. Class<T> clazz = (Class<T>) type.getActualTypeArguments()[0];
  15. return clazz;
  16. }
  17.  
  18. public static void main(String[] args) throws java.lang.Exception {
  19.  
  20. BaseDao<String> daoString = new BaseDao<String>() {};
  21. System.out.println(daoString.getClazz().getSimpleName());
  22. BaseDao<Long> daoLong = new BaseDao<Long>() {};
  23. System.out.println(daoLong.getClazz().getSimpleName());
  24. }
  25.  
  26. }
  27.  
Success #stdin #stdout 0.07s 381184KB
stdin
Standard input is empty
stdout
String
Long