Package org.miaixz.bus.core.xyz
Class TypeKit
java.lang.Object
org.miaixz.bus.core.xyz.TypeKit
针对
Type 的工具类封装
最主要功能包括:
1. 获取方法的参数和返回值类型(包括Type和Class) 2. 获取泛型参数类型(包括对象的泛型参数或集合元素的泛型类型)
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TypegetActualType(Type type, Field field) 获得泛型字段对应的泛型实际类型,如果此变量没有对应的实际类型,返回nullstatic TypegetActualType(Type type, ParameterizedType parameterizedType) 获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理复杂的泛型化对象,类似于Map<User, Key<Long>>static TypegetActualType(Type type, Type typeVariable) 获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理:static Type[]getActualTypes(Type type, Type... typeVariables) 获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回nullstatic Class<?> 获得Field对应的原始类static Class<?> 获得Type对应的原始类static TypegetClosestParentType(Class<?> cls, Class<?> superClass) 获取与superClass指定的超类最接近的父类static TypegetFieldType(Class<?> clazz, String fieldName) 获得字段的泛型类型static Class<?> getFirstParamClass(Method method) 获取方法的第一个参数类static TypegetFirstParamType(Method method) 获取方法的第一个参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypesstatic ParameterizedType[]getGenerics(Class<?> clazz) 获取指定类所有泛型父类和泛型接口 指定类及其所有的泛型父类 指定类实现的直接泛型接口static Type[]getImplicitBounds(TypeVariable<?> typeVariable) 如果TypeVariable.getBounds()返回一个空数组, 则返回一个包含Object唯一类型的数组.static Type[]getImplicitUpperBounds(WildcardType wildcardType) 如果WildcardType.getUpperBounds()返回一个空数组, 则返回一个包含Object唯一值的数组否则, 它将返回传递给normalizeUpperBounds(java.lang.reflect.Type[])的WildcardType.getUpperBounds()的结果static Class<?> getParamClass(Method method, int index) 获取方法的参数类static Class<?>[]getParamClasses(Method method) 解析方法的参数类型列表 依赖jre\lib\rt.jarstatic TypegetParamType(Method method, int index) 获取方法的参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypesstatic Type[]getParamTypes(Method method) 获取方法的参数类型列表 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypesstatic Class<?> getReturnClass(Method method) 解析方法的返回类型类列表static TypegetReturnType(Method method) 获取方法的返回值类型 获取方法的GenericReturnTypestatic Type获取字段对应的Type类型 方法优先获取GenericType,获取不到则获取Typestatic TypegetTypeArgument(Type type) 获得给定类的第一个泛型参数static TypegetTypeArgument(Type type, int index) 获得给定类的泛型参数static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回类的类型参数的映射static Map<TypeVariable<?>, Type> 检索此参数化类型的所有类型参数,包括所有者层次结构参数static Map<TypeVariable<?>, Type> getTypeArguments(ParameterizedType parameterizedType, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回参数化类型的类型参数的映射static Type[]getTypeArguments(Type type) 获得指定类型中所有泛型参数类型,例如:static Map<TypeVariable<?>, Type> getTypeArguments(Type type, Class<?> toClass) 获取基于子类型的类/接口的类型参数static Map<TypeVariable<?>, Type> getTypeArguments(Type type, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回type的类型参数的映射getTypeMap(Class<?> clazz) 获取泛型变量和泛型实际类型的对应关系Map 例如:static booleanhasTypeVariable(Type... types) 指定泛型数组中是否含有泛型变量static booleanisAbstract(Class<?> clazz) 是否为抽象类static booleanisAbstractOrInterface(Class<?> clazz) 是抽象类或者接口static boolean是否为 数组 class 类型static booleanisArrayType(Type type) 了解指定的类型是否表示数组类型static booleanisAssignable(Type type, Type toType) 检查subject类型是否可以按照Java泛型规则隐式转换为目标类型.static boolean是否为基本类型 1.static booleanisCollection(Class<?> clazz) 是否为 Collection class 类型static booleanisIterable(Class<?> clazz) 是否为 Iterable class 类型static booleanisJavaBean(Class<?> clazz) 是否为标准的类 这个类必须:static boolean判断一个类是JDK 自带的类型 jdk 自带的类,classLoader 是为空的static boolean是否为 map class 类型static boolean是否未知类型 type为null或者TypeVariable都视为未知类型static Type[]normalizeUpperBounds(Type[] bounds) 该方法在类型变量类型和通配符类型中去除冗余的上界类型static ParameterizedTypetoParameterizedType(Type type) static ParameterizedTypetoParameterizedType(Type type, int interfaceIndex)
-
Constructor Details
-
TypeKit
public TypeKit()
-
-
Method Details
-
isMap
是否为 map class 类型- Parameters:
clazz- 对象类型- Returns:
- 是否为 map class
-
isArray
是否为 数组 class 类型- Parameters:
clazz- 对象类型- Returns:
- 是否为 数组 class
-
isCollection
是否为 Collection class 类型- Parameters:
clazz- 对象类型- Returns:
- 是否为 Collection class
-
isIterable
是否为 Iterable class 类型- Parameters:
clazz- 对象类型- Returns:
- 是否为 数组 class
-
isBase
是否为基本类型 1. 8大基本类型 2. 常见的值类型- Parameters:
clazz- 对象类型- Returns:
- 是否为基本类型
-
isAbstract
是否为抽象类- Parameters:
clazz- 类- Returns:
- 是否为抽象类
-
isAbstractOrInterface
是抽象类或者接口- Parameters:
clazz- 类信息- Returns:
- 是否
-
isJavaBean
是否为标准的类 这个类必须:0、不为 null 1、非接口 2、非抽象类 3、非Enum枚举 4、非数组 5、非注解 6、非原始类型(int, long等) 7、非集合 Iterable 8、非 Map.clas 9、非 JVM 生成类
- Parameters:
clazz- 类- Returns:
- 是否为标准类
-
isJdk
判断一个类是JDK 自带的类型 jdk 自带的类,classLoader 是为空的- Parameters:
clazz- 类- Returns:
- 是否为 java 类
-
isAssignable
检查subject类型是否可以按照Java泛型规则隐式转换为目标类型. 如果这两种类型都是Class对象, 则该方法返回ClassKit.isAssignable(Class, Class)的结果- Parameters:
type- 要分配给目标类型的主题类型toType- 目标类型- Returns:
- 如果
type可赋值给toType,则true.
-
isArrayType
了解指定的类型是否表示数组类型- Parameters:
type- 要检查的类型- Returns:
- 如果
type是数组类或GenericArrayType,则为true
-
isUnknown
是否未知类型 type为null或者TypeVariable都视为未知类型- Parameters:
type- Type类型- Returns:
- 是否未知类型
-
getClass
获得Type对应的原始类- Parameters:
type-Type- Returns:
- 原始类,如果无法获取原始类,返回
null
-
getType
获取字段对应的Type类型 方法优先获取GenericType,获取不到则获取Type- Parameters:
field- 字段- Returns:
Type,可能为null
-
getFieldType
获得字段的泛型类型- Parameters:
clazz- Bean类fieldName- 字段名- Returns:
- 字段的泛型类型
-
getClass
获得Field对应的原始类- Parameters:
field-Field- Returns:
- 原始类,如果无法获取原始类,返回
null
-
getFirstParamType
获取方法的第一个参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes- Parameters:
method- 方法- Returns:
Type,可能为null
-
getFirstParamClass
获取方法的第一个参数类- Parameters:
method- 方法- Returns:
- 第一个参数类型,可能为
null
-
getParamType
获取方法的参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes- Parameters:
method- 方法index- 第几个参数的索引,从0开始计数- Returns:
Type,可能为null
-
getParamClass
获取方法的参数类- Parameters:
method- 方法index- 第几个参数的索引,从0开始计数- Returns:
- 参数类,可能为
null
-
getParamTypes
获取方法的参数类型列表 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes- Parameters:
method- 方法- Returns:
Type列表,可能为null- See Also:
-
getParamClasses
解析方法的参数类型列表 依赖jre\lib\rt.jar- Parameters:
method- t方法- Returns:
- 参数类型类列表
- See Also:
-
getReturnType
获取方法的返回值类型 获取方法的GenericReturnType- Parameters:
method- 方法- Returns:
Type,可能为null- See Also:
-
getReturnClass
解析方法的返回类型类列表- Parameters:
method- 方法- Returns:
- 返回值类型的类
- See Also:
-
getTypeArgument
获得给定类的第一个泛型参数- Parameters:
type- 被检查的类型,必须是已经确定泛型类型的类型- Returns:
Type,可能为null
-
getTypeArgument
获得给定类的泛型参数- Parameters:
type- 被检查的类型,必须是已经确定泛型类型的类index- 泛型类型的索引号,即第几个泛型类型- Returns:
Type
-
getTypeArguments
获得指定类型中所有泛型参数类型,例如:class A<T> class B extends A<String>
通过此方法,传入B.class即可得到String
- Parameters:
type- 指定类型- Returns:
- 所有泛型参数类型
-
getTypeArguments
检索此参数化类型的所有类型参数,包括所有者层次结构参数- Parameters:
type- 指定要从中获取参数的主题参数化类型- Returns:
- 带有类型参数的
Map.
-
getTypeArguments
获取基于子类型的类/接口的类型参数- Parameters:
type- 用于确定toClass的类型参数的类型toClass- 类型参数将根据子类型type确定的类- Returns:
- 带有类型参数的
Map
-
getTypeArguments
public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回type的类型参数的映射- Parameters:
type- 问题类型toClass- 类subtypeVarAssigns- 带有类型变量的映射- Returns:
- 带有类型参数的
Map
-
getTypeArguments
public static Map<TypeVariable<?>,Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回类的类型参数的映射- Parameters:
cls- 要确定类型参数的类toClass- 上下文类subtypeVarAssigns- 带有类型变量的映射- Returns:
- 带有类型参数的
Map
-
getTypeArguments
public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType parameterizedType, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) 在toClass的上下文中返回参数化类型的类型参数的映射- Parameters:
parameterizedType- 参数化类型toClass- 类subtypeVarAssigns- 带有类型变量的映射- Returns:
- 带有类型参数的
Map
-
getClosestParentType
获取与superClass指定的超类最接近的父类- Parameters:
cls- 类superClass- 超类- Returns:
- 父类型
-
toParameterizedType
将Type转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型 一般用于获取泛型参数具体的参数类型,例如:class A<T> class B extends A<String>
通过此方法,传入B.class即可得到B
ParameterizedType,从而获取到String- Parameters:
type-Type- Returns:
ParameterizedType
-
toParameterizedType
将Type转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型 一般用于获取泛型参数具体的参数类型,例如:
通过此方法,传入B.class即可得到B对应的class A<T> class B extends A<String>;ParameterizedType,从而获取到String- Parameters:
type-TypeinterfaceIndex- 实现的第几个接口- Returns:
ParameterizedType
-
getGenerics
获取指定类所有泛型父类和泛型接口- 指定类及其所有的泛型父类
- 指定类实现的直接泛型接口
- Parameters:
clazz- 类- Returns:
- 泛型父类或接口数组
-
hasTypeVariable
指定泛型数组中是否含有泛型变量- Parameters:
types- 泛型数组- Returns:
- 是否含有泛型变量
-
getTypeMap
获取泛型变量和泛型实际类型的对应关系Map 例如:E java.lang.Integer- Parameters:
clazz- 被解析的包含泛型参数的类- Returns:
- 泛型对应关系Map
-
getActualType
获得泛型字段对应的泛型实际类型,如果此变量没有对应的实际类型,返回null- Parameters:
type- 实际类型明确的类field- 字段- Returns:
- 实际类型,可能为Class等
-
getActualType
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理:1. 泛型化对象,类似于Map<User, Key<Long>> 2. 泛型变量,类似于T- Parameters:
type- 类typeVariable- 泛型变量,例如T等- Returns:
- 实际类型,可能为Class等
-
getActualType
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理复杂的泛型化对象,类似于Map<User, Key<Long>>- Parameters:
type- 类parameterizedType- 泛型变量,例如List<T>等- Returns:
- 实际类型,可能为Class等
-
getActualTypes
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null- Parameters:
type- 类typeVariables- 泛型变量数组,例如T等- Returns:
- 实际类型数组,可能为Class等
-
getImplicitBounds
如果TypeVariable.getBounds()返回一个空数组, 则返回一个包含Object唯一类型的数组. 否则返回TypeVariable.getBounds()传递给normalizeUpperBounds(java.lang.reflect.Type[])的结果- Parameters:
typeVariable- 类型变量- Returns:
- 包含类型变量边界的非空数组.
-
getImplicitUpperBounds
如果WildcardType.getUpperBounds()返回一个空数组, 则返回一个包含Object唯一值的数组否则, 它将返回传递给normalizeUpperBounds(java.lang.reflect.Type[])的WildcardType.getUpperBounds()的结果- Parameters:
wildcardType- 通配符类型- Returns:
- 包含通配符类型下界的非空数组.
-
normalizeUpperBounds
该方法在类型变量类型和通配符类型中去除冗余的上界类型- Parameters:
bounds- 表示WildcardType或TypeVariable的上界的类型数组.- Returns:
- 包含来自
bounds的值减去冗余类型的数组.
-