Class OpenClass

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

public final class OpenClass extends Object
Class Utility Class - Class loading, type checking, generics handling and classpath operations Class 工具类 - 类加载、类型判断、泛型处理和类路径操作

Provides comprehensive class operations including loading, type checking, generics and classpath operations.

提供全面的类操作,包括类加载、类型判断、泛型处理、包扫描和类路径操作。

Features | 主要功能:

  • Class loading (loadClass, loadClassSafely, forName) - 类加载
  • Type checking (isPrimitive, isWrapper, isArray, isEnum) - 类型判断
  • Primitive/Wrapper conversion (wrap, unwrap, getDefaultValue) - 原始/包装类型转换
  • Generics handling (getTypeArguments, getSuperclassTypeArguments) - 泛型处理
  • Hierarchy operations (getAllSuperclasses, getAllInterfaces, getCommonSuperClass) - 类层次操作
  • Classpath operations (getResource, getResourceAsStream, getJarPath) - 类路径操作
  • Instantiation (newInstance) - 实例化

Usage Examples | 使用示例:

// Class loading - 类加载
Class<?> clazz = OpenClass.loadClass("com.example.MyClass");
Optional<Class<?>> opt = OpenClass.loadClassSafely("Unknown");

// Type checking - 类型判断
boolean isPrimitive = OpenClass.isPrimitive(int.class);  // true
boolean isWrapper = OpenClass.isWrapper(Integer.class);  // true

// Primitive/Wrapper conversion - 类型转换
Class<?> wrapped = OpenClass.wrap(int.class);  // Integer.class
Object defaultVal = OpenClass.getDefaultValue(int.class); // 0

// Generics - 泛型
Type[] args = OpenClass.getTypeArguments(MyClass.class);

Security | 安全性:

  • Thread-safe: Yes (stateless with immutable caches) - 线程安全: 是 (无状态, 不可变缓存)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getClassLoader

      public static ClassLoader getClassLoader()
      Returns the default ClassLoader for the current thread or this class. 获取默认 ClassLoader
    • getClassLoader

      public static ClassLoader getClassLoader(Class<?> clazz)
      Returns the ClassLoader for the specified class. 获取指定类的 ClassLoader
    • loadClass

      public static Class<?> loadClass(String className)
      Loads and initializes the class with the given name. 加载类
    • loadClass

      public static Class<?> loadClass(String className, boolean initialize)
      Loads the class with the given name, optionally initializing it. 加载类(不初始化)
    • loadClass

      public static Class<?> loadClass(String className, ClassLoader classLoader)
      Loads and initializes the class using the specified ClassLoader. 加载类(指定 ClassLoader)
    • loadClassSafely

      public static Class<?> loadClassSafely(String className)
      Safely loads the class, returning null if it does not exist. 安全加载类(不存在返回 null)
    • loadClassOptional

      public static Optional<Class<?>> loadClassOptional(String className)
      Safely loads the class, returning an Optional. 安全加载类(返回 Optional)
    • isPresent

      public static boolean isPresent(String className)
      Returns true if the class with the given name is present on the classpath. 检查类是否存在
    • isPresent

      public static boolean isPresent(String className, ClassLoader classLoader)
      Returns true if the class is present using the specified ClassLoader. 检查类是否存在(指定 ClassLoader)
    • isPrimitive

      public static boolean isPrimitive(Class<?> clazz)
      Returns true if the class represents a primitive type. 检查是否为原始类型
    • isPrimitiveWrapper

      public static boolean isPrimitiveWrapper(Class<?> clazz)
      Returns true if the class is a primitive wrapper type. 检查是否为原始类型包装类
    • isPrimitiveOrWrapper

      public static boolean isPrimitiveOrWrapper(Class<?> clazz)
      Returns true if the class is a primitive type or its wrapper. 检查是否为原始类型或包装类
    • isArray

      public static boolean isArray(Class<?> clazz)
      Returns true if the class represents an array type. 检查是否为数组类型
    • isCollection

      public static boolean isCollection(Class<?> clazz)
      Returns true if the class is a Collection or Map type. 检查是否为集合类型
    • isInterface

      public static boolean isInterface(Class<?> clazz)
      Returns true if the class is an interface. 检查是否为接口
    • isAbstract

      public static boolean isAbstract(Class<?> clazz)
      Returns true if the class is abstract. 检查是否为抽象类
    • isEnum

      public static boolean isEnum(Class<?> clazz)
      Returns true if the class is an enum type. 检查是否为枚举类型
    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Returns true if the class is a record type. 检查是否为 Record 类型
    • isSealed

      public static boolean isSealed(Class<?> clazz)
      Returns true if the class is a sealed type. 检查是否为 sealed 类型
    • isInnerClass

      public static boolean isInnerClass(Class<?> clazz)
      Returns true if the class is a non-static inner class. 检查是否为内部类
    • isAnonymousClass

      public static boolean isAnonymousClass(Class<?> clazz)
      Returns true if the class is an anonymous class. 检查是否为匿名类
    • isLocalClass

      public static boolean isLocalClass(Class<?> clazz)
      Returns true if the class is a local class. 检查是否为本地类
    • isLambdaClass

      public static boolean isLambdaClass(Class<?> clazz)
      Returns true if the class was generated by a lambda expression. 检查是否为 Lambda 表达式生成的类
    • isAssignable

      public static boolean isAssignable(Class<?> superType, Class<?> subType)
      Returns true if subType is assignable to superType (considering primitive/wrapper conversions). 检查是否可赋值
    • getWrapperClass

      public static Class<?> getWrapperClass(Class<?> primitiveType)
      Returns the wrapper class corresponding to the given primitive type. 获取原始类型对应的包装类
    • getPrimitiveClass

      public static Class<?> getPrimitiveClass(Class<?> wrapperType)
      Returns the primitive type corresponding to the given wrapper class. 获取包装类对应的原始类型
    • getComponentType

      public static Class<?> getComponentType(Class<?> arrayClass)
      Returns the component type of the given array class. 获取数组的组件类型
    • getArrayClass

      public static Class<?> getArrayClass(Class<?> componentType)
      Returns the array type for the given component type. 获取数组类型
    • getSimpleName

      public static String getSimpleName(Class<?> clazz)
      Returns the simple name of the class. 获取类的简单名称
    • getShortName

      public static String getShortName(Class<?> clazz)
      Returns the short name of the class (last segment of the fully-qualified name). 获取类的短名称
    • getFullName

      public static String getFullName(Class<?> clazz)
      Returns the fully-qualified name of the class. 获取类的完全限定名
    • getPackageName

      public static String getPackageName(Class<?> clazz)
      Returns the package name of the class. 获取包名
    • getPackageName

      public static String getPackageName(String className)
      Returns the package name extracted from the fully-qualified class name string. 获取包名(从类名字符串)
    • classNameToPath

      public static String classNameToPath(String className)
      Converts a class name to a resource path by replacing '.' with '/'. 类名转资源路径
    • pathToClassName

      public static String pathToClassName(String path)
      Converts a resource path to a class name by replacing '/' with '.'. 资源路径转类名
    • getSuperClasses

      public static List<Class<?>> getSuperClasses(Class<?> clazz)
      Returns all superclasses of the given class, excluding Object. 获取所有父类(不含 Object)
    • getAllInterfaces

      public static Set<Class<?>> getAllInterfaces(Class<?> clazz)
      Returns all interfaces implemented by the class, including inherited interfaces. 获取所有接口(包括继承的)
    • getAllSuperTypes

      public static Set<Class<?>> getAllSuperTypes(Class<?> clazz)
      Returns all superclasses and interfaces of the given class. 获取所有父类和接口
    • getCommonSuperClass

      public static Class<?> getCommonSuperClass(Class<?> class1, Class<?> class2)
      Returns the closest common superclass of two classes, or Object.class if none. 获取公共父类
      Parameters:
      class1 - first class | 第一个类
      class2 - second class | 第二个类
      Returns:
      closest common superclass, or Object.class if none | 最近公共父类,如果没有则返回 Object.class
    • getTypeArguments

      public static Type[] getTypeArguments(Class<?> clazz)
      Returns the type parameters declared on the given class. 获取类的泛型参数
      Parameters:
      clazz - the class | 类
      Returns:
      array of type parameters, or empty array if none | 泛型参数数组,如果没有则返回空数组
    • getSuperclassTypeArguments

      public static Type[] getSuperclassTypeArguments(Class<?> clazz)
      Returns the actual type arguments of the generic superclass of the given class. 获取父类的泛型参数
      Parameters:
      clazz - the class | 类
      Returns:
      array of superclass type arguments, or empty array if none | 父类泛型参数数组,如果没有则返回空数组
    • getInterfaceTypeArguments

      public static Type[] getInterfaceTypeArguments(Class<?> clazz, Class<?> interfaceClass)
      Returns the actual type arguments for the specified interface on the given class. 获取接口的泛型参数
      Parameters:
      clazz - implementing class | 实现类
      interfaceClass - interface class | 接口类
      Returns:
      array of interface type arguments, or empty array if none | 接口泛型参数数组,如果没有则返回空数组
    • resolveTypeArgument

      public static Class<?> resolveTypeArgument(Class<?> clazz, Class<?> genericClass)
      Resolves the first type argument for the given generic superclass or interface. 解析泛型类型为实际类型
      Parameters:
      clazz - subclass | 子类
      genericClass - generic superclass or interface | 泛型父类或接口
      Returns:
      actual type of the first type argument, or null if unresolvable | 第一个泛型参数的实际类型,如果无法解析则返回 null
    • getDefaultValue

      public static <T> T getDefaultValue(Class<T> clazz)
      Returns the default value for the given type (null for non-primitive types). 获取类型的默认值
    • getPrimitiveDefaultValue

      public static Object getPrimitiveDefaultValue(Class<?> primitiveType)
      Returns the default value for the given primitive type. 获取原始类型的默认值
    • getResource

      public static URL getResource(String resourceName)
      Returns the URL of the resource with the given name from the classpath. 获取类路径下的资源 URL
    • getResourceAsStream

      public static InputStream getResourceAsStream(String resourceName)
      Returns an InputStream for reading the named resource from the classpath. 获取类路径下的资源流
    • getCodeSourceLocation

      public static URL getCodeSourceLocation(Class<?> clazz)
      Returns the code source location URL for the given class. 获取类的代码源位置
    • getJarPath

      public static String getJarPath(Class<?> clazz)
      Returns the JAR file path for the given class, or null if not in a JAR. 获取类所在的 JAR 文件路径
      Parameters:
      clazz - the class | 类
      Returns:
      JAR file path, or null if not in a JAR | JAR 文件路径,如果不在 JAR 中则返回 null
    • newInstance

      public static <T> T newInstance(Class<T> clazz)
      Creates a new instance of the given class using its no-arg constructor. 创建类的新实例