直接上代码
public class ParameterizedTypeUtils {
/**
* 根据索引获取泛型实例类
*
* @param type 类型
* @param index
* @return java.lang.Class<?>
*/
public static Type get(Type type, int index) {
ParameterizedType aClass = (ParameterizedType) type;
Type[] actualTypes = aClass.getActualTypeArguments();
return actualTypes[index];
}
}
Api
带泛型的属性值 Field.getGenericType
带泛型的方法返回值 Method.getGenericReturnType
带泛型的父类 Class.getGenericSuperclass
带泛型的接口集合 Class.getGenericInterfaces
例子
获取不涉及泛型的返回值以及属性值
List<Integer> list1 = new ArrayList<>();
List<Integer[]> list2 = new ArrayList<>();
List<List<Integer>> list3 = new ArrayList<>();
List<String> test(){
return null;
}
List<Map<String,Object>> testMap(){
return null;
}
@Test
public void typeTest() throws NoSuchFieldException, NoSuchMethodException {
Type list1 = this.getClass().getDeclaredField("list1").getGenericType();
Type list2 = this.getClass().getDeclaredField("list2").getGenericType();
Type list3 = this.getClass().getDeclaredField("list3").getGenericType();
Type test = this.getClass().getDeclaredMethod("test").getGenericReturnType();
Type testMap = this.getClass().getDeclaredMethod("testMap").getGenericReturnType();
Type type1 = ParameterizedTypeUtils.get(list1, 0);
Type type2 = ParameterizedTypeUtils.get(list2, 0);
Type type3 = ParameterizedTypeUtils.get(list3, 0);
Type testType= ParameterizedTypeUtils.get(test, 0);
Type typeMapType = ParameterizedTypeUtils.get(testMap, 0);
System.out.println(type2.getClass() + " : "+type1);
System.out.println(type2.getClass() + " : "+type2.getTypeName());
System.out.println(type3.getClass() + " : "+type3);
System.out.println(testType.getClass() + " : "+testType);
System.out.println(typeMapType.getClass() + " : "+typeMapType);
}
}
输出 :
class java.lang.Class : class java.lang.Integer
class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl : java.util.List<java.lang.Integer>
class java.lang.Class : class java.lang.String
class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl : java.util.Map<java.lang.String, java.lang.Object>
这里注意,第二个和第四个的返回值又是一个泛型Type,属于泛型中的泛型类 ParameterizedTypeImpl
,并不是 Class ,如果直接强转为 Class<?> 会报错
System.out.println((Class<?>)ParameterizedTypeUtils.get( testMap , 0)); // 报错
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
涉及泛型
public class Main<T,E> {
List<? extends T> list = new ArrayList<>();
List<T> list1 = new ArrayList<>();
List<T[]> list2 = new ArrayList<>();
List<List<T>> list3 = new ArrayList<>();
List<T> test(){
return null;
}
List<Map<String,T>> testMap(){
return null;
}
@Test
public void typeTest() throws NoSuchFieldException, NoSuchMethodException {
System.out.println(Arrays.toString(this.getClass().getTypeParameters()));
Type list = this.getClass().getDeclaredField("list").getGenericType();
Type list1 = this.getClass().getDeclaredField("list1").getGenericType();
Type list2 = this.getClass().getDeclaredField("list2").getGenericType();
Type list3 = this.getClass().getDeclaredField("list3").getGenericType();
Type test = this.getClass().getDeclaredMethod("test").getGenericReturnType();
Type testMap = this.getClass().getDeclaredMethod("testMap").getGenericReturnType();
Type type = ParameterizedTypeUtils.get(list , 0);
Type type1 = ParameterizedTypeUtils.get(list1, 0);
Type type2 = ParameterizedTypeUtils.get(list2, 0);
Type type3 = ParameterizedTypeUtils.get(list3, 0);
Type testType= ParameterizedTypeUtils.get(test, 0);
Type typeMapType = ParameterizedTypeUtils.get(testMap, 0);
System.out.println(type.getClass() + " : "+type);
System.out.println(type2.getClass() + " : "+type1);
System.out.println(type2.getClass() + " : "+type2.getTypeName());
System.out.println(type3.getClass() + " : "+type3);
System.out.println(testType.getClass() + " : "+testType);
System.out.println(typeMapType.getClass() + " : "+typeMapType);
}
}
输出:
[T, E]
class sun.reflect.generics.reflectiveObjects.WildcardTypeImpl : ? extends T
class sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl : T
class sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl : T[]
class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl : java.util.List<T>
class sun.reflect.generics.reflectiveObjects.TypeVariableImpl : T
class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl : java.util.Map<java.lang.String, T>
获取父类上面的泛型注解
getGenericSuperclass
class Father{ }
class Son{ }
class Person<T>{ }
class XiaoMing extends Person<Son> { }
class ZhangSan extends Person<Father>{ }
System.out.println(ParameterizedTypeUtils.get(ZhangSan.class.getGenericSuperclass(), 0) );
System.out.println(ParameterizedTypeUtils.get(XiaoMing.class.getGenericSuperclass(), 0) );
class Father
class Son
获取实现接口上面的泛型注解
getGenericInterfaces
class Father{ }
class Son{ }
interface Person<T>{ }
class XiaoMing implements Person<Son> { }
class ZhangSan implements Person<Father>{ }
System.out.println(ParameterizedTypeUtils.get(ZhangSan.class.getGenericInterfaces()[0], 0) );
System.out.println(ParameterizedTypeUtils.get(XiaoMing.class.getGenericInterfaces()[0], 0) );
class Father
class Son
不要获取未实例化对象的类的泛型,会直接得出泛型本身
例如泛型为 T, 那也只能获取到 T,不会是某个具体类型。
class A<T>{}
ParameterizedTypeUtils.get(A.class,0) // T