java获取泛型的class

介绍

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值