Class BytecodeClassLoader

All Implemented Interfaces:
Cloneable

public class BytecodeClassLoader extends SourceClassLoader
Bytecode-based class loader that also implements the SourceClassLoader interface.

Loads classes based on given bytecode or from the parent class loader. Depending on the given load mode, the parent class loader is searched first or last.

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

    • BytecodeClassLoader

      public BytecodeClassLoader(ClassLoader parent, LoadMode loadMode, Code code)
      constructor.
      Parameters:
      parent - parent class loader
      loadMode - load mode
      code - code with bytecode for one or more classes
      Throws:
      NullPointerException - if any of the arguments is null
      Since:
      1.0
  • Method Details

    • findClass

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

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

      public Class<?> loadMainClass(Source source)
      Description copied from class: SourceClassLoader
      loads the main class of the given source.

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

      Specified by:
      loadMainClass in class SourceClassLoader
      Parameters:
      source - source
      Returns:
      main class
    • loadClass

      public Class<?> loadClass(Source source, String name)
      Description copied from class: SourceClassLoader
      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.

      Specified by:
      loadClass in class SourceClassLoader
      Parameters:
      source - source
      name - class name
      Returns:
      class
    • findBytecodeClassLoaderBySource

      public BytecodeClassLoader findBytecodeClassLoaderBySource(Source source)
      Description copied from class: SourceClassLoader
      tries to find the bytecode class loader that can load classes that were created by compiling the given source.
      Specified by:
      findBytecodeClassLoaderBySource in class SourceClassLoader
      Parameters:
      source - source
      Returns:
      bytecode class loader if found, null otherwise
    • getLoadMode

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

      public static Class<?> loadMainClassBySource(ClassLoader classLoader, Source source)
      Helper method for loading the main class of the given source from the given class loader.
      Parameters:
      classLoader - class loader
      source - source
      Returns:
      main class
      Throws:
      LoadException - if loading failed, including if the class was not found
      Since:
      1.0
    • loadClassBySourceAndName

      public static Class<?> loadClassBySourceAndName(ClassLoader classLoader, Source source, String name)
      Helper method for loading a class with the given name and from the given source from the given class loader.
      Parameters:
      classLoader - class loader
      source - source
      name - class name
      Returns:
      class
      Throws:
      LoadException - if loading failed, including if the class was not found
      Since:
      1.0
    • clone

      public BytecodeClassLoader clone()
      Description copied from class: SourceClassLoader
      creates a clone with identical behavior, typically sharing the same bytecode.
      Specified by:
      clone in class SourceClassLoader
      Returns:
      clone
    • 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
    • getCode

      public Code getCode()
      gets the code.
      Returns:
      code
      Since:
      1.0