Class OpenClass

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

public final class OpenClass extends Object
Class Facade Entry Class 类门面入口类

Provides common class operations API. Similar to Commons Lang ClassUtils.

提供常用类操作API。 对标Commons Lang ClassUtils。

Features | 主要功能:

  • Class loading - 类加载
  • Type checking - 类型检查
  • Inheritance analysis - 继承分析
  • Primitive/wrapper conversion - 原始/包装类型转换

Usage Examples | 使用示例:

// Load class by name
Class<?> clazz = OpenClass.forName("com.example.MyClass");

// Check type
boolean isRecord = OpenClass.isRecord(clazz);

// Get all interfaces
List<Class<?>> interfaces = OpenClass.getAllInterfaces(clazz);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class, immutable maps) - 线程安全: 是(无状态工具类,不可变映射)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
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 a class using context class loader 加载类(使用当前线程类加载器)
      Parameters:
      className - the class name | 类名
      Returns:
      the class | 类
      Throws:
      OpenReflectException - if not found | 如果未找到
    • forName

      public static Class<?> forName(String className, ClassLoader classLoader)
      Loads a class with specified class loader 加载类(指定类加载器)
      Parameters:
      className - the class name | 类名
      classLoader - the class loader | 类加载器
      Returns:
      the class | 类
      Throws:
      OpenReflectException - if not found | 如果未找到
    • forNameWithoutInit

      public static Class<?> forNameWithoutInit(String className)
      Loads a class without initialization 加载类(不初始化)
      Parameters:
      className - the class name | 类名
      Returns:
      the class | 类
      Throws:
      OpenReflectException - if not found | 如果未找到
    • forNameSafe

      public static Optional<Class<?>> forNameSafe(String className)
      Safely loads a class returning Optional 安全加载类(返回Optional)
      Parameters:
      className - the class name | 类名
      Returns:
      Optional of class | 类的Optional
    • exists

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

      public static Class<?> getPrimitiveClass(String name)
      Gets a primitive class by name 加载原始类型
      Parameters:
      name - the primitive name | 原始类型名
      Returns:
      the primitive class or null | 原始类型或null
    • getShortClassName

      public static String getShortClassName(Class<?> clazz)
      Gets short class name 获取类名(简短)
      Parameters:
      clazz - the class | 类
      Returns:
      the short class name | 简短类名
    • getSimpleName

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

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

      public static String getCanonicalName(Class<?> clazz)
      Gets canonical name 获取规范名
      Parameters:
      clazz - the class | 类
      Returns:
      the canonical name | 规范名
    • getClassLocation

      public static Optional<URL> getClassLocation(Class<?> clazz)
      Gets the class location (JAR file) 获取类所在JAR文件
      Parameters:
      clazz - the class | 类
      Returns:
      Optional of URL | URL的Optional
    • 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 type 检查是否为原始类型或包装类型
      Parameters:
      clazz - the class | 类
      Returns:
      true if primitive or wrapper | 如果是原始类型或包装类型返回true
    • isArray

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

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

      public static boolean isAnnotation(Class<?> clazz)
      Checks if annotation 检查是否为注解
      Parameters:
      clazz - the class | 类
      Returns:
      true if annotation | 如果是注解返回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 检查是否为抽象类
      Parameters:
      clazz - the class | 类
      Returns:
      true if abstract | 如果是抽象类返回true
    • isFinal

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

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

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

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

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

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

      public static boolean isAssignable(Class<?> target, Class<?> source)
      Checks if target is assignable from source 检查是否可赋值
      Parameters:
      target - the target class | 目标类
      source - the source class | 源类
      Returns:
      true if assignable | 如果可赋值返回true
    • isAssignable

      public static boolean isAssignable(Class<?> target, Class<?> source, boolean autoboxing)
      Checks if target is assignable from source with autoboxing option 检查是否可赋值(支持原始类型转换)
      Parameters:
      target - the target class | 目标类
      source - the source class | 源类
      autoboxing - whether to consider autoboxing | 是否考虑自动装箱
      Returns:
      true if assignable | 如果可赋值返回true
    • getAllSuperclasses

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

      public static List<Class<?>> getAllInterfaces(Class<?> clazz)
      Gets all interfaces 获取所有接口
      Parameters:
      clazz - the class | 类
      Returns:
      list of interfaces | 接口列表
    • getClassHierarchy

      public static List<Class<?>> getClassHierarchy(Class<?> clazz)
      Gets class hierarchy (from current to Object) 获取继承层次(从当前类到Object)
      Parameters:
      clazz - the class | 类
      Returns:
      list of classes in hierarchy | 继承层次中的类列表
    • primitiveToWrapper

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

      public static Class<?> wrapperToPrimitive(Class<?> clazz)
      Converts wrapper to primitive 包装类型转原始类型
      Parameters:
      clazz - the wrapper class | 包装类型
      Returns:
      the primitive class or original if not wrapper | 原始类型,如果不是包装类型则返回原类型
    • 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 | 数组类
    • isInstantiable

      public static boolean isInstantiable(Class<?> clazz)
      Checks if class can be instantiated 检查是否可实例化
      Parameters:
      clazz - the class | 类
      Returns:
      true if can be instantiated | 如果可实例化返回true
    • hasDefaultConstructor

      public static boolean hasDefaultConstructor(Class<?> clazz)
      Checks if class has default constructor 检查是否有默认构造器
      Parameters:
      clazz - the class | 类
      Returns:
      true if has default constructor | 如果有默认构造器返回true