Class ClassUtil

java.lang.Object
cloud.opencode.base.reflect.ClassUtil

public final class ClassUtil extends Object
Class Utility Class 类工具类

Provides low-level class operation utilities with caching.

提供带缓存的底层类操作工具。

Features | 主要功能:

  • Class loading with caching - 带缓存的类加载
  • Class hierarchy resolution with caching - 带缓存的类层次解析
  • Interface resolution with caching - 带缓存的接口解析
  • Primitive/wrapper conversion - 原始/包装类型转换

Usage Examples | 使用示例:

Class<?> clazz = ClassUtil.loadClass("com.example.User");
List<Class<?>> hierarchy = ClassUtil.getClassHierarchy(User.class);
Class<?> wrapper = ClassUtil.primitiveToWrapper(int.class);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(h) for first hierarchy resolution where h is the class hierarchy depth - 时间复杂度: 缓存命中时 O(1);首次层次解析为 O(h),h为类层次深度
  • Space complexity: O(h) for the cached hierarchy per class - 空间复杂度: O(h),每类缓存层次
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • forName

      public static Class<?> forName(String className)
      Loads class by name (cached) 按名称加载类(缓存)
      Parameters:
      className - the class name | 类名
      Returns:
      the class | 类
      Throws:
      OpenReflectException - if not found | 如果未找到
    • forNameSafe

      public static Optional<Class<?>> forNameSafe(String className)
      Loads class by name safely 安全按名称加载类
      Parameters:
      className - the class name | 类名
      Returns:
      Optional of class | 类的Optional
    • forName

      public static Class<?> forName(String className, ClassLoader classLoader)
      Loads class by name with class loader 使用类加载器按名称加载类
      Parameters:
      className - the class name | 类名
      classLoader - the class loader | 类加载器
      Returns:
      the class | 类
    • exists

      public static boolean exists(String className)
      Checks if class exists 检查类是否存在
      Parameters:
      className - the class name | 类名
      Returns:
      true if exists | 如果存在返回true
    • getAllSuperclasses

      public static List<Class<?>> getAllSuperclasses(Class<?> clazz)
      Gets all superclasses (cached) 获取所有父类(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of superclasses | 父类列表
    • getAllInterfaces

      public static List<Class<?>> getAllInterfaces(Class<?> clazz)
      Gets all interfaces including inherited (cached) 获取所有接口包含继承(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of interfaces | 接口列表
    • getClassHierarchy

      public static List<Class<?>> getClassHierarchy(Class<?> clazz)
      Gets full class hierarchy (cached) 获取完整类层次结构(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of all classes in hierarchy | 层次结构中的所有类
    • isPrimitive

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

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

      public static boolean isPrimitiveOrWrapper(Class<?> clazz)
      Checks if primitive or wrapper 检查是否为原始类型或包装类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if primitive or wrapper | 如果是原始或包装类型返回true
    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Checks if Record class 检查是否为Record类
      Parameters:
      clazz - the class | 类
      Returns:
      true if record | 如果是Record返回true
    • isSealed

      public static boolean isSealed(Class<?> clazz)
      Checks if sealed class 检查是否为密封类
      Parameters:
      clazz - the class | 类
      Returns:
      true if sealed | 如果是密封类返回true
    • isEnum

      public static boolean isEnum(Class<?> clazz)
      Checks if enum type 检查是否为枚举类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if enum | 如果是枚举返回true
    • isArray

      public static boolean isArray(Class<?> clazz)
      Checks if array type 检查是否为数组类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if array | 如果是数组返回true
    • isInterface

      public static boolean isInterface(Class<?> clazz)
      Checks if interface 检查是否为接口
      Parameters:
      clazz - the class | 类
      Returns:
      true if interface | 如果是接口返回true
    • isAbstract

      public static boolean isAbstract(Class<?> clazz)
      Checks if abstract class 检查是否为抽象类
      Parameters:
      clazz - the class | 类
      Returns:
      true if abstract | 如果是抽象类返回true
    • isFinal

      public static boolean isFinal(Class<?> clazz)
      Checks if final class 检查是否为final类
      Parameters:
      clazz - the class | 类
      Returns:
      true if final | 如果是final返回true
    • isAnonymous

      public static boolean isAnonymous(Class<?> clazz)
      Checks if anonymous class 检查是否为匿名类
      Parameters:
      clazz - the class | 类
      Returns:
      true if anonymous | 如果是匿名类返回true
    • isInnerClass

      public static boolean isInnerClass(Class<?> clazz)
      Checks if inner class 检查是否为内部类
      Parameters:
      clazz - the class | 类
      Returns:
      true if inner | 如果是内部类返回true
    • isFunctionalInterface

      public static boolean isFunctionalInterface(Class<?> clazz)
      Checks if functional interface 检查是否为函数式接口
      Parameters:
      clazz - the class | 类
      Returns:
      true if functional interface | 如果是函数式接口返回true
    • primitiveToWrapper

      public static Class<?> primitiveToWrapper(Class<?> primitiveType)
      Converts primitive to wrapper 原始类型转包装类型
      Parameters:
      primitiveType - the primitive type | 原始类型
      Returns:
      the wrapper type | 包装类型
    • wrapperToPrimitive

      public static Class<?> wrapperToPrimitive(Class<?> wrapperType)
      Converts wrapper to primitive 包装类型转原始类型
      Parameters:
      wrapperType - the wrapper type | 包装类型
      Returns:
      the primitive type or null | 原始类型或null
    • getComponentType

      public static Class<?> getComponentType(Class<?> arrayClass)
      Gets array component type 获取数组组件类型
      Parameters:
      arrayClass - the array class | 数组类
      Returns:
      the component type | 组件类型
    • getArrayClass

      public static Class<?> getArrayClass(Class<?> componentType)
      Gets array class for component type 获取组件类型的数组类
      Parameters:
      componentType - the component type | 组件类型
      Returns:
      the array class | 数组类
    • getSimpleName

      public static String getSimpleName(Class<?> clazz)
      Gets simple name 获取简单名称
      Parameters:
      clazz - the class | 类
      Returns:
      the simple name | 简单名称
    • getCanonicalNameOrName

      public static String getCanonicalNameOrName(Class<?> clazz)
      Gets canonical name or name 获取规范名称或名称
      Parameters:
      clazz - the class | 类
      Returns:
      the canonical name or name | 规范名称或名称
    • getPackageName

      public static String getPackageName(Class<?> clazz)
      Gets package name 获取包名
      Parameters:
      clazz - the class | 类
      Returns:
      the package name | 包名
    • isSamePackage

      public static boolean isSamePackage(Class<?> class1, Class<?> class2)
      Checks if same package 检查是否在同一包
      Parameters:
      class1 - the first class | 第一个类
      class2 - the second class | 第二个类
      Returns:
      true if same package | 如果在同一包返回true
    • isAssignableFrom

      public static boolean isAssignableFrom(Class<?> target, Class<?> source)
      Checks if assignable from 检查是否可从另一类赋值
      Parameters:
      target - the target type | 目标类型
      source - the source type | 源类型
      Returns:
      true if assignable | 如果可赋值返回true
    • getCommonSuperclass

      public static Class<?> getCommonSuperclass(Class<?> class1, Class<?> class2)
      Gets common superclass 获取公共父类
      Parameters:
      class1 - the first class | 第一个类
      class2 - the second class | 第二个类
      Returns:
      the common superclass | 公共父类
    • clearCache

      public static void clearCache()
      Clears all caches 清除所有缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for specific class 清除特定类的缓存
      Parameters:
      clazz - the class | 类