Package org.aoju.bus.core.toolkit
Class TypeKit
java.lang.Object
org.aoju.bus.core.toolkit.TypeKit
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TypegetActualType(Type actualType, Class<?> typeDefineClass, Type typeVariable) 获取指定泛型变量对应的真实类型 由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意:static 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 actualType, Class<?> typeDefineClass, Type... typeVariables) 获取指定泛型变量对应的真实类型 由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意: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 Type[]getImplicitBounds(TypeVariable<?> typeVariable) 如果TypeVariable.getBounds()返回一个空数组, 则返回一个包含Object唯一类型的数组.static Type[]getImplicitLowerBounds(WildcardType wildcardType) 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<?> getRawType(Type type, Type assigningType) 根据上下文获取Java类型的原始类型 主要用于TypeVariables和GenericArrayTypestatic 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) 获取泛型变量和泛型实际类型的对应关系Mapstatic 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. 8大基本类型 2.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)
-
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
-
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
-
getClass
-
getType
-
getFieldType
-
getFirstParamType
-
getFirstParamClass
-
getParamType
-
getParamClass
-
getParamTypes
-
getParamClasses
-
getReturnType
-
getReturnClass
-
getTypeArgument
-
getTypeArgument
-
getTypeArguments
-
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
-
getActualTypes
public static Type[] getActualTypes(Type actualType, Class<?> typeDefineClass, Type... typeVariables) 获取指定泛型变量对应的真实类型 由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意:1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明
- Parameters:
actualType- 真实类型所在类,此类中记录了泛型参数对应的实际类型typeDefineClass- 泛型变量声明所在类或接口,此类中定义了泛型类型typeVariables- 泛型变量,需要的实际类型对应的泛型参数- Returns:
- 给定泛型参数对应的实际类型, 如果无对应类型, 返回null
-
getActualType
获取指定泛型变量对应的真实类型 由于子类中泛型参数实现和父类(接口)中泛型定义位置是一一对应的,因此可以通过对应关系找到泛型实现类型 使用此方法注意:1. superClass必须是clazz的父类或者clazz实现的接口 2. typeVariable必须在superClass中声明
- Parameters:
actualType- 真实类型所在类,此类中记录了泛型参数对应的实际类型typeDefineClass- 泛型变量声明所在类或接口,此类中定义了泛型类型typeVariable- 泛型变量,需要的实际类型对应的泛型参数- Returns:
- 给定泛型参数对应的实际类型
-
toParameterizedType
将Type转换为ParameterizedTypeParameterizedType用于获取当前类或父类中泛型参数化后的类型 一般用于获取泛型参数具体的参数类型,例如:class A<T> class B extends A<String>
通过此方法,传入B.class即可得到B
ParameterizedType,从而获取到String- Parameters:
type-Type- Returns:
ParameterizedType
-
getTypeMap
-
getActualType
-
getActualType
-
getActualType
获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理复杂的泛型化对象,类似于Map<User, Key<Long>>- Parameters:
type- 类parameterizedType- 泛型变量,例如List<T>等- Returns:
- 实际类型,可能为Class等
-
getActualTypes
-
getRawType
根据上下文获取Java类型的原始类型 主要用于TypeVariables和GenericArrayType- Parameters:
type- 类型assigningType- 要解析的类型- Returns:
- 如果不能解析类型,则解析
Class对象或null
-
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:
- 包含通配符类型下界的非空数组.
-
getImplicitLowerBounds
- Parameters:
wildcardType- 通配符类型- Returns:
- 包含通配符类型下界的非空数组.
-
normalizeUpperBounds
该方法在类型变量类型和通配符类型中去除冗余的上界类型- Parameters:
bounds- 表示WildcardType或TypeVariable的上界的类型数组.- Returns:
- 包含来自
bounds的值减去冗余类型的数组.
-
hasTypeVariable
指定泛型数组中是否含有泛型变量- Parameters:
types- 泛型数组- Returns:
- 是否含有泛型变量
-