Class TypeKit

java.lang.Object
org.miaixz.bus.core.xyz.TypeKit

public class TypeKit extends Object
针对 Type 的工具类封装 最主要功能包括:
 1. 获取方法的参数和返回值类型(包括Type和Class)
 2. 获取泛型参数类型(包括对象的泛型参数或集合元素的泛型类型)
 
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • TypeKit

      public TypeKit()
  • Method Details

    • isMap

      public static boolean isMap(Class<?> clazz)
      是否为 map class 类型
      Parameters:
      clazz - 对象类型
      Returns:
      是否为 map class
    • isArray

      public static boolean isArray(Class<?> clazz)
      是否为 数组 class 类型
      Parameters:
      clazz - 对象类型
      Returns:
      是否为 数组 class
    • isCollection

      public static boolean isCollection(Class<?> clazz)
      是否为 Collection class 类型
      Parameters:
      clazz - 对象类型
      Returns:
      是否为 Collection class
    • isIterable

      public static boolean isIterable(Class<?> clazz)
      是否为 Iterable class 类型
      Parameters:
      clazz - 对象类型
      Returns:
      是否为 数组 class
    • isBase

      public static boolean isBase(Class<?> clazz)
      是否为基本类型 1. 8大基本类型 2. 常见的值类型
      Parameters:
      clazz - 对象类型
      Returns:
      是否为基本类型
    • isAbstract

      public static boolean isAbstract(Class<?> clazz)
      是否为抽象类
      Parameters:
      clazz - 类
      Returns:
      是否为抽象类
    • isAbstractOrInterface

      public static boolean isAbstractOrInterface(Class<?> clazz)
      是抽象类或者接口
      Parameters:
      clazz - 类信息
      Returns:
      是否
    • isJavaBean

      public static boolean isJavaBean(Class<?> clazz)
      是否为标准的类 这个类必须:
       0、不为 null
       1、非接口
       2、非抽象类
       3、非Enum枚举
       4、非数组
       5、非注解
       6、非原始类型(int, long等)
       7、非集合 Iterable
       8、非 Map.clas
       9、非 JVM 生成类
       
      Parameters:
      clazz - 类
      Returns:
      是否为标准类
    • isJdk

      public static boolean isJdk(Class<?> clazz)
      判断一个类是JDK 自带的类型 jdk 自带的类,classLoader 是为空的
      Parameters:
      clazz - 类
      Returns:
      是否为 java 类
    • isAssignable

      public static boolean isAssignable(Type type, Type toType)
      检查subject类型是否可以按照Java泛型规则隐式转换为目标类型. 如果这两种类型都是Class对象, 则该方法返回ClassKit.isAssignable(Class, Class)的结果
      Parameters:
      type - 要分配给目标类型的主题类型
      toType - 目标类型
      Returns:
      如果type可赋值给toType,则true.
    • isArrayType

      public static boolean isArrayType(Type type)
      了解指定的类型是否表示数组类型
      Parameters:
      type - 要检查的类型
      Returns:
      如果type是数组类或GenericArrayType,则为true
    • isUnknown

      public static boolean isUnknown(Type type)
      是否未知类型 type为null或者TypeVariable 都视为未知类型
      Parameters:
      type - Type类型
      Returns:
      是否未知类型
    • getClass

      public static Class<?> getClass(Type type)
      获得Type对应的原始类
      Parameters:
      type - Type
      Returns:
      原始类,如果无法获取原始类,返回null
    • getType

      public static Type getType(Field field)
      获取字段对应的Type类型 方法优先获取GenericType,获取不到则获取Type
      Parameters:
      field - 字段
      Returns:
      Type,可能为null
    • getFieldType

      public static Type getFieldType(Class<?> clazz, String fieldName)
      获得字段的泛型类型
      Parameters:
      clazz - Bean类
      fieldName - 字段名
      Returns:
      字段的泛型类型
    • getClass

      public static Class<?> getClass(Field field)
      获得Field对应的原始类
      Parameters:
      field - Field
      Returns:
      原始类,如果无法获取原始类,返回null
    • getFirstParamType

      public static Type getFirstParamType(Method method)
      获取方法的第一个参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
      Parameters:
      method - 方法
      Returns:
      Type,可能为null
    • getFirstParamClass

      public static Class<?> getFirstParamClass(Method method)
      获取方法的第一个参数类
      Parameters:
      method - 方法
      Returns:
      第一个参数类型,可能为null
    • getParamType

      public static Type getParamType(Method method, int index)
      获取方法的参数类型 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
      Parameters:
      method - 方法
      index - 第几个参数的索引,从0开始计数
      Returns:
      Type,可能为null
    • getParamClass

      public static Class<?> getParamClass(Method method, int index)
      获取方法的参数类
      Parameters:
      method - 方法
      index - 第几个参数的索引,从0开始计数
      Returns:
      参数类,可能为null
    • getParamTypes

      public static Type[] getParamTypes(Method method)
      获取方法的参数类型列表 优先获取方法的GenericParameterTypes,如果获取不到,则获取ParameterTypes
      Parameters:
      method - 方法
      Returns:
      Type列表,可能为null
      See Also:
    • getParamClasses

      public static Class<?>[] getParamClasses(Method method)
      解析方法的参数类型列表 依赖jre\lib\rt.jar
      Parameters:
      method - t方法
      Returns:
      参数类型类列表
      See Also:
    • getReturnType

      public static Type getReturnType(Method method)
      获取方法的返回值类型 获取方法的GenericReturnType
      Parameters:
      method - 方法
      Returns:
      Type,可能为null
      See Also:
    • getReturnClass

      public static Class<?> getReturnClass(Method method)
      解析方法的返回类型类列表
      Parameters:
      method - 方法
      Returns:
      返回值类型的类
      See Also:
    • getTypeArgument

      public static Type getTypeArgument(Type type)
      获得给定类的第一个泛型参数
      Parameters:
      type - 被检查的类型,必须是已经确定泛型类型的类型
      Returns:
      Type,可能为null
    • getTypeArgument

      public static Type getTypeArgument(Type type, int index)
      获得给定类的泛型参数
      Parameters:
      type - 被检查的类型,必须是已经确定泛型类型的类
      index - 泛型类型的索引号,即第几个泛型类型
      Returns:
      Type
    • getTypeArguments

      public static Type[] getTypeArguments(Type type)
      获得指定类型中所有泛型参数类型,例如:
       class A<T>
       class B extends A<String>
       

      通过此方法,传入B.class即可得到String

      Parameters:
      type - 指定类型
      Returns:
      所有泛型参数类型
    • getTypeArguments

      public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)
      检索此参数化类型的所有类型参数,包括所有者层次结构参数
      Parameters:
      type - 指定要从中获取参数的主题参数化类型
      Returns:
      带有类型参数的Map.
    • getTypeArguments

      public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass)
      获取基于子类型的类/接口的类型参数
      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

      public static Type getClosestParentType(Class<?> cls, Class<?> superClass)
      获取与superClass 指定的超类最接近的父类
      Parameters:
      cls - 类
      superClass - 超类
      Returns:
      父类型
    • toParameterizedType

      public static ParameterizedType toParameterizedType(Type type)
      Type 转换为ParameterizedType ParameterizedType用于获取当前类或父类中泛型参数化后的类型 一般用于获取泛型参数具体的参数类型,例如:
       class A<T>
       class B extends A<String>
       

      通过此方法,传入B.class即可得到BParameterizedType,从而获取到String

      Parameters:
      type - Type
      Returns:
      ParameterizedType
    • toParameterizedType

      public static ParameterizedType toParameterizedType(Type type, int interfaceIndex)
      Type 转换为ParameterizedType ParameterizedType用于获取当前类或父类中泛型参数化后的类型 一般用于获取泛型参数具体的参数类型,例如:
      
         class A<T>
         class B extends A<String>;
       
      通过此方法,传入B.class即可得到B对应的ParameterizedType,从而获取到String
      Parameters:
      type - Type
      interfaceIndex - 实现的第几个接口
      Returns:
      ParameterizedType
    • getGenerics

      public static ParameterizedType[] getGenerics(Class<?> clazz)
      获取指定类所有泛型父类和泛型接口
      • 指定类及其所有的泛型父类
      • 指定类实现的直接泛型接口
      Parameters:
      clazz - 类
      Returns:
      泛型父类或接口数组
    • hasTypeVariable

      public static boolean hasTypeVariable(Type... types)
      指定泛型数组中是否含有泛型变量
      Parameters:
      types - 泛型数组
      Returns:
      是否含有泛型变量
    • getTypeMap

      public static Map<Type,Type> getTypeMap(Class<?> clazz)
      获取泛型变量和泛型实际类型的对应关系Map 例如:
           E    java.lang.Integer
       
      Parameters:
      clazz - 被解析的包含泛型参数的类
      Returns:
      泛型对应关系Map
    • getActualType

      public static Type getActualType(Type type, Field field)
      获得泛型字段对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
      Parameters:
      type - 实际类型明确的类
      field - 字段
      Returns:
      实际类型,可能为Class等
    • getActualType

      public static Type getActualType(Type type, Type typeVariable)
      获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理:
           1. 泛型化对象,类似于Map<User, Key<Long>>
           2. 泛型变量,类似于T
       
      Parameters:
      type - 类
      typeVariable - 泛型变量,例如T等
      Returns:
      实际类型,可能为Class等
    • getActualType

      public static Type getActualType(Type type, ParameterizedType parameterizedType)
      获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null 此方法可以处理复杂的泛型化对象,类似于Map<User, Key<Long>>
      Parameters:
      type - 类
      parameterizedType - 泛型变量,例如List<T>等
      Returns:
      实际类型,可能为Class等
    • getActualTypes

      public static Type[] getActualTypes(Type type, Type... typeVariables)
      获得泛型变量对应的泛型实际类型,如果此变量没有对应的实际类型,返回null
      Parameters:
      type - 类
      typeVariables - 泛型变量数组,例如T等
      Returns:
      实际类型数组,可能为Class等
    • getImplicitBounds

      public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
      如果TypeVariable.getBounds()返回一个空数组, 则返回一个包含Object 唯一类型的数组. 否则返回TypeVariable.getBounds() 传递给normalizeUpperBounds(java.lang.reflect.Type[])的结果
      Parameters:
      typeVariable - 类型变量
      Returns:
      包含类型变量边界的非空数组.
    • getImplicitUpperBounds

      public static Type[] getImplicitUpperBounds(WildcardType wildcardType)
      如果WildcardType.getUpperBounds()返回一个空数组, 则返回一个包含Object唯一值的数组否则, 它将返回传递给normalizeUpperBounds(java.lang.reflect.Type[])WildcardType.getUpperBounds()的结果
      Parameters:
      wildcardType - 通配符类型
      Returns:
      包含通配符类型下界的非空数组.
    • normalizeUpperBounds

      public static Type[] normalizeUpperBounds(Type[] bounds)
      该方法在类型变量类型和通配符类型中去除冗余的上界类型
      Parameters:
      bounds - 表示WildcardTypeTypeVariable的上界的类型数组.
      Returns:
      包含来自bounds的值减去冗余类型的数组.