Java 反射:Type 、ParameterizedType、TypeVariable 、WildcardType、GenericArrayType


#Java 反射、泛型与注解


ParameterizedType、TypeVariable、WildcardType、GenericArrayType 都实现了 Type 接口。

  • ParameterizedType : 泛型类型。比如 List<String>List<T>List<? extends T>
  • TypeVariable : 类型变量。比如 T
  • WildcardType : 通配符类型。例如: ?? super T? extends T
  • GenericArrayType 类型数组。比如 T[]

示例1

import lombok.Data;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class TestJava {

    @Data
    public static class DataHolder<T> {
        private T data01;
        private String data02;
        private int data03;
        private List<T> data04;
        private List<String> data05;
        private Map<String, Integer> data06;
        private T[] data07;
        private List<? extends T> data08;
        private List<?> data09;
    }

    @Test
    public void test_01() {

        for (Field field : DataHolder.class.getDeclaredFields()) {
            System.out.println("--------------------------");
            System.out.println("处理字段:" + field.getName());

            Type genericType = field.getGenericType();

            System.out.println("genericType: " + field.getGenericType());

            if (genericType == int.class) {
                System.out.println("是 int");
            }

            if (genericType instanceof ParameterizedType) {
                System.out.println("是 ParameterizedType");
                ParameterizedType parameterizedType = (ParameterizedType) genericType;
                System.out.println("getRawType: " + parameterizedType.getRawType());
                System.out.println("getOwnerType: " + parameterizedType.getOwnerType());
                System.out.println("getActualTypeArguments: "
                        + Arrays.toString(parameterizedType.getActualTypeArguments()));
                for (Type type : parameterizedType.getActualTypeArguments()) {
                    System.out.println("\t类型参数: " + type);
                    if (type instanceof WildcardType) {
                        WildcardType wildcardType = (WildcardType) type;
                        System.out.println("\t\t类型参数是 wildcardType: " + wildcardType);
                    }
                    if (type instanceof TypeVariable) {
                        TypeVariable typeVariable = (TypeVariable) type;
                        System.out.println("\t\t类型参数是 typeVariable: " + typeVariable);
                    }
                }
            }

            if (genericType instanceof TypeVariable) {
                System.out.println("是 TypeVariable");
                TypeVariable typeVariable = (TypeVariable) genericType;
                System.out.println("getName: " + typeVariable.getName());
                System.out.println("getTypeName: " + typeVariable.getTypeName());
                System.out.println("getBounds: " + Arrays.toString(typeVariable.getBounds()));
            }

            if (genericType instanceof WildcardType) {
                System.out.println("是 TypeVariable");
                WildcardType wildcardType = (WildcardType) genericType;
                System.out.println("getTypeName: " + wildcardType.getTypeName());
                System.out.println("getLowerBounds: " + wildcardType.getLowerBounds());
                System.out.println("getUpperBounds: " + wildcardType.getUpperBounds());
            }

            if (genericType instanceof GenericArrayType) {
                System.out.println("是 GenericArrayType");
                GenericArrayType genericArrayType = (GenericArrayType) field.getGenericType();
                System.out.println("getTypeName: " + genericArrayType.getTypeName());
                System.out.println("getLowerBounds: " + genericArrayType.getGenericComponentType());
            }

        }

    }

}

运行结果:

--------------------------
处理字段:data01
genericType: T
是 TypeVariable
getName: T
getTypeName: T
getBounds: [class java.lang.Object]
--------------------------
处理字段:data02
genericType: class java.lang.String
--------------------------
处理字段:data03
genericType: int
是 int
--------------------------
处理字段:data04
genericType: java.util.List<T>
是 ParameterizedType
getRawType: interface java.util.List
getOwnerType: null
getActualTypeArguments: [T]
	类型参数: T
		类型参数是 typeVariable: T
--------------------------
处理字段:data05
genericType: java.util.List<java.lang.String>
是 ParameterizedType
getRawType: interface java.util.List
getOwnerType: null
getActualTypeArguments: [class java.lang.String]
	类型参数: class java.lang.String
--------------------------
处理字段:data06
genericType: java.util.Map<java.lang.String, java.lang.Integer>
是 ParameterizedType
getRawType: interface java.util.Map
getOwnerType: null
getActualTypeArguments: [class java.lang.String, class java.lang.Integer]
	类型参数: class java.lang.String
	类型参数: class java.lang.Integer
--------------------------
处理字段:data07
genericType: T[]
是 GenericArrayType
getTypeName: T[]
getLowerBounds: T
--------------------------
处理字段:data08
genericType: java.util.List<? extends T>
是 ParameterizedType
getRawType: interface java.util.List
getOwnerType: null
getActualTypeArguments: [? extends T]
	类型参数: ? extends T
		类型参数是 wildcardType: ? extends T
--------------------------
处理字段:data09
genericType: java.util.List<?>
是 ParameterizedType
getRawType: interface java.util.List
getOwnerType: null
getActualTypeArguments: [?]
	类型参数: ?
		类型参数是 wildcardType: ?


( 本文完 )