java.lang.Object
cn.wjybxx.base.reflect.TypeParameterFinder
泛型参数具体类型查找器。
哪些泛型参数可以被找到?
需要满足下面两个条件:
1.指定泛型参数是在该对象的类所在的类层次的上层定义的。
2.在定义该泛型参数的类/接口的下层被具体化了。
举个栗子:
在io.netty.handler.codec.MessageToMessageDecoder中定义了泛型参数I,
而其子类 io.netty.handler.codec.http.HttpContentDecoder将其指定为HttpObject,
那么可以通过HttpContentDecoder的实例查找到MessageToMessageDecoder上的泛型参数I为HttpObject类型。
反面栗子:
在List中定义了泛型参数E,
在ArrayList中声明了新的泛型参数E,并将List中的E指定为新声明的E(这是两个泛型参数)。
那么无法通过ArrayList的实例查找到List上泛型参数E的具体类型的。
该类对Netty的泛型参数查找进行了增强,Netty自带的查找只支持超类中查找,这里进行适配增强,以支持查找接口中声明的泛型参数。
- 作者:
- wjybxx date 2023/4/1
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static <T> Class<?> findTypeParameter(T instance, Class<? super T> superClazzOrInterface, String typeParamName) 从instance所属的类开始,查找在superClazzOrInterfaced定义的泛型参数typeParamName的具体类型 (该方法更安全)static <T> Class<?> findTypeParameterUnsafe(Class<T> thisClass, Class<? super T> superClazzOrInterface, String typeParamName) 从指定类开始查找在superClazzOrInterfaced定义的泛型参数typeParamName的具体类型。
-
构造器详细资料
-
TypeParameterFinder
public TypeParameterFinder()
-
-
方法详细资料
-
findTypeParameter
public static <T> Class<?> findTypeParameter(T instance, Class<? super T> superClazzOrInterface, String typeParamName) 从instance所属的类开始,查找在superClazzOrInterfaced定义的泛型参数typeParamName的具体类型 (该方法更安全)- 类型参数:
T- 约束必须有继承关系或实现关系- 参数:
instance- 实例对象superClazzOrInterface- 声明泛型参数typeParamName的类,class或interfacetypeParamName- 泛型参数名字- 返回:
- 如果定义的泛型存在,则返回对应的泛型clazz
-
findTypeParameterUnsafe
public static <T> Class<?> findTypeParameterUnsafe(Class<T> thisClass, Class<? super T> superClazzOrInterface, String typeParamName) 从指定类开始查找在superClazzOrInterfaced定义的泛型参数typeParamName的具体类型。 请优先使用findTypeParameter(Object, Class, String)。- 类型参数:
T- 约束必须有继承关系或实现关系- 参数:
thisClass- 查找起始类,注意最好是this.getClass()获取到的class对象。superClazzOrInterface- 声明泛型参数typeParamName的类,class或interfacetypeParamName- 泛型参数名字- 返回:
- 如果定义的泛型存在,则返回对应的泛型clazz
-