Java使用反射获取list泛型过程浅析

通过属性来获取泛型的类型

Field[] fields = bean.getClass().getDeclaredFields();
for(Field f : fields){
        f.setAccessible(true);
        if(f.getType() == java.util.List.class){
                // 如果是List类型,得到其Generic的类型  
                Type genericType = f.getGenericType(); 
                if(genericType == null) continue;  
                // 如果是泛型参数的类型   
                if(genericType instanceof ParameterizedType){   
                        ParameterizedType pt = (ParameterizedType) genericType;
                        //得到泛型里的class类型对象  
                        Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0]; 
                }   
        }

通过class对象来获取泛型类型

Type type = getClass().getGenericSuperclass();
                System.err.println("generic super class type:" + type);
                Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
                //trueType就是泛型的真实类型

一、getSuperclass 返回直接继承的父类(由于编译擦除,没有显示泛型参数)

二、getGenericSuperclass 返回直接继承的父类(包含泛型参数)

返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。

如果超类是参数化类型,则返回的 Type 对象必须准确反映源代码中所使用的实际类型参数。如果以前未曾创建表示超类的参数化类型,则创建这个类型。有关参数化类型创建过程的语义,请参阅 ParameterizedType 声明。

如果此 Class 表示 Object 类、接口、基本类型或 void,则返回 null。

如果此对象表示一个数组类,则返回表示 Object 类的 Class 对象。

列如

package cn.test;
public class Test {
    public static void main(String[] args) {
        System.out.println("Student.class.getSuperclass()\t"                             + Student.class.getSuperclass());
        System.out.println("Student.class.getGenericSuperclass()\t"                            + Student.class.getGenericSuperclass());
        System.out.println("Test.class.getSuperclass()\t"                             + Test.class.getSuperclass());
        System.out.println("Test.class.getGenericSuperclass()\t"                            + Test.class.getGenericSuperclass());
        System.out.println("Object.class.getGenericSuperclass()\t"                             + Object.class.getGenericSuperclass());
        System.out.println("Object.class.getSuperclass()\t"                             + Object.class.getSuperclass());
        System.out.println("void.class.getSuperclass()\t"                             + void.class.getSuperclass());
        System.out.println("void.class.getGenericSuperclass()\t"                             + void.class.getGenericSuperclass());
        System.out.println("int[].class.getSuperclass()\t"                             + int[].class.getSuperclass());
        System.out.println("int[].class.getGenericSuperclass()\t"                             + int[].class.getGenericSuperclass());
    }
}
class Person<T> {
}
class Student extends Person<Test> {
}

输出结果:

Student.class.getSuperclass() class cn.test.Person
Student.class.getGenericSuperclass() cn.test.Person<cn.test.Test>
Test.class.getSuperclass() class java.lang.Object
Test.class.getGenericSuperclass() class java.lang.Object
Object.class.getGenericSuperclass() null
Object.class.getSuperclass() null
void.class.getSuperclass() null
void.class.getGenericSuperclass() null
int[].class.getSuperclass() class java.lang.Object
int[].class.getGenericSuperclass() class java.lang.Object

原文地址:https://blog.csdn.net/Wis57/article/details/129186133