类 TypeUtils

java.lang.Object
com.alibaba.ageiport.common.utils.TypeUtils

public class TypeUtils extends Object
Type的工具类封装
作者:
lingyi
  • 构造器详细资料

    • TypeUtils

      public TypeUtils()
  • 方法详细资料

    • getGenericParamType

      public static List<Type> getGenericParamType(Class source, Class target)
    • findDirectTargetInterface

      public static ParameterizedType findDirectTargetInterface(Class source, Class target)
      获取直接继承目标接口, 此接口是泛型接口
      参数:
      source -
      target -
      返回:
    • findTargetInterfaceRecursive

      public static ParameterizedType findTargetInterfaceRecursive(Class source, Class target)
    • findActualTypeArguments

      public static List<Type> findActualTypeArguments(Type type)
       获取泛型的实际类型
       如果泛型是 List 这种, 继续找到D
       Type 可能是实际类型, 也可能是TypeVariableImpl
       
      参数:
      type -
      返回:
    • findActualTypeArgumentIfRawTypeIfList

      public static Type findActualTypeArgumentIfRawTypeIfList(Type type)
    • findParentTypeArgumentMap

      public static Map<String,Type> findParentTypeArgumentMap(Class source)
      参数:
      source -
      返回:
      {{"T", ParameterizedType}}
    • getDeclaredFields

      public static List<Field> getDeclaredFields(Class clazz)
      获取所有的字段,包括父类
      参数:
      clazz -
      返回:
    • flattenHierarchy

      public static Set<Class<?>> flattenHierarchy(Class<?> concreteClass)
    • isUnknow

      public static boolean isUnknow(Type type)
      是否未知类型
      type为null或者TypeVariable 都视为未知类型
      参数:
      type - Type类型
      返回:
      是否未知类型
    • getTypeArgument

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

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

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

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

      参数:
      type - 指定类型
      返回:
      所有泛型参数类型
    • getClass

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

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

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

      参数:
      type - Type
      返回:
      ParameterizedType