Class SealedUtil

java.lang.Object
cloud.opencode.base.reflect.sealed.SealedUtil

public final class SealedUtil extends Object
Sealed Class Utility Class 密封类工具类

Provides low-level sealed class operation utilities with caching.

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

Features | 主要功能:

  • Sealed class detection - 密封类检测
  • Permitted subclasses discovery with caching - 带缓存的许可子类发现
  • Recursive subclass resolution - 递归子类解析

Usage Examples | 使用示例:

boolean isSealed = SealedUtil.isSealed(Shape.class);
Class<?>[] permitted = SealedUtil.getPermittedSubclasses(Shape.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(p) for first access where p is the number of permitted subclasses - 时间复杂度: 缓存命中时 O(1);首次访问为 O(p),p为许可子类数量
  • Space complexity: O(p) for the cached permitted subclasses per sealed class - 空间复杂度: O(p),每个密封类缓存许可子类
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isSealed

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

      public static boolean isSealedClass(Object obj)
      Checks if an object's class is sealed 检查对象的类是否为密封类
      Parameters:
      obj - the object | 对象
      Returns:
      true if sealed | 如果是密封类返回true
    • requireSealed

      public static void requireSealed(Class<?> clazz)
      Requires class to be sealed 要求类为密封类
      Parameters:
      clazz - the class | 类
      Throws:
      IllegalArgumentException - if not sealed | 如果不是密封类
    • isNonSealed

      public static boolean isNonSealed(Class<?> clazz)
      Checks if a class is a non-sealed subclass 检查类是否为非密封子类
      Parameters:
      clazz - the class | 类
      Returns:
      true if non-sealed | 如果是非密封返回true
    • getPermittedSubclasses

      public static Class<?>[] getPermittedSubclasses(Class<?> sealedClass)
      Gets permitted subclasses (cached) 获取许可子类(缓存)
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      array of permitted subclasses | 许可子类数组
    • getPermittedSubclassList

      public static List<Class<?>> getPermittedSubclassList(Class<?> sealedClass)
      Gets permitted subclasses as list 获取许可子类列表
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of permitted subclasses | 许可子类列表
    • getPermittedSubclassCount

      public static int getPermittedSubclassCount(Class<?> sealedClass)
      Gets permitted subclasses count 获取许可子类数量
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      the count | 数量
    • isPermittedSubclass

      public static boolean isPermittedSubclass(Class<?> sealedClass, Class<?> subclass)
      Checks if a class is a permitted subclass 检查类是否为许可子类
      Parameters:
      sealedClass - the sealed class | 密封类
      subclass - the potential subclass | 潜在子类
      Returns:
      true if permitted | 如果是许可的返回true
    • getAllSubclassesRecursive

      public static Set<Class<?>> getAllSubclassesRecursive(Class<?> sealedClass)
      Gets all subclasses recursively (cached) 递归获取所有子类(缓存)
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      set of all subclasses | 所有子类集合
    • getLeafClasses

      public static List<Class<?>> getLeafClasses(Class<?> sealedClass)
      Gets all leaf classes (final or non-sealed) 获取所有叶类(final或非密封)
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of leaf classes | 叶类列表
    • getConcreteTypes

      public static List<Class<?>> getConcreteTypes(Class<?> sealedClass)
      Gets all concrete (instantiable) types 获取所有具体(可实例化)类型
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of concrete types | 具体类型列表
    • hasSealedParent

      public static boolean hasSealedParent(Class<?> clazz)
      Checks if class has a sealed parent 检查类是否有密封父类
      Parameters:
      clazz - the class | 类
      Returns:
      true if has sealed parent | 如果有密封父类返回true
    • getSealedParent

      public static Optional<Class<?>> getSealedParent(Class<?> clazz)
      Gets the sealed parent class 获取密封父类
      Parameters:
      clazz - the class | 类
      Returns:
      Optional of sealed parent | 密封父类的Optional
    • hasSealedInterface

      public static boolean hasSealedInterface(Class<?> clazz)
      Checks if class implements a sealed interface 检查类是否实现密封接口
      Parameters:
      clazz - the class | 类
      Returns:
      true if implements sealed interface | 如果实现密封接口返回true
    • getSealedInterfaces

      public static List<Class<?>> getSealedInterfaces(Class<?> clazz)
      Gets all sealed interfaces implemented by a class 获取类实现的所有密封接口
      Parameters:
      clazz - the class | 类
      Returns:
      list of sealed interfaces | 密封接口列表
    • validateHierarchy

      public static boolean validateHierarchy(Class<?> sealedClass)
      Validates that all permitted subclasses extend the sealed class 验证所有许可子类都继承密封类
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      true if valid | 如果有效返回true
    • isExhaustive

      public static boolean isExhaustive(Class<?> sealedClass)
      Checks if the sealed hierarchy is exhaustive 检查密封层次结构是否穷尽
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      true if exhaustive | 如果穷尽返回true
    • getNonExhaustiveLeaves

      public static List<Class<?>> getNonExhaustiveLeaves(Class<?> sealedClass)
      Gets non-exhaustive leaf classes 获取非穷尽叶类
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of non-exhaustive leaves | 非穷尽叶列表
    • getHierarchyDepth

      public static int getHierarchyDepth(Class<?> sealedClass)
      Gets the depth of the sealed hierarchy 获取密封层次结构的深度
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      the depth | 深度
    • getTotalTypeCount

      public static int getTotalTypeCount(Class<?> sealedClass)
      Gets the total number of types in the hierarchy 获取层次结构中的类型总数
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      the total count | 总数
    • clearCache

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

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