Class LayeredClassLoader

All Implemented Interfaces:
Cloneable

public class LayeredClassLoader extends SourceClassLoader
SourceClassLoader based on layers of BytecodeClassLoader plus optionally a TopCodeCache.

Depending on load modes, classes are first searched in the parent class loader, the individual layers or optionally created on-the-fly from source as part of the top code cache.

Since:
1.0
Author:
Alain Stalder, Made in Switzerland.
  • Constructor Details

    • LayeredClassLoader

      protected LayeredClassLoader(LayeredClassLoader.Builder builder)
      constructor from builder, based on already compiled code layers.
      Parameters:
      builder - builder
      Since:
      1.0
    • LayeredClassLoader

      protected LayeredClassLoader(LayeredClassLoader.Builder builder, boolean fromSourcesLayers)
      constructor from builder, based on sources to compile to code layers or on already compiled code layers.
      Parameters:
      builder - builder
      fromSourcesLayers - if based on sources to compile to code layers or on already compiled code layers
      Throws:
      CompileException - if based on sources and compiling the sources layers failed
      Since:
      1.0
  • Method Details

    • loadClass

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

      public Class<?> loadMainClass(Source source)
      loads the main class of the given source.

      First searches for the source, then loads the main class that resulted from compiling the source.

      Specifically for LayeredClassLoader, the source is eventually compiled on-the-fly from the given source if top code cache is on. First, the class corresponding to the source is searched in static layers using findBytecodeClassLoaderBySource(Source). If not found, uses the source of the top code cache (compiling it, if necessary). If found, uses the source of the top code cache (compiling it, if necessary) if the source had changed since compiled in static layers (or top code cache) and if top load mode is current first.

      Specified by:
      loadMainClass in class SourceClassLoader
      Parameters:
      source - source
      Returns:
      main class
      Throws:
      CompileException - if compilation was necessary to load the class and failed
      LoadException - if loading failed, including if the class was not found
      Since:
      1.0
    • loadClass

      public Class<?> loadClass(Source source, String name)
      loads a class with the given name and from the given source.

      First searches for the source, only then for the class with given name as part of the classes that resulted from compiling the source.

      Specifically for LayeredClassLoader, the source is eventually compiled on-the-fly from the given source if top code cache is on. First, the class corresponding to the source is searched in static layers using findBytecodeClassLoaderBySource(Source). If not found, uses the source of the top code cache (compiling it, if necessary). If found, uses the source of the top code cache (compiling it, if necessary) if the source had changed since compiled in static layers (or top code cache) and if top load mode is current first.

      Specified by:
      loadClass in class SourceClassLoader
      Parameters:
      source - source
      name - class name
      Returns:
      class
      Throws:
      CompileException - if compilation was necessary to load the class and failed
      LoadException - if loading failed, including if the class was not found
      Since:
      1.0
    • findBytecodeClassLoaderBySource

      public BytecodeClassLoader findBytecodeClassLoaderBySource(Source source)
      tries to find the bytecode class loader that can load classes that were created by compiling the given source.

      Specifically for LayeredClassLoader, top code cache is not involved; the search is limited to static code layers and the parent class loader.

      Specified by:
      findBytecodeClassLoaderBySource in class SourceClassLoader
      Parameters:
      source - source
      Returns:
      bytecode class loader if found, null otherwise
      Since:
      1.0
    • getLoadMode

      public LoadMode getLoadMode()
      Description copied from class: SourceClassLoader
      gets the load mode.
      Specified by:
      getLoadMode in class SourceClassLoader
      Returns:
      load mode
    • clone

      public LayeredClassLoader clone()
      creates a clone with the same code layers and with shared top code cache.
      Specified by:
      clone in class SourceClassLoader
      Returns:
      clone
      Since:
      1.0
    • releaseClasses

      public void releaseClasses(ClassReleaser releaser)
      Description copied from class: SourceClassLoader
      release metadata for all classed ever loaded by this class loader.

      Allows to remove metadata associated by Groovy (or Java) with a class, which is often necessary to get on-the-fly garbage collection.

      Generally call only when really done using this class loader and all loaded classes; subsequently trying to use this class loader or its classes results generally in undefined behavior.

      Specified by:
      releaseClasses in class SourceClassLoader
      Parameters:
      releaser - class releaser
    • cloneWithSeparateTopCodeCache

      public LayeredClassLoader cloneWithSeparateTopCodeCache()
      creates a clone with the same code layers and a separate top code cache (initially with the same cached bytecode).
      Returns:
      clone
      Since:
      1.0
    • getCodeLayers

      public List<Code> getCodeLayers()
      gets the code layers.
      Returns:
      code layers
      Since:
      1.0
    • getBuilder

      public LayeredClassLoader.Builder getBuilder()
      gets the builder.
      Returns:
      builder
      Since:
      1.0
    • getTopCodeCache

      public TopCodeCache getTopCodeCache()
      gets the top code cache.
      Returns:
      top code cache
      Since:
      1.0