How to get generic's class

前端 未结 3 1514
南方客
南方客 2020-12-19 18:52
Class Model{

   private T t;

   .....


   private void someMethod(){
       //now t is null
       Class c = t.getClass();
   } 

   .....

}
         


        
3条回答
  •  没有蜡笔的小新
    2020-12-19 19:11

    You can do it without passing in the class:

    class Model {
      Class c = (Class) DAOUtil.getTypeArguments(Model.class, this.getClass()).get(0);
    }
    

    You need two functions from this file: http://code.google.com/p/hibernate-generic-dao/source/browse/trunk/dao/src/main/java/com/googlecode/genericdao/dao/DAOUtil.java

    For more explanation: http://www.artima.com/weblogs/viewpost.jsp?thread=208860

提交回复
热议问题