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)

Java 中,由于泛型的类型信息在运行时会被擦除,所以无法直接通过泛型参数获取泛型class。不过,你可以通过一些技巧来获取泛型class。以下是两种常用的方法: 方法一:通过构造函数参数获取 ```java import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class GenericClass<T> { private Class<T> clazz; public GenericClass() { Type type = getClass().getGenericSuperclass(); ParameterizedType parameterizedType = (ParameterizedType) type; this.clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0]; } public Class<T> getGenericType() { return clazz; } } ``` 你可以通过继承 `GenericClass` 并指定具体的泛型类型来获取泛型class。例如: ```java public class MyClass extends GenericClass<String> { // ... } public class Main { public static void main(String[] args) { MyClass myClass = new MyClass(); Class<String> genericType = myClass.getGenericType(); System.out.println(genericType); // 输出:class java.lang.String } } ``` 方法二:通过传入 Class 对象获取 ```java public class GenericClass<T> { private Class<T> clazz; public GenericClass(Class<T> clazz) { this.clazz = clazz; } public Class<T> getGenericType() { return clazz; } } ``` 你可以直接在创建 `GenericClass` 对象时传入泛型class。例如: ```java public class Main { public static void main(String[] args) { GenericClass<String> genericClass = new GenericClass<>(String.class); Class<String> genericType = genericClass.getGenericType(); System.out.println(genericType); // 输出:class java.lang.String } } ``` 这两种方法都可以在运行时获取泛型class。请根据你的需求选择合适的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值