forked from qiujiayu/AutoLoadCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectUtils.java
More file actions
41 lines (36 loc) · 1.15 KB
/
ReflectUtils.java
File metadata and controls
41 lines (36 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.test;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* 注意:只能获取超类中的参数类型
* @author jiayu.qiu
*/
public class ReflectUtils {
/**
* 获得超类的参数类型,取第一个参数类型
* @param <T> 类型参数
* @param clazz 超类类型
*/
public static Class<?> getClassGenricType(final Class<?> clazz) {
return getClassGenricType(clazz, 0);
}
/**
* 根据索引获得超类的参数类型
* @param clazz 超类类型
* @param index 索引
*/
public static Class<?> getClassGenricType(final Class<?> clazz, final int index) {
Type genType=clazz.getGenericSuperclass();
if(!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params=((ParameterizedType)genType).getActualTypeArguments();
if(index >= params.length || index < 0) {
return Object.class;
}
if(!(params[index] instanceof Class)) {
return Object.class;
}
return (Class<?>)params[index];
}
}