java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
獲取泛型參數(shù)的類型
出現(xiàn):
使用以下工具類方法獲取~
Class<T> entityClass = (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 * 通過(guò)反射,獲得定義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 * 通過(guò)反射,獲得定義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 }
2 import java.lang.reflect.ParameterizedType;
3 import java.lang.reflect.Type;
4
5 public class GenericsUtils {
6 /**
7 * 通過(guò)反射,獲得定義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 * 通過(guò)反射,獲得定義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<T> entityClass = GenericsUtils.getSuperClassGenricType(BasicService.class, 0);
posted on 2011-12-26 14:37 leisure 閱讀(17898) 評(píng)論(4) 編輯 收藏