【Spring源码分析】ResolvableType
参考
目录
文章目录
- 【Spring源码分析】ResolvableType
- 一、ParameterizedType 参数化类型,即泛型;例如:List< T>、Map< K,V>等带有参数化的对象;
- 二、GenericArrayType—— 泛型数组 泛型数组,描述的是形如:A< T>[]或T[]类型变量和原始类型
- 三、TypeVariable
- 四、WildcardType
- 五、获取父接口父类中的泛型参数,使用原生和spring ResolvableType
Type
所有类型的类型是常见的超接口Java编程语言。这些包括原始类型,参数化的类型,数组类型,类型变量和原始类型(These include raw types, parameterized types, array types, type variables and primitive types).它并不是我们平常工作中经常使用的 int、String、List、Map等数据类型,而是从Java语言角度来说,对基本类型、引用类型向上的抽象;
Type体系中类型的包括:原始类型(Class)、参数化类型(ParameterizedType)、数组类型(GenericArrayType)、类型变量(TypeVariable)、基本类型(Class);
原始类型,不仅仅包含我们平常所指的类,还包括枚举、数组、注解等;
参数化类型,就是我们平常所用到的泛型List、Map;
数组类型,并不是我们工作中所使用的数组String[] 、byte[],而是带有泛型的数组,即T[] ;
基本类型,也就是我们所说的java的基本类型,即int,float,double等
一、ParameterizedType 参数化类型,即泛型;例如:List< T>、Map< K,V>等带有参数化的对象;
作用于Class的Field上面
Type[] getActualTypeArguments(); 返回 这个 Type 类型的参数的实际类型数组。
package com.lvyuanj.standard.resolvabletypes;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* ParameterizedType 使用测试 参数化类型
*
*/
@Slf4j
public class ParameterizedTypeTest {
/**
* 1、map: 获取ParameterizedType:class java.lang.String
* 2、map: 获取ParameterizedType:class com.wangji.demo.ParameterizedTypeTest
* 3、map:getOwnerType is null
* 4、map:getRawType:interface java.util.Map
*/
private Map<String, ParameterizedTypeTest> map;
/**
* 1、set1: 获取ParameterizedType:class java.lang.String
* 2、set1:getOwnerType is null
* 3、set1:getRawType:interface java.util.Set
*/
private Set<String> set1;
/**
* 1、 clz: 获取ParameterizedType:?
* 2、 clz:getOwnerType is null
* 3、clz:getRawType:class java.lang.Class
*/
private Class<?> clz;
/**
* 1、holder: 获取ParameterizedType:class java.lang.String
* 2、holder:getOwnerType:class com.wangji.demo.ParameterizedTypeTest
* 3、holder:getRawType:class com.wangji.demo.ParameterizedTypeTest$Holder
*/
private Holder<String> holder;
/**
* 1、list: 获取ParameterizedType:class java.lang.String
* 2、list:getOwnerType is null
* 3、list:getRawType:interface java.util.List
*/
private List<String> list;
/**
* str:is not ParameterizedType
*/
private String str;
/**
* i:is not ParameterizedType
*/
private Integer i;
/**
* set:is not ParameterizedType
*/
private Set set;
/**
* aList:is not ParameterizedType
*/
private List aList;
/**
* 1、entry: 获取ParameterizedType:class java.lang.String
* 2、entry: 获取ParameterizedType:class java.lang.String
* 3、entry:getOwnerType:interface java.util.Map
* 4、entry:getRawType:interface java.util.Map$Entry
*/
private Map.Entry<String, String> entry;
static class Holder<V> {
}
public static void testParameterizedType() {
Field f = null;
try {
Field[] fields = ParameterizedTypeTest.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
f = fields[i];
if(f.getName().equals("log")){
continue;
}
System.out.println(f.getName()+" == " + f.getType());
if(f.getGenericType() instanceof ParameterizedType){
ParameterizedType parameterizedType = (ParameterizedType) f.getGenericType();
for(Type type :parameterizedType.getActualTypeArguments()){
log.info(f.getName()+": 获取ParameterizedType:"+type);
}
if(parameterizedType.getOwnerType() !=null){
log.info(f.getName()+":getOwnerType:"+parameterizedType.getOwnerType());
}else{
log.info(f.getName()+":getOwnerType is null");
}
if(parameterizedType.getRawType() !=null){
log.info(f.getName()+":getRawType:"+parameterizedType.getRawType());
}
}else{
log.info(f.getName() + ":is not ParameterizedType ");
}
}
}catch (Exception e){
log.error("error",e);
}
}
public static void main(String[] args) {
testParameterizedType();
}
}
运行结果如下:
map == interface java.util.Map
11:04:52.041 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - map: 获取ParameterizedType:class java.lang.String
11:04:52.047 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - map: 获取ParameterizedType:class com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest
11:04:52.047 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - map:getOwnerType is null
11:04:52.048 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - map:getRawType:interface java.util.Map
set1 == interface java.util.Set
11:04:52.049 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - set1: 获取ParameterizedType:class java.lang.String
11:04:52.049 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - set1:getOwnerType is null
11:04:52.049 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - set1:getRawType:interface java.util.Set
clz == class java.lang.Class
11:04:52.050 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - clz: 获取ParameterizedType:?
11:04:52.050 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - clz:getOwnerType is null
11:04:52.050 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - clz:getRawType:class java.lang.Class
holder == class com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest$Holder
11:04:52.051 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - holder: 获取ParameterizedType:class java.lang.String
11:04:52.051 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - holder:getOwnerType:class com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest
11:04:52.051 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - holder:getRawType:class com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest$Holder
list == interface java.util.List
11:04:52.051 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - list: 获取ParameterizedType:class java.lang.String
11:04:52.051 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - list:getOwnerType is null
11:04:52.052 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - list:getRawType:interface java.util.List
str == class java.lang.String
11:04:52.052 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - str:is not ParameterizedType
i == class java.lang.Integer
11:04:52.052 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - i:is not ParameterizedType
set == interface java.util.Set
11:04:52.052 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - set:is not ParameterizedType
aList == interface java.util.List
11:04:52.052 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - aList:is not ParameterizedType
entry == interface java.util.Map$Entry
11:04:52.053 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - entry: 获取ParameterizedType:class java.lang.String
11:04:52.053 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - entry: 获取ParameterizedType:class java.lang.String
11:04:52.053 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - entry:getOwnerType:interface java.util.Map
11:04:52.053 [main] INFO com.lvyuanj.standard.resolvabletypes.ParameterizedTypeTest - entry:getRawType:interface java.util.Map$Entry
二、GenericArrayType—— 泛型数组 泛型数组,描述的是形如:A< T>[]或T[]类型变量和原始类型
作用于Class的method上面
package com.lvyuanj.standard.resolvabletypes;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.*;
import java.util.List;
/**
* GenericArrayType—— 泛型数组
* 泛型数组,描述的是形如:A<T>[]或T[]类型
* is either a parameterized type or a type variable.
*/
@Slf4j
public class GenericArrayTypeTest<T> {
/**
* 含有泛型数组的才是GenericArrayType
* @param pTypeArray GenericArrayType type :java.util.List<java.lang.String>[];
* genericComponentType:java.util.List<java.lang.String>
* @param vTypeArray GenericArrayType type :T[];genericComponentType:T
* @param list ParameterizedType type :java.util.List<java.lang.String>;
* @param strings type :class [Ljava.lang.String;
* @param test type :class [Lcom.wangji.demo.GenericArrayTypeTest;
*/
public void testGenericArrayType(List<String>[] pTypeArray, T[] vTypeArray
, List<String> list, String[] strings, GenericArrayTypeTest[] test) {
}
/**
* 1、getGenericComponentType
* 返回泛型数组中元素的Type类型,即List<String>[] 中的 List<String>(ParameterizedTypeImpl)
* 、T[] 中的T(TypeVariableImpl);
* 值得注意的是,无论是几维数组,getGenericComponentType()方法都只会脱去最右边的[],返回剩下的值;
*/
public static void testGenericArrayType() {
Method[] declaredMethods = GenericArrayTypeTest.class.getDeclaredMethods();
for(Method method :declaredMethods){
if(method.getName().startsWith("main")){
continue;
}
log.info("declare Method:"+method);
/**
* 获取当前参数所有的类型信息
*/
Type[] types = method.getGenericParameterTypes();
for(Type type: types){
if(type instanceof ParameterizedType){
log.info("ParameterizedType type :"+type);
}else if(type instanceof GenericArrayType){
log.info("GenericArrayType type :"+type);
Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
/**
* 获取泛型数组中元素的类型,要注意的是:无论从左向右有几个[]并列,
* 这个方法仅仅脱去最右边的[]之后剩下的内容就作为这个方法的返回值。
*/
log.info("genericComponentType:"+genericComponentType);
}else if(type instanceof WildcardType){
log.info("WildcardType type :"+type);
}else if(type instanceof TypeVariable){
log.info("TypeVariable type :"+type);
}else {
log.info("type :"+type);
}
}
}
}
public static void main(String[] args) {
testGenericArrayType();
}
}
运行结果:
11:31:23.706 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - declare Method:public void com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest.testGenericArrayType(java.util.List[],java.lang.Object[],java.util.List,java.lang.String[],com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest[])
11:31:23.718 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - GenericArrayType type :java.util.List<java.lang.String>[]
11:31:23.718 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - genericComponentType:java.util.List<java.lang.String>
11:31:23.718 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - GenericArrayType type :T[]
11:31:23.719 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - genericComponentType:T
11:31:23.719 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - ParameterizedType type :java.util.List<java.lang.String>
11:31:23.719 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - type :class [Ljava.lang.String;
11:31:23.719 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - type :class [Lcom.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest;
11:31:23.719 [main] INFO com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest - declare Method:public static void com.lvyuanj.standard.resolvabletypes.GenericArrayTypeTest.testGenericArrayType()
三、TypeVariable
TypeVariable Interface TypeVariable< D extends GenericDeclaration>
泛型的类型变量,指的是List< T>、Map< K,V>中的T,K,V等值,实际的Java类型是TypeVariableImpl(TypeVariable的子类;此外,还可以对类型变量加上extend限定,这样会有类型变量对应的上限;值得注意的是,类型变量的上限可以为多个,必须使用&符号相连接,例如 List< T extends Number & Serializable>;其中,& 后必须为接口;
1、Type[] getBounds() 类型对应的上限,默认为Object
2、D getGenericDeclaration() 获取声明该类型变量实体,也就是TypeVariableTest< T>中的TypeVariableTest
3、String getName() 获取类型变量在源码中定义的名称;
package com.lvyuanj.standard.resolvabletypes;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.lang.reflect.*;
import java.util.List;
/**
* Interface TypeVariable<D extends GenericDeclaration> ,
* D - the type of generic declaration that declared the underlying type variable.
* 类型变量是类型变量的公共超接口。类型变量是第一次使用反射方法创建的,如在这个包中指定的。
* 如果类型变量T由类型(即类、接口或注释类型)T引用,并且T由第n个封闭类T(参见JLS.1.2)来声明,
* 那么T的创建需要T的第i个包围类的分辨率(参见JVMS 5),对于i=0到n,包含。创建类型变量不能导致其边界的创建。
* 重复创建类型变量没有任何效果。
* <p>
* 可以在运行时实例化多个对象以表示给定的类型变量。即使类型变量只创建一次,
* 但这并不意味着缓存表示类型变量的实例的任何要求。
* 但是,表示一个类型变量的所有实例必须是相等的()。因此,类型变量的用户不能依赖实现该接口的类实例的标识。
* <p>
* 泛型的类型变量,指的是List<T>、Map<K,V>中的T,K,V等值,实际的Java类型是TypeVariableImpl
* (TypeVariable的子类);
* 此外,还可以对类型变量加上extend限定,这样会有类型变量对应的上限;值得注意的是,类型变量的上限可以为多个,
* 必须使用&符号相连接,例如 List<T extends Number & Serializable>;其中,& 后必须为接口;
* 没有指定的话 ,V 的 上边界 属于 Object
*/
@Slf4j
public class TypeVariableTest<T extends Number & Serializable, V> {
/**
* TypeVariable
*/
private T key;
/**
* TypeVariable
*/
private V value;
/**
* GenericArrayType V[]-> V TypeVariable 两种混合起来了
*/
private V[] values;
/**
* 原始类型,不仅仅包含我们平常所指的类,还包括枚举、数组、注解等;
* 基本类型,也就是我们所说的java的基本类型,即int,float,double等
*/
private String str;
/**
* 获取ParameterizedType List<T> -> T TypeVariable 两种混合起来了
*/
private List<T> tList;
/**
* 从这个例子中可以看出来各种类型之间是相互在使用的
* TypeVariable<D extends GenericDeclaration>
* GenericDeclaration All Known Implementing Classes: Class, Constructor, Method
*/
public static void testTypeVariableTest() {
Field f = null;
try {
Field[] fields = TypeVariableTest.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
f = fields[i];
if (f.getName().equals("log")) {
continue;
}
log.info("begin ******当前field:" + f.getName() + " *************************");
if (f.getGenericType() instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) f.getGenericType();
for (Type type : parameterizedType.getActualTypeArguments()) {
log.info(f.getName() + ": 获取ParameterizedType:" + type);
if (type instanceof TypeVariable) {
printTypeVariable(f.getName(), (TypeVariable) type);
}
}
if (parameterizedType.getOwnerType() != null) {
log.info(f.getName() + ":getOwnerType:" + parameterizedType.getOwnerType());
} else {
log.info(f.getName() + ":getOwnerType is null");
}
if (parameterizedType.getRawType() != null) {
log.info(f.getName() + ":getRawType:" + parameterizedType.getRawType());
}
} else if (f.getGenericType() instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) f.getGenericType();
log.info("GenericArrayType type :" + genericArrayType);
Type genericComponentType = genericArrayType.getGenericComponentType();
if (genericComponentType instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) genericComponentType;
printTypeVariable(f.getName(), typeVariable);
}
} else if (f.getGenericType() instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) f.getGenericType();
printTypeVariable(f.getName(), typeVariable);
} else {
log.info("type :" + f.getGenericType());
}
log.info("end ******当前field:" + f.getName() + " *************************");
}
} catch (Exception e) {
log.error("error", e);
}
}
/**
* 1、Type[] getBounds() 类型对应的上限,默认为Object
* 2、D getGenericDeclaration() 获取声明该类型变量实体,也就是TypeVariableTest<T>中的TypeVariableTest
* 3、String getName() 获取类型变量在源码中定义的名称;
*
* @param fieldName
* @param typeVariable
*/
private static void printTypeVariable(String fieldName, TypeVariable typeVariable) {
for (Type type : typeVariable.getBounds()) {
log.info(fieldName + ": TypeVariable getBounds " + type);
}
log.info("定义Class getGenericDeclaration: " + typeVariable.getGenericDeclaration());
log.info("getName: " + typeVariable.getName());
}
public static void main(String[] args) {
testTypeVariableTest();
}
}
运行结果
11:39:48.697 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - begin ******当前field:key *************************
11:39:48.708 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - key: TypeVariable getBounds class java.lang.Number
11:39:48.709 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - key: TypeVariable getBounds interface java.io.Serializable
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - 定义Class getGenericDeclaration: class com.lvyuanj.standard.resolvabletypes.TypeVariableTest
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - getName: T
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - end ******当前field:key *************************
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - begin ******当前field:value *************************
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - value: TypeVariable getBounds class java.lang.Object
11:39:48.711 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - 定义Class getGenericDeclaration: class com.lvyuanj.standard.resolvabletypes.TypeVariableTest
11:39:48.712 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - getName: V
11:39:48.712 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - end ******当前field:value *************************
11:39:48.712 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - begin ******当前field:values *************************
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - GenericArrayType type :V[]
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - values: TypeVariable getBounds class java.lang.Object
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - 定义Class getGenericDeclaration: class com.lvyuanj.standard.resolvabletypes.TypeVariableTest
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - getName: V
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - end ******当前field:values *************************
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - begin ******当前field:str *************************
11:39:48.713 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - type :class java.lang.String
11:39:48.714 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - end ******当前field:str *************************
11:39:48.716 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - begin ******当前field:tList *************************
11:39:48.717 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - tList: 获取ParameterizedType:T
11:39:48.717 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - tList: TypeVariable getBounds class java.lang.Number
11:39:48.717 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - tList: TypeVariable getBounds interface java.io.Serializable
11:39:48.717 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - 定义Class getGenericDeclaration: class com.lvyuanj.standard.resolvabletypes.TypeVariableTest
11:39:48.717 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - getName: T
11:39:48.718 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - tList:getOwnerType is null
11:39:48.718 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - tList:getRawType:interface java.util.List
11:39:48.718 [main] INFO com.lvyuanj.standard.resolvabletypes.TypeVariableTest - end ******当前field:tList *************************
四、WildcardType
WildcardType represents a wildcard type expression, such as ?, ? extends Number, or ? super Integer.
通配符表达式,或泛型表达式,它虽然是Type的一个子接口,但并不是Java类型中的一种,表示的仅仅是类似 ? extends T、? super K这样的通配符表达式。 ?—通配符表达式,表示通配符泛型,但是WildcardType并不属于Java-Type中的一钟;例如:List< ? extends Number> 和 List< ? super Integer>;
1、Type[] getUpperBounds(); //获得泛型表达式上界(上限) 获取泛型变量的上边界(extends)
2、Type[] getLowerBounds(); //获得泛型表达式下界(下限) 获取泛型变量的下边界(super)
package com.lvyuanj.standard.resolvabletypes;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.*;
import java.util.List;
/**
* WildcardType represents a wildcard type expression, such as ?, ? extends Number,
* or ? super Integer.
* 通配符表达式,或泛型表达式,它虽然是Type的一个子接口,但并不是Java类型中的一种,表示的仅仅是类似
* ? extends T、? super K这样的通配符表达式。
* ?---通配符表达式,表示通配符泛型,但是WildcardType并不属于Java-Type中的一钟;
* 例如:List< ? extends Number> 和 List< ? super Integer>;
* 1、Type[] getUpperBounds(); //获得泛型表达式上界(上限) 获取泛型变量的上边界(extends)
* 2、Type[] getLowerBounds(); //获得泛型表达式下界(下限) 获取泛型变量的下边界(super)
*
*/
@Slf4j
public class WildcardTypeTest {
/**
* 1、 a: 获取ParameterizedType:? extends java.lang.Number
* 2、上界:class java.lang.Number
*/
private List< ? extends Number> a;
/**
* b: 获取ParameterizedType:? super java.lang.String
* 上届:class java.lang.Object
* 下届:class java.lang.String
*/
private List< ? super String> b;
/**
* c: 获取ParameterizedType:class java.lang.String
*/
private List<String> c;
/**
* aClass: 获取ParameterizedType:?
* 上届:class java.lang.Object
*/
private Class<?> aClass;
private String wangji;
/**
* 多种数据进行混合
*/
public static void testWildcardType() {
Field f = null;
try {
Field[] fields = WildcardTypeTest.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
f = fields[i];
if (f.getName().equals("log")) {
continue;
}
log.info("begin ******当前field:" + f.getName() + " *************************");
if (f.getGenericType() instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) f.getGenericType();
for (Type type : parameterizedType.getActualTypeArguments()) {
log.info(f.getName() + ": 获取ParameterizedType:" + type);
if (type instanceof WildcardType) {
printWildcardType((WildcardType) type);
}
}
} else if (f.getGenericType() instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) f.getGenericType();
log.info("GenericArrayType type :" + genericArrayType);
Type genericComponentType = genericArrayType.getGenericComponentType();
if (genericComponentType instanceof WildcardType) {
printWildcardType((WildcardType) genericComponentType);
}
} else if (f.getGenericType() instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) f.getGenericType();
log.info("typeVariable:" + typeVariable);
} else {
log.info("type :" + f.getGenericType());
if (f.getGenericType() instanceof WildcardType) {
printWildcardType((WildcardType) f.getGenericType());
}
}
log.info("end ******当前field:" + f.getName() + " *************************");
}
} catch (Exception e) {
log.error("error", e);
}
}
private static void printWildcardType(WildcardType wildcardType) {
for (Type type : wildcardType.getUpperBounds()) {
log.info("上界:" + type);
}
for (Type type : wildcardType.getLowerBounds()) {
log.info("下界:" + type);
}
}
public static void main(String[] args) {
testWildcardType();
}
}
运行结果:
11:48:56.258 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - begin ******当前field:a *************************
11:48:56.287 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - a: 获取ParameterizedType:? extends java.lang.Number
11:48:56.287 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - 上界:class java.lang.Number
11:48:56.287 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - end ******当前field:a *************************
11:48:56.288 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - begin ******当前field:b *************************
11:48:56.288 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - b: 获取ParameterizedType:? super java.lang.String
11:48:56.288 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - 上界:class java.lang.Object
11:48:56.288 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - 下界:class java.lang.String
11:48:56.288 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - end ******当前field:b *************************
11:48:56.289 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - begin ******当前field:c *************************
11:48:56.289 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - c: 获取ParameterizedType:class java.lang.String
11:48:56.289 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - end ******当前field:c *************************
11:48:56.289 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - begin ******当前field:aClass *************************
11:48:56.290 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - aClass: 获取ParameterizedType:?
11:48:56.290 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - 上界:class java.lang.Object
11:48:56.291 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - end ******当前field:aClass *************************
11:48:56.291 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - begin ******当前field:wangji *************************
11:48:56.292 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - type :class java.lang.String
11:48:56.292 [main] INFO com.lvyuanj.standard.resolvabletypes.WildcardTypeTest - end ******当前field:wangji *************************
五、获取父接口父类中的泛型参数,使用原生和spring ResolvableType
public class Parent<T> {
}
public interface IParent<T> {
}
使用原生和spring ResolvableType的比较