Class TypeUtils

java.lang.Object
cn.dinodev.spring.commons.utils.TypeUtils

public class TypeUtils extends Object
Author:
Cody Lu, JL
  • Constructor Details

    • TypeUtils

      public TypeUtils()
  • Method Details

    • getGenericParamClass

      public static <T> Class<T> getGenericParamClass(Type actualType, Class<?> contextType, int paramIndex)
      获取某个泛型接口的实际参数
      Type Parameters:
      T -
      Parameters:
      actualType - 实际类型
      contextType - 需要查询的接口类
      paramIndex - 类型参数的索引,从0开始
      Returns:
    • resolveGenericParamType

      public static org.springframework.core.ResolvableType resolveGenericParamType(org.springframework.core.ResolvableType actualType, Class<?> interfaceType)
      获取某个泛型接口的实际类型
      Parameters:
      actualType - 实际类型
      interfaceType - 需要查询的接口类
      Returns:
    • getGenericParamClass

      public static <T> Class<T> getGenericParamClass(Object inst, Class<?> contextType, int paramIndex)
      获取某个泛型接口的实际参数
      Type Parameters:
      T -
      Parameters:
      inst - 对象实例
      contextType - 需要查询的接口类
      paramIndex - 类型参数的索引,从0开始
      Returns:
    • getGenericSuperclassParamClass

      public static <T> Class<T> getGenericSuperclassParamClass(Object inst, Class<?> interfaceClass, int paramIndex)
      获取某个父类泛型接口的实际参数
      Type Parameters:
      T -
      Parameters:
      inst - 对象实例
      interfaceClass - 需要查询的接口类
      paramIndex - 类型参数的索引,从0开始
      Returns:
    • isPrimitiveOrString

      public static boolean isPrimitiveOrString(@Nonnull Class<?> clazz)
      判断class是否为基本类型或者基本类型的包装类型
      Parameters:
      clazz - 类
      Returns:
      是否为基本类型或者基本类型的包装类型
    • newInstance

      public static <T> T newInstance(Class<T> clazz)
      用默认构造函数构建创建新实例
      Parameters:
      clazz - 类
      Returns:
      新实例
    • isInstanceOfAny

      public static boolean isInstanceOfAny(Object instance, Type... types)
      判断instance是否为types中的一种类型
      Parameters:
      instance - 实例
      types - 类型,可以是接口或者类,也可以是基本类型
      Returns:
    • cast

      @Nullable public static <T> T cast(@Nullable Object object)
      类型转换
      Parameters:
      object - 待转换对象
      Returns:
      转换后的对象,可能为空
    • castNonNull

      @NonNull public static <T> T castNonNull(@Nullable Object object)
      类型转换
      Parameters:
      object - 待转换对象
      Returns:
      非空, 否则抛出异常