leisure

          JAVA - exceed,helloworld
          隨筆 - 50, 文章 - 0, 評論 - 11, 引用 - 0
          數(shù)據(jù)加載中……

          java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

          獲取泛型參數(shù)的類型
                  
          Class<TentityClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

          出現(xiàn):
          java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

          使用以下工具類方法獲取~
           1 package cn.pconline.prolib.util;
           2 import java.lang.reflect.ParameterizedType;  
           3 import java.lang.reflect.Type;  
           4   
           5 public class GenericsUtils {  
           6     /**   
           7      * 通過反射,獲得定義Class時(shí)聲明的父類的范型參數(shù)的類型.   
           8      * 如public BookManager extends GenricManager<Book>   
           9      *   
          10      * @param clazz The class to introspect   
          11      * @return the first generic declaration, or <code>Object.class</code> if cannot be determined   
          12      */  
          13     public static Class getSuperClassGenricType(Class clazz) {  
          14         return getSuperClassGenricType(clazz, 0);  
          15     }  
          16   
          17     /**   
          18      * 通過反射,獲得定義Class時(shí)聲明的父類的范型參數(shù)的類型.   
          19      * 如public BookManager extends GenricManager<Book>   
          20      *   
          21      * @param clazz clazz The class to introspect   
          22      * @param index the Index of the generic ddeclaration,start from 0.   
          23      */  
          24     public static Class getSuperClassGenricType(Class clazz, int index) throws IndexOutOfBoundsException {  
          25   
          26         Type genType = clazz.getGenericSuperclass();  
          27   
          28         if (!(genType instanceof ParameterizedType)) {  
          29             return Object.class;  
          30         }  
          31   
          32         Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
          33   
          34         if (index >= params.length || index < 0) {  
          35             return Object.class;  
          36         }  
          37         if (!(params[index] instanceof Class)) {  
          38             return Object.class;  
          39         }  
          40         return (Class) params[index];  
          41     }  
          42 }  

                  
          Class<TentityClass = GenericsUtils.getSuperClassGenricType(BasicService.class0);

          posted on 2011-12-26 14:37 leisure 閱讀(17904) 評論(4)  編輯  收藏

          評論

          # re: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType[未登錄]  回復(fù)  更多評論   

          BasicService.class,不就是我需要的類型嗎?你Y的相當(dāng)于脫了褲子放屁一樣,傳入某類型對象又獲取某類型對象,有意義嗎?
          2013-02-05 15:47 | 小熊

          # re: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType[未登錄]  回復(fù)  更多評論   

          @小熊

          樓上的,沒看懂!樓主是拿泛型的Class類型。
          沒文化,等你到了這個(gè)層次,你就會(huì)理解。好好看多點(diǎn)架構(gòu)方面的吧。

          2013-03-07 11:57 | peter

          # re: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType  回復(fù)  更多評論   

          很對!
          2013-05-21 18:43 | yenshi

          # re: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType  回復(fù)  更多評論   

          還是不成功咋辦
          2013-09-25 12:42 | txy779

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 盐池县| 故城县| 秦皇岛市| 繁峙县| 红原县| 石河子市| 靖江市| 崇左市| 玛纳斯县| 石城县| 确山县| 青阳县| 黑河市| 九江市| 吴桥县| 满洲里市| 中超| 赫章县| 广州市| 东乌珠穆沁旗| 揭东县| 宜兴市| 汉中市| 安顺市| 永安市| 大姚县| 尚志市| 绥阳县| 太谷县| 西畴县| 依兰县| 新野县| 且末县| 青浦区| 广水市| 沙坪坝区| 富平县| 马关县| 密云县| 海伦市| 常熟市|