Class TypeUtil

java.lang.Object
cloud.opencode.base.core.convert.TypeUtil

public final class TypeUtil extends Object
Type Utility Class - Type checking, conversion and metadata operations 类型工具类 - 类型检查、转换和元数据操作

Provides utilities for type checking, primitive/wrapper conversion and type classification.

提供类型检查、原始/包装类型转换和类型分类功能。

Features | 主要功能:

  • Type checking (isPrimitive, isWrapper, isNumber, isDate) - 类型检查
  • Primitive/Wrapper conversion (wrap, unwrap) - 原始/包装类型转换
  • Generic type extraction (getTypeArgument) - 泛型类型提取
  • Type compatibility checking (isAssignable) - 类型兼容性检查

Usage Examples | 使用示例:

// Type checking - 类型检查
boolean isPrim = TypeUtil.isPrimitive(int.class);     // true
boolean isNum = TypeUtil.isNumber(Integer.class);     // true

// Wrapper conversion - 包装类型转换
Class<?> wrapper = TypeUtil.wrap(int.class);          // Integer.class
Class<?> prim = TypeUtil.unwrap(Integer.class);       // int.class

Security | 安全性:

  • Thread-safe: Yes (immutable constants) - 线程安全: 是 (不可变常量)
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(1) for type resolution - 类型解析 O(1)
  • Space complexity: O(1) - O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isPrimitive

      public static boolean isPrimitive(Class<?> clazz)
      Checks if the type is a primitive 检查是否为原始类型
    • isWrapper

      public static boolean isWrapper(Class<?> clazz)
      Checks if the type is a wrapper 检查是否为包装类型
    • isPrimitiveOrWrapper

      public static boolean isPrimitiveOrWrapper(Class<?> clazz)
      Checks if the type is a primitive or wrapper 检查是否为原始类型或包装类型
    • isNumber

      public static boolean isNumber(Class<?> clazz)
      Checks if the type is a number 检查是否为数字类型
    • isCollection

      public static boolean isCollection(Class<?> clazz)
      Checks if the type is a collection 检查是否为集合类型
    • isMap

      public static boolean isMap(Class<?> clazz)
      检查是否为 Map 类型
    • isArray

      public static boolean isArray(Class<?> clazz)
      Checks if the type is an array 检查是否为数组类型
    • isString

      public static boolean isString(Class<?> clazz)
      Checks if the type is a String 检查是否为字符串类型
    • isDateTime

      public static boolean isDateTime(Class<?> clazz)
      Checks if the type is a date/time type 检查是否为日期时间类型
    • getWrapperClass

      public static Class<?> getWrapperClass(Class<?> primitiveType)
      Gets the wrapper class 获取包装类型
    • getPrimitiveClass

      public static Class<?> getPrimitiveClass(Class<?> wrapperType)
      Gets the primitive class 获取原始类型
    • getDefaultValue

      public static <T> T getDefaultValue(Class<T> clazz)
      Gets the default value for the type 获取类型的默认值
    • convert

      public static <T> T convert(Object value, Class<T> targetType)
      Converts the type 类型转换
    • convertOptional

      public static <T> Optional<T> convertOptional(Object value, Class<T> targetType)
      安全类型转换,返回 Optional
    • getGenericTypes

      public static Type[] getGenericTypes(Type type)
      Gets the generic type arguments 获取泛型类型参数
    • getRawType

      public static Class<?> getRawType(Type type)
      Gets the raw type of a generic type 获取泛型的原始类型
    • getFieldGenericType

      public static Type getFieldGenericType(Field field)
      Gets the generic type of a field 获取字段的泛型类型
    • getMethodReturnGenericType

      public static Type getMethodReturnGenericType(Method method)
      Gets the generic return type of a method 获取方法返回值的泛型类型
    • getSuperclassGenericTypes

      public static Type[] getSuperclassGenericTypes(Class<?> clazz)
      Gets the generic type arguments of the superclass 获取父类的泛型参数
    • getInterfaceGenericTypes

      public static Type[] getInterfaceGenericTypes(Class<?> clazz, Class<?> interfaceClass)
      Gets the generic type arguments of an interface 获取接口的泛型参数
    • isAssignable

      public static boolean isAssignable(Class<?> superType, Class<?> subType)
      Checks if the type is assignable 检查类型是否可赋值