介绍
java泛型使用“擦拭法” , 导致java的泛型不能直接获取到自身声明的泛型类型
使用的测试类
public class Main {
static class C {}
static class A<T> {
protected List<T> list = new ArrayList<>();
protected A<C> a = new A<>();
protected A<T> aa = new A<>();
}
static class B extends A<C> {
protected List<C> list = new ArrayList<>();
}
static class BB<T> extends A<T> {}
interface D<T> {}
static class E implements D<C> {}
interface EE extends D<C> {}
}
可以获取泛型的样例
获取父类上的泛型(必须明确指定)
public static void main(String[] args) throws Exception {
Type genericSuperclass = B.class.getGenericSuperclass();
Class type = (Class) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
com.wyj.core.Main$C
获取继承接口上的泛型(必须明确指定)
public static void main(String[] args) throws Exception {
Type[] genericInterfaces = E.class.getGenericInterfaces();
Class type = (Class) ((ParameterizedType) genericInterfaces[0]).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
com.wyj.core.Main$C
获取类中集合字段的泛型(必须明确指定)
public static void main(String[] args) throws Exception {
Field list = B.class.getDeclaredField("list");
Type genericType = list.getGenericType();
Class type = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
com.wyj.core.Main$C
获取类中某个字段的泛型(必须明确指定)
public static void main(String[] args) throws Exception {
Field list = A.class.getDeclaredField("a");
Type genericType = list.getGenericType();
Class type = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
com.wyj.core.Main$C
不可获取泛型的样例
获取类上的泛型(没有明确指定)
获取A上的泛型
public static void main(String[] args) throws Exception {
Type genericSuperclass = A.class.getGenericSuperclass();
Class type = (Class) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
Exception in thread “main” java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.wyj.core.Main.main(Main.java:17)
获取BB上的泛型
public static void main(String[] args) throws Exception {
Type genericSuperclass = BB.class.getGenericSuperclass();
Class type = (Class) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
Exception in thread “main” java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class at com.wyj.core.Main.main(Main.java:25)
获取字段上的泛型(没有明确指定)
获取集合字段上的泛型
public static void main(String[] args) throws Exception {
Field list = A.class.getDeclaredField("list");
Type genericType = list.getGenericType();
Class type = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
Exception in thread “main” java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class at com.wyj.core.Main.main(Main.java:39)
获取其他字段上的泛型
public static void main(String[] args) throws Exception {
Field list = A.class.getDeclaredField("aa");
Type genericType = list.getGenericType();
Class type = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];
System.out.println(type.getName());
}
输出结果:
Exception in thread “main” java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class at com.wyj.core.Main.main(Main.java:43)