Class AgiClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class AgiClassLoader extends URLClassLoader
A session-scoped URLClassLoader that maintains identity and holds in-memory compiled classes (AgiCompiledClass) across turns for an AGI session.

Serving as the parent classloader for ephemeral script loaders (such as Java.AnahataClassLoader), AgiClassLoader ensures that classes compiled via Java.compile(String, String, String, String[], Path) are defined exactly once in the session metaspace. This completely eliminates ClassCastException when instantiating compiled types in one turn and casting them in subsequent turns (e.g. via sessionMap).

Author:
anahata
  • Field Details

    • javaToolkit

      private final Java javaToolkit
      Reference to the owning Java toolkit for accessing compiled classes, parent-first whitelist, fallback bytes, and sibling loaders.
  • Constructor Details

    • AgiClassLoader

      public AgiClassLoader(List<URL> urls, Java javaToolkit, ClassLoader parent)
      Constructs a new AgiClassLoader.
      Parameters:
      urls - initial classpath URLs (from extraClassPath entries across compiled classes).
      javaToolkit - the owning Java toolkit instance.
      parent - the host parent classloader.
  • Method Details

    • addExtraClassPath

      public void addExtraClassPath(String extraClassPath)
      Adds extra classpath directory or JAR entries dynamically to this loader's URLs.
      Parameters:
      extraClassPath - path-separator delimited classpath string.
    • getRegisteredUrls

      public Set<URL> getRegisteredUrls()
      Returns a set of all URLs currently registered on this classloader.
      Returns:
      set of registered URLs.
    • isClassLoaded

      public boolean isClassLoaded(String name)
      Checks whether a class has already been loaded and defined by this classloader.
      Parameters:
      name - the binary name of the class.
      Returns:
      true if the class is already defined in this loader.
    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Implements session-scoped classloading:
      1. Returns previously loaded classes (guaranteeing single-instance identity).
      2. Delegates parent-first infrastructure classes to the host loader.
      3. Defines in-memory compiled bytecode from active AgiCompiledClasses in Java.
      4. Searches child-first URLs for libraries supplied in extra classpath.
      5. Falls back to the host parent classloader.
      6. Queries toolkit fallback bytes and extra sibling classloaders (e.g. JavaFX).
      Overrides:
      loadClass in class ClassLoader
      Parameters:
      name - the binary name of the class.
      resolve - if true, resolves the class.
      Returns:
      the resulting Class object.
      Throws:
      ClassNotFoundException - if the class cannot be found.
    • findAgiCompiledBytecode

      private byte[] findAgiCompiledBytecode(String name)
      Resolves bytecode for a class or any of its inner/nested classes from AgiCompiledClasses.
      Parameters:
      name - the binary class name.
      Returns:
      the bytecode byte array, or null if not found.