Java 反射:Field 类 getType 和 getGenericType 的异同


#Java 反射、泛型与注解


简介

源码:

// java.lang.reflect.Field 部分源码
public Class<?> getType() {
    return this.type;
}

public Type getGenericType() {
    return (Type)(this.getGenericSignature() != null ? this.getGenericInfo().getGenericType() : this.getType());
}
  • getType
    • 返回类型是 Class (Class 是 Type 的子类)。
    • 返回值无泛型信息
  • getGenericType
    • 返回类型是 Type
    • 若Field 涉及到泛型,返回结果中有泛型信息

示例

代码示例:

package org.example;

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
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() {

        for (Field field : DataHolder.class.getDeclaredFields()) {
            System.out.println("--------------------------");
            String filedName = field.getName();
            System.out.println("处理字段:" + filedName);
            Class<?> cls = field.getType();
            System.out.println("\t type: " + cls);
            Type genericType = field.getGenericType();
            System.out.println("\t genericType: " + genericType);
        }

    }

}

执行结果:

--------------------------
处理字段:data01
	 type: class java.lang.Object
	 genericType: T
--------------------------
处理字段:data02
	 type: class java.lang.String
	 genericType: class java.lang.String
--------------------------
处理字段:data03
	 type: int
	 genericType: int
--------------------------
处理字段:data04
	 type: interface java.util.List
	 genericType: java.util.List<T>
--------------------------
处理字段:data05
	 type: interface java.util.List
	 genericType: java.util.List<java.lang.String>
--------------------------
处理字段:data06
	 type: interface java.util.Map
	 genericType: java.util.Map<java.lang.String, java.lang.Integer>
--------------------------
处理字段:data07
	 type: class [Ljava.lang.Object;
	 genericType: T[]
--------------------------
处理字段:data08
	 type: interface java.util.List
	 genericType: java.util.List<? extends T>
--------------------------
处理字段:data09
	 type: interface java.util.List
	 genericType: java.util.List<?>

用表格整理如下:

字段信息 getType() 返回的实际类型 getType().toString getGenericType() 返回的实际类型 getGenericType().toString()
T data01 Class class java.lang.Object TypeVariableImpl T
String data02 Class class java.lang.String Class class java.lang.String
int data03 Class int Class int
List<T> data04 Class interface java.util.List ParameterizedTypeImpl java.util.List<T>
List<String> data05 Class interface java.util.List ParameterizedTypeImpl java.util.List<java.lang.String>
Map<String, Integer> data06 Class interface java.util.Map ParameterizedTypeImpl java.util.Map<java.lang.String, java.lang.Integer>
T[] data07 Class class [Ljava.lang.Object; GenericArrayTypeImpl T[]
List<? extends T> data08 Class interface java.util.List ParameterizedTypeImpl java.util.List<? extends T>
List<?> data09 Class interface java.util.List ParameterizedTypeImpl java.util.List<?>


( 本文完 )