Package: groovy.lang

## [Java] Class GroovyClassLoader

- groovy.lang.GroovyClassLoader

-     public class GroovyClassLoader
      extends URLClassLoader

  A ClassLoader which can load Groovy classes. The loaded classes are cached, classes from other classloaders should not be cached. To be able to load a script that was asked for earlier but was created later it is essential not to keep anything like a "class not found" information for that class name. This includes possible parent loaders. Classes that are not cached are always reloaded.

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `static class` | `GroovyClassLoader.ClassCollector` |
    `static class` | `GroovyClassLoader.InnerLoader` |

  - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovyClassLoader()` creates a GroovyClassLoader using the current Thread's context Class loader as parent.
    `GroovyClassLoader(ClassLoader loader)` creates a GroovyClassLoader using the given ClassLoader as parent
    `GroovyClassLoader(GroovyClassLoader parent)` creates a GroovyClassLoader using the given GroovyClassLoader as parent.
    `GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath)` creates a GroovyClassLoader.
    `GroovyClassLoader(ClassLoader loader, CompilerConfiguration config)` creates a GroovyClassLoader using the given ClassLoader as parent.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addClasspath(String path)` adds a classpath to this classloader.
    `` | `public void` | `addURL(URL url)` adds a URL to the classloader.
    `` | `public void` | `clearCache()` Removes all classes from the class cache.
    `` | `public void` | `close()` Closes this GroovyClassLoader and clears any caches it maintains.
    `` | `public Class` | `defineClass(ClassNode classNode, String file, String newCodeBase)` Loads the given class node returning the implementation Class.
    `` | `public Class` | `defineClass(String name, byte[] bytes)` Converts an array of bytes into an instance of `Class`.
    `` | `public String` | `generateScriptName()`
    `` | `public Class[]` | `getLoadedClasses()`
    `` | `public GroovyResourceLoader` | `getResourceLoader()`
    `` | `public boolean` | `hasCompatibleConfiguration(CompilerConfiguration config)` Check if this class loader has compatible CompilerConfiguration with the provided one.
    `` | `public Boolean` | `isShouldRecompile()` gets the currently set recompilation mode. null means, the compiler configuration is used.
    `` | `public Class` | `loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript)` loads a class from a file or a parent classloader.
    `` | `public Class` | `loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)` loads a class from a file or a parent classloader.
    `` | `public Class<?>` | `loadClass(String name)`
    `` | `public URL` | `loadGroovySource(String filename)`
    `` | `public Class` | `parseClass(File file)` Parses the given file into a Java class capable of being run
    `` | `public Class` | `parseClass(String text, String fileName)` Parses the given text into a Java class capable of being run
    `` | `public Class` | `parseClass(String text)` Parses the given text into a Java class capable of being run
    `` | `public Class` | `parseClass(Reader reader, String fileName)`
    `` | `public Class` | `parseClass(GroovyCodeSource codeSource)`
    `` | `public Class` | `parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)` Parses the given code source into a Java class.
    `` | `public ProtectionDomain` | `run()`
    `` | `public void` | `setResourceLoader(GroovyResourceLoader resourceLoader)`
    `` | `public void` | `setShouldRecompile(Boolean mode)` sets if the recompilation should be enable.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class URLClassLoader` | `newInstance, newInstance, findResource, getResourceAsStream, findResources, close, getURLs, getName, loadClass, getPlatformClassLoader, getSystemClassLoader, getSystemResourceAsStream, getSystemResource, getResource, getResources, getDefinedPackage, resources, isRegisteredAsParallelCapable, getSystemResources, getParent, getUnnamedModule, getDefinedPackages, setDefaultAssertionStatus, setPackageAssertionStatus, setClassAssertionStatus, clearAssertionStatus, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll`

- - ### Constructor Detail

    - #### public GroovyClassLoader()

      creates a GroovyClassLoader using the current Thread's context Class loader as parent.

    - #### public GroovyClassLoader(ClassLoader loader)

      creates a GroovyClassLoader using the given ClassLoader as parent

    - #### public GroovyClassLoader(GroovyClassLoader parent)

      creates a GroovyClassLoader using the given GroovyClassLoader as parent. This loader will get the parent's CompilerConfiguration

    - #### public GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath)

      creates a GroovyClassLoader.

      Parameters:
        `parent` - the parent class loader
        `config` - the compiler configuration
        `useConfigurationClasspath` - determines if the configurations classpath should be added

    - #### public GroovyClassLoader(ClassLoader loader, CompilerConfiguration config)

      creates a GroovyClassLoader using the given ClassLoader as parent.

  - ### Method Detail

    - #### @SuppressWarnings("removal")
      public void addClasspath(String path)

      adds a classpath to this classloader.

      Parameters:
        `path` - is a jar file or a directory.
      See Also:
        addURL(URL)

    - #### @Override
      public void addURL(URL url)

      adds a URL to the classloader.

      Parameters:
        `url` - the new classpath element

    - #### public void clearCache()

      Removes all classes from the class cache.

      In addition to internal caches this method also clears any previously set MetaClass information for the given set of classes being removed.

      See Also:
        getClassCacheEntry(String)
        setClassCacheEntry(Class)
        removeClassCacheEntry(String)

    - #### @Override
      public void close()

      Closes this GroovyClassLoader and clears any caches it maintains.

      No use should be made of this instance after this method is invoked. Any classes that are already loaded are still accessible.

      throws:
        IOException
      See Also:
        URLClassLoader.close
        clearCache()
      Since:
        2.5.0

    - #### public Class defineClass(ClassNode classNode, String file, String newCodeBase)

      Loads the given class node returning the implementation Class.

      WARNING: this compilation is not synchronized

      Parameters:
        classNode
      Returns:
        a class

    - #### public Class defineClass(String name, byte[] bytes)

      Converts an array of bytes into an instance of `Class`.

    - #### public String generateScriptName()

    - #### public Class[] getLoadedClasses()

      Returns all Groovy classes loaded by this class loader.

      Returns:
        all classes loaded by this class loader

    - #### public GroovyResourceLoader getResourceLoader()

    - #### public boolean hasCompatibleConfiguration(CompilerConfiguration config)

      Check if this class loader has compatible CompilerConfiguration with the provided one.

      Parameters:
        `config` - the compiler configuration to test for compatibility
      Returns:
        `true` if the provided config is exactly the same instance of CompilerConfiguration as this loader has

    - #### public Boolean isShouldRecompile()

      gets the currently set recompilation mode. null means, the compiler configuration is used. False means no recompilation and true means that recompilation will be done if needed.

      Returns:
        the recompilation mode

    - #### public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript)

      loads a class from a file or a parent classloader. This method does call loadClass(String, boolean, boolean, boolean) with the last parameter set to false.

      throws:
        CompilationFailedException if compilation was not successful

    - #### @SuppressWarnings("removal")
      public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)

      loads a class from a file or a parent classloader.

      throws:
        ClassNotFoundException if the class could not be found
      throws:
        CompilationFailedException if the source file could not be compiled
      Parameters:
        `name` - of the class to be loaded
        `lookupScriptFiles` - if false no lookup at files is done at all
        `preferClassOverScript` - if true the file lookup is only done if there is no class
        `resolve` - see ClassLoader.loadClass
      Returns:
        the class found or the class created from a file lookup

    - #### @Override
      public Class<?> loadClass(String name)

    - #### @Override
      @SuppressWarnings("removal")
      public URL loadGroovySource(String filename)

    - #### public Class parseClass(File file)

      Parses the given file into a Java class capable of being run

      Parameters:
        `file` - the file name to parse
      Returns:
        the main class defined in the given script

    - #### public Class parseClass(String text, String fileName)

      Parses the given text into a Java class capable of being run

      Parameters:
        `text` - the text of the script/class to parse
        `fileName` - the file name to use as the name of the class
      Returns:
        the main class defined in the given script

    - #### public Class parseClass(String text)

      Parses the given text into a Java class capable of being run

      Parameters:
        `text` - the text of the script/class to parse
      Returns:
        the main class defined in the given script

    - #### public Class parseClass(Reader reader, String fileName)

    - #### public Class parseClass(GroovyCodeSource codeSource)

    - #### public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)

      Parses the given code source into a Java class. If there is a class file for the given code source, then no parsing is done, instead the cached class is returned.

      Parameters:
        `shouldCacheSource` - if true then the generated class will be stored in the source cache
      Returns:
        the main class defined in the given script

    - #### @Override
      public ProtectionDomain run()

    - #### public void setResourceLoader(GroovyResourceLoader resourceLoader)

    - #### public void setShouldRecompile(Boolean mode)

      sets if the recompilation should be enable. There are 3 possible values for this. Any value different than null overrides the value from the compiler configuration. true means to recompile if needed false means to never recompile.

      Parameters:
        `mode` - the recompilation mode
      See Also:
        CompilerConfiguration