Class HotSwapClassLoader

java.lang.Object
java.lang.ClassLoader
cloud.opencode.base.classloader.loader.HotSwapClassLoader
All Implemented Interfaces:
AutoCloseable

public class HotSwapClassLoader extends ClassLoader implements AutoCloseable
HotSwap ClassLoader - Supports dynamic class replacement 热替换类加载器 - 支持类的动态替换

ClassLoader that supports hot swapping of classes at runtime.

支持运行时热替换类的类加载器。

Features | 主要功能:

  • Load classes from bytecode - 从字节码加载类
  • Reload modified classes - 重新加载修改的类
  • Version tracking - 版本跟踪
  • Class unloading - 类卸载

Usage Examples | 使用示例:

HotSwapClassLoader loader = HotSwapClassLoader.create();
Class<?> v1 = loader.loadClass("MyClass", bytecodeV1);
// After modification
Class<?> v2 = loader.reloadClass("MyClass", classFile);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-classloader V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • HotSwapClassLoader

      public HotSwapClassLoader()
      Create HotSwap classloader with default parent 使用默认父加载器创建热替换类加载器
    • HotSwapClassLoader

      public HotSwapClassLoader(ClassLoader parent)
      Create HotSwap classloader with specified parent 使用指定父加载器创建热替换类加载器
      Parameters:
      parent - parent classloader | 父类加载器
  • Method Details

    • create

      public static HotSwapClassLoader create()
      Create HotSwap classloader with default parent 使用默认父加载器创建热替换类加载器
      Returns:
      new HotSwapClassLoader | 新的热替换类加载器
    • create

      public static HotSwapClassLoader create(ClassLoader parent)
      Create HotSwap classloader with specified parent 使用指定父加载器创建热替换类加载器
      Parameters:
      parent - parent classloader | 父类加载器
      Returns:
      new HotSwapClassLoader | 新的热替换类加载器
    • loadClass

      public Class<?> loadClass(String name, byte[] bytecode)
      Load or reload class from bytecode 从字节码加载或重新加载类
      Parameters:
      name - class name | 类名
      bytecode - class bytecode | 类字节码
      Returns:
      loaded class | 加载的类
    • reloadClass

      public Class<?> reloadClass(String name, Path classFile)
      Reload class from file 从文件重新加载类
      Parameters:
      name - class name | 类名
      classFile - class file path | 类文件路径
      Returns:
      reloaded class | 重新加载的类
    • loadClass

      public Class<?> loadClass(String name) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • findClass

      protected Class<?> findClass(String name) throws ClassNotFoundException
      Overrides:
      findClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • getVersion

      public int getVersion(String className)
      Get current version of class 获取类的当前版本
      Parameters:
      className - class name | 类名
      Returns:
      version number or 0 if not loaded | 版本号,未加载返回 0
    • getLoadedClassNames

      public Set<String> getLoadedClassNames()
      Get all loaded class names 获取所有已加载的类名
      Returns:
      set of class names | 类名集合
    • isLoaded

      public boolean isLoaded(String className)
      Check if class is loaded 检查类是否已加载
      Parameters:
      className - class name | 类名
      Returns:
      true if loaded | 已加载返回 true
    • getBytecode

      public Optional<byte[]> getBytecode(String className)
      Get bytecode of loaded class 获取已加载类的字节码
      Parameters:
      className - class name | 类名
      Returns:
      optional bytecode | 可选的字节码
    • unloadClass

      public void unloadClass(String className)
      Unload class (will be reloaded on next access) 卸载类(下次访问时重新加载)
      Parameters:
      className - class name | 类名
    • clear

      public void clear()
      Clear all loaded classes 清除所有已加载的类
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Check if classloader is closed 检查类加载器是否已关闭
      Returns:
      true if closed | 已关闭返回 true