Class TypeUtil

java.lang.Object
cloud.opencode.base.reflect.type.TypeUtil

public final class TypeUtil extends Object
Type Utility Class 类型工具类

Utility class providing common type operations including type resolution, primitive/wrapper conversion, and type string formatting.

提供常用类型操作的工具类,包括类型解析、原始/包装类型转换和类型字符串格式化。

Features | 主要功能:

  • Raw type extraction - 原始类型提取
  • Primitive/wrapper conversion - 原始/包装类型转换
  • Type assignability checking - 类型可赋值检查
  • Type string formatting - 类型字符串格式化

Usage Examples | 使用示例:

Class<?> raw = TypeUtil.getRawType(parameterizedType);
boolean assignable = TypeUtil.isAssignable(targetType, sourceType);
String formatted = TypeUtil.toString(genericType);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class, immutable maps) - 线程安全: 是(无状态工具类,不可变映射)
  • Null-safe: No (caller must ensure non-null type arguments) - 空值安全: 否(调用方须确保非空类型参数)

Performance | 性能特性:

  • Time complexity: O(1) for type lookups using immutable maps; toString() O(n) where n is the number of type arguments - 时间复杂度: 使用不可变映射的类型查找为 O(1);toString() 为 O(n),n为类型参数数量
  • Space complexity: O(1) - 空间复杂度: O(1)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    equals(Type type1, Type type2)
    Checks if two types are equal considering type parameters 检查两个类型是否相等(考虑类型参数)
    static Type[]
    Gets actual type arguments from a parameterized type 从参数化类型获取实际类型参数
    static Class<?>
    Gets the raw type from a Type 从Type获取原始类型
    static TypeVariable<?>[]
    Gets type parameters from a class 从类获取类型参数
    static boolean
    isAssignableFrom(Type target, Type source)
    Checks if target type is assignable from source type 检查目标类型是否可从源类型赋值
    static boolean
    isPrimitive(Class<?> type)
    Checks if type is primitive 检查是否为原始类型
    static boolean
    isWrapper(Class<?> type)
    Checks if type is wrapper 检查是否为包装类型
    static String
    toString(Type type)
    Converts Type to readable string 将Type转换为可读字符串
    static Class<?>
    unwrap(Class<?> type)
    Converts wrapper to primitive 将包装类型转换为原始类型
    static Class<?>
    wrap(Class<?> type)
    Converts primitive to wrapper 将原始类型转换为包装类型

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getRawType

      public static Class<?> getRawType(Type type)
      Gets the raw type from a Type 从Type获取原始类型
      Parameters:
      type - the type | 类型
      Returns:
      the raw class | 原始类
    • isPrimitive

      public static boolean isPrimitive(Class<?> type)
      Checks if type is primitive 检查是否为原始类型
      Parameters:
      type - the type | 类型
      Returns:
      true if primitive | 如果是原始类型返回true
    • isWrapper

      public static boolean isWrapper(Class<?> type)
      Checks if type is wrapper 检查是否为包装类型
      Parameters:
      type - the type | 类型
      Returns:
      true if wrapper | 如果是包装类型返回true
    • wrap

      public static Class<?> wrap(Class<?> type)
      Converts primitive to wrapper 将原始类型转换为包装类型
      Parameters:
      type - the primitive type | 原始类型
      Returns:
      the wrapper type | 包装类型
    • unwrap

      public static Class<?> unwrap(Class<?> type)
      Converts wrapper to primitive 将包装类型转换为原始类型
      Parameters:
      type - the wrapper type | 包装类型
      Returns:
      the primitive type or original if not wrapper | 原始类型,如果不是包装类型则返回原类型
    • isAssignableFrom

      public static boolean isAssignableFrom(Type target, Type source)
      Checks if target type is assignable from source type 检查目标类型是否可从源类型赋值
      Parameters:
      target - the target type | 目标类型
      source - the source type | 源类型
      Returns:
      true if assignable | 如果可赋值返回true
    • toString

      public static String toString(Type type)
      Converts Type to readable string 将Type转换为可读字符串
      Parameters:
      type - the type | 类型
      Returns:
      string representation | 字符串表示
    • getTypeParameters

      public static TypeVariable<?>[] getTypeParameters(Class<?> clazz)
      Gets type parameters from a class 从类获取类型参数
      Parameters:
      clazz - the class | 类
      Returns:
      array of type variables | 类型变量数组
    • getActualTypeArguments

      public static Type[] getActualTypeArguments(Type type)
      Gets actual type arguments from a parameterized type 从参数化类型获取实际类型参数
      Parameters:
      type - the type | 类型
      Returns:
      array of type arguments or empty array | 类型参数数组或空数组
    • equals

      public static boolean equals(Type type1, Type type2)
      Checks if two types are equal considering type parameters 检查两个类型是否相等(考虑类型参数)
      Parameters:
      type1 - the first type | 第一个类型
      type2 - the second type | 第二个类型
      Returns:
      true if equal | 如果相等返回true