Class OpenSealed

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

public final class OpenSealed extends Object
Sealed Class Facade Entry Class 密封类门面入口类

Provides common sealed class operations API.

提供常用密封类操作API。

Features | 主要功能:

  • Sealed class detection - 密封类检测
  • Permitted subclasses access - 许可子类访问
  • Hierarchy navigation - 层次结构导航

Usage Examples | 使用示例:

boolean isSealed = OpenSealed.isSealed(Shape.class);
List<Class<?>> subclasses = OpenSealed.getPermittedSubclasses(Shape.class);
PermittedSubclasses permitted = OpenSealed.permitted(Shape.class);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Partially (null class returns false for isSealed) - 空值安全: 部分(null类在isSealed中返回false)
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 <T> Class<T> requireSealed(Class<T> clazz)
      Requires class to be sealed 要求类为密封类
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the class | 类
      Throws:
      IllegalArgumentException - if not sealed | 如果不是密封类
    • getPermittedSubclasses

      public static PermittedSubclasses getPermittedSubclasses(Class<?> sealedClass)
      Gets permitted subclasses 获取许可子类
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      the PermittedSubclasses collection | PermittedSubclasses集合
    • 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 the number of permitted subclasses 获取许可子类数量
      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 递归获取所有子类
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      set of all subclasses | 所有子类集合
    • getLeafClasses

      public static List<Class<?>> getLeafClasses(Class<?> sealedClass)
      Gets all leaf (non-sealed, non-abstract) classes 获取所有叶(非密封、非抽象)类
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of leaf classes | 叶类列表
    • getHierarchy

      public static PermittedSubclasses.HierarchyNode getHierarchy(Class<?> sealedClass)
      Gets the hierarchy tree 获取层次结构树
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      the hierarchy node | 层次结构节点
    • getSealedParent

      public static Optional<Class<?>> getSealedParent(Class<?> clazz)
      Gets the sealed parent of a class (if any) 获取类的密封父类(如果有)
      Parameters:
      clazz - the class | 类
      Returns:
      Optional of sealed parent | 密封父类的Optional
    • 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 (all cases handled) 检查密封层次结构是否穷尽(所有情况都已处理)
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      true if exhaustive | 如果穷尽返回true
    • getConcreteTypes

      public static List<Class<?>> getConcreteTypes(Class<?> sealedClass)
      Gets all concrete types for pattern matching 获取模式匹配的所有具体类型
      Parameters:
      sealedClass - the sealed class | 密封类
      Returns:
      list of concrete types | 具体类型列表
    • generateSwitchTemplate

      public static String generateSwitchTemplate(Class<?> sealedClass, String variableName)
      Generates a switch expression pattern matching template (as string) 生成switch表达式模式匹配模板(作为字符串)
      Parameters:
      sealedClass - the sealed class | 密封类
      variableName - the variable name | 变量名
      Returns:
      the template string | 模板字符串