通过反射获取泛型<T>的class

来源:互联网 发布:方维社区o2o系统 源码 编辑:程序博客网 时间:2024/06/10 07:32
  1. public class RawDao<T> {  
  2.     protected Class<T> clazz;  
  3.   
  4.     @SuppressWarnings("unchecked")  
  5.     public RawDao() {  
  6.         @SuppressWarnings("rawtypes")  
  7.         Class clazz = getClass();  
  8.   
  9.         while (clazz != Object.class) {  
  10.             Type t = clazz.getGenericSuperclass();  
  11.             if (t instanceof ParameterizedType) {  
  12.                 Type[] args = ((ParameterizedType) t).getActualTypeArguments();  
  13.                 if (args[0instanceof Class) {  
  14.                     this.clazz = (Class<T>) args[0];  
  15.                     break;  
  16.                 }  
  17.             }  
  18.             clazz = clazz.getSuperclass();  
  19.         }  
  20.     } 
0 0
原创粉丝点击