Class IsoClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class IsoClassLoader extends URLClassLoader implements AutoCloseable
Isolated ClassLoader - Supports independent class loading environment 隔离类加载器 - 支持独立的类加载环境

ClassLoader that supports class isolation for plugin systems and hot deployment.

支持类隔离的类加载器,用于插件系统和热部署。

Features | 主要功能:

  • Isolated class loading - 隔离类加载
  • Configurable loading strategy - 可配置的加载策略
  • Package-level isolation control - 包级别隔离控制
  • Resource access - 资源访问

Usage Examples | 使用示例:

IsoClassLoader loader = IsoClassLoader.fromJar("/path/to/plugin.jar")
    .addIsolatedPackage("com.plugin")
    .build();
Class<?> pluginClass = loader.loadClass("com.plugin.MyPlugin");
loader.close();

Security | 安全性:

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

    • fromJar

      public static IsoClassLoader.Builder fromJar(String jarPath)
      Create builder from JAR file path string 从 JAR 文件路径字符串创建构建器
      Parameters:
      jarPath - JAR file path | JAR 文件路径
      Returns:
      builder | 构建器
    • fromJar

      public static IsoClassLoader.Builder fromJar(Path jarPath)
      Create builder from JAR file path 从 JAR 文件路径创建构建器
      Parameters:
      jarPath - JAR file path | JAR 文件路径
      Returns:
      builder | 构建器
    • fromDirectory

      public static IsoClassLoader.Builder fromDirectory(Path directory)
      Create builder from directory 从目录创建构建器
      Parameters:
      directory - directory path | 目录路径
      Returns:
      builder | 构建器
    • fromUrls

      public static IsoClassLoader.Builder fromUrls(URL... urls)
      Create builder from URLs 从 URL 创建构建器
      Parameters:
      urls - URLs | URL 数组
      Returns:
      builder | 构建器
    • loadClass

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

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • findClass

      protected Class<?> findClass(String name) throws ClassNotFoundException
      Find and define a class locally, enforcing bytecode policy checks before defineClass. 本地查找并定义类,在 defineClass 之前执行字节码策略检查。

      When a ClassLoadingPolicy is configured with maxBytecodeSize or a BytecodeVerifier, this method reads the raw bytecode, validates it against the policy, and then defines the class. Without a policy (or without bytecode-level constraints), delegates to the parent URLClassLoader.findClass(String).

      Overrides:
      findClass in class URLClassLoader
      Parameters:
      name - the binary name of the class | 类的二进制名称
      Returns:
      the resulting Class object | 结果 Class 对象
      Throws:
      ClassNotFoundException - if the class could not be found | 类未找到时抛出
    • loadClassLocally

      public Class<?> loadClassLocally(String name) throws ClassNotFoundException
      Force load class locally (from this classloader) 强制从本加载器加载类
      Parameters:
      name - class name | 类名
      Returns:
      loaded class | 加载的类
      Throws:
      ClassNotFoundException - if class not found | 类未找到时抛出
    • canLoad

      public boolean canLoad(String className)
      Check if class can be loaded 检查类是否可加载
      Parameters:
      className - class name | 类名
      Returns:
      true if can load | 可加载返回 true
    • getResource

      public URL getResource(String name)
      Overrides:
      getResource in class ClassLoader
    • getResources

      public Enumeration<URL> getResources(String name) throws IOException
      Overrides:
      getResources in class ClassLoader
      Throws:
      IOException
    • getResourceAsStream

      public InputStream getResourceAsStream(String name)
      Overrides:
      getResourceAsStream in class URLClassLoader
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class URLClassLoader
    • isClosed

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

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

      public String getLoaderName()
      Get loader name 获取加载器名称
      Returns:
      loader name | 加载器名称
    • getLeakDetection

      public LeakDetection getLeakDetection()
      Get leak detection level 获取泄漏检测级别
      Returns:
      leak detection level | 泄漏检测级别
    • getPolicy

      public ClassLoadingPolicy getPolicy()
      Get class loading policy 获取类加载策略
      Returns:
      class loading policy, or null if not set | 类加载策略,未设置则为 null