Groovy Documentation

## groovy.lang
[Java] Class GroovyClassLoader

    java.lang.Object
      java.lang.ClassLoader
          java.security.SecureClassLoader
              java.net.URLClassLoader
                  groovy.lang.GroovyClassLoader

    public class GroovyClassLoader
    extends java.net.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.

Authors:
  James Strachan
  Guillaume Laforge
  Steve Goetze
  Bing Ran
  Scott Stirling
  Jochen Theodorou

Version:
  \$Revision\$

Nested Class Summary
`static class` | `GroovyClassLoader.ClassCollector`
`static class` | `GroovyClassLoader.InnerLoader`

Field Summary
`protected java.util.Map` | `classCache` this cache contains the loaded classes or PARSING, if the class is currently parsed
`protected java.util.Map` | `sourceCache` This cache contains mappings of file name to class.

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

Method Summary
`void` | `addClasspath(java.lang.String path)`
`void` | `addURL(java.net.URL url)`
`void` | `clearCache()`
`protected GroovyClassLoader.ClassCollector` | `createCollector(CompilationUnit unit, SourceUnit su)`
`protected CompilationUnit` | `createCompilationUnit(CompilerConfiguration config, java.security.CodeSource source)`
`java.lang.Class` | `defineClass(ClassNode classNode, java.lang.String file, java.lang.String newCodeBase)` Loads the given class node returning the implementation Class.
`java.lang.Class` | `defineClass(java.lang.String name, byte[] b)`
`java.lang.String` | `generateScriptName()`
`protected java.lang.Class` | `getClassCacheEntry(java.lang.String name)`
`protected java.lang.String[]` | `getClassPath()` gets the currently used classpath.
`java.lang.Class[]` | `getLoadedClasses()`
`protected java.security.PermissionCollection` | `getPermissions(java.security.CodeSource codeSource)`
`GroovyResourceLoader` | `getResourceLoader()`
`protected long` | `getTimeStamp(java.lang.Class cls)`
`protected boolean` | `isRecompilable(java.lang.Class cls)`
`java.lang.Boolean` | `isShouldRecompile()`
`protected boolean` | `isSourceNewer(java.net.URL source, java.lang.Class cls)` adds a classpath to this classloader.
`java.lang.Class` | `loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript)`
`java.lang.Class` | `loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)`
`java.lang.Class` | `loadClass(java.lang.String name)` Implemented here to check package access prior to returning an already loaded class.
`protected java.lang.Class` | `loadClass(java.lang.String name, boolean resolve)` This method will take a file name and try to "decode" any URL encoded characters.
`java.lang.Class` | `parseClass(java.io.File file)` Parses the given file into a Java class capable of being run
`java.lang.Class` | `parseClass(java.lang.String text, java.lang.String fileName)` Parses the given text into a Java class capable of being run
`java.lang.Class` | `parseClass(java.lang.String text)` Parses the given text into a Java class capable of being run
`java.lang.Class` | `parseClass(java.io.InputStream in, java.lang.String fileName)` @deprecated Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.
`java.lang.Class` | `parseClass(GroovyCodeSource codeSource)`
`java.lang.Class` | `parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)`
`protected java.lang.Class` | `recompile(java.net.URL source, java.lang.String className, java.lang.Class oldClass)` (Re)Compiles the given source.
`protected void` | `removeClassCacheEntry(java.lang.String name)`
`protected void` | `setClassCacheEntry(java.lang.Class cls)`
`void` | `setResourceLoader(GroovyResourceLoader resourceLoader)`
`void` | `setShouldRecompile(java.lang.Boolean mode)` sets if the recompilation should be enable.

Methods inherited from class java.net.URLClassLoader

java.net.URLClassLoader#findResource(java.lang.String), java.net.URLClassLoader#findResources(java.lang.String), java.net.URLClassLoader#newInstance([Ljava.net.URL;, java.lang.ClassLoader), java.net.URLClassLoader#newInstance([Ljava.net.URL;), java.net.URLClassLoader#getResourceAsStream(java.lang.String), java.net.URLClassLoader#close(), java.net.URLClassLoader#getURLs(), java.net.URLClassLoader#loadClass(java.lang.String), java.net.URLClassLoader#getSystemClassLoader(), java.net.URLClassLoader#getResources(java.lang.String), java.net.URLClassLoader#getSystemResources(java.lang.String), java.net.URLClassLoader#getParent(), java.net.URLClassLoader#setDefaultAssertionStatus(boolean), java.net.URLClassLoader#setPackageAssertionStatus(java.lang.String, boolean), java.net.URLClassLoader#setClassAssertionStatus(java.lang.String, boolean), java.net.URLClassLoader#clearAssertionStatus(), java.net.URLClassLoader#getResource(java.lang.String), java.net.URLClassLoader#getSystemResourceAsStream(java.lang.String), java.net.URLClassLoader#getSystemResource(java.lang.String), java.net.URLClassLoader#wait(long), java.net.URLClassLoader#wait(long, int), java.net.URLClassLoader#wait(), java.net.URLClassLoader#equals(java.lang.Object), java.net.URLClassLoader#toString(), java.net.URLClassLoader#hashCode(), java.net.URLClassLoader#getClass(), java.net.URLClassLoader#notify(), java.net.URLClassLoader#notifyAll()

Methods inherited from class java.security.SecureClassLoader

java.security.SecureClassLoader#loadClass(java.lang.String), java.security.SecureClassLoader#getSystemClassLoader(), java.security.SecureClassLoader#getResources(java.lang.String), java.security.SecureClassLoader#getSystemResources(java.lang.String), java.security.SecureClassLoader#getParent(), java.security.SecureClassLoader#setDefaultAssertionStatus(boolean), java.security.SecureClassLoader#setPackageAssertionStatus(java.lang.String, boolean), java.security.SecureClassLoader#setClassAssertionStatus(java.lang.String, boolean), java.security.SecureClassLoader#clearAssertionStatus(), java.security.SecureClassLoader#getResourceAsStream(java.lang.String), java.security.SecureClassLoader#getResource(java.lang.String), java.security.SecureClassLoader#getSystemResourceAsStream(java.lang.String), java.security.SecureClassLoader#getSystemResource(java.lang.String), java.security.SecureClassLoader#wait(long), java.security.SecureClassLoader#wait(long, int), java.security.SecureClassLoader#wait(), java.security.SecureClassLoader#equals(java.lang.Object), java.security.SecureClassLoader#toString(), java.security.SecureClassLoader#hashCode(), java.security.SecureClassLoader#getClass(), java.security.SecureClassLoader#notify(), java.security.SecureClassLoader#notifyAll()

Field Detail

### classCache

    protected final java.util.Map classCache

  this cache contains the loaded classes or PARSING, if the class is currently parsed

### sourceCache

    protected final java.util.Map sourceCache

  This cache contains mappings of file name to class. It is used to bypass compilation.

Constructor Detail

### GroovyClassLoader

    public GroovyClassLoader()

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

### GroovyClassLoader

    public GroovyClassLoader(java.lang.ClassLoader loader)

  creates a GroovyClassLoader using the given ClassLoader as parent

### GroovyClassLoader

    public GroovyClassLoader(GroovyClassLoader parent)

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

### GroovyClassLoader

    public GroovyClassLoader(java.lang.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

### GroovyClassLoader

    public GroovyClassLoader(java.lang.ClassLoader loader, CompilerConfiguration config)

  creates a GroovyClassLoader using the given ClassLoader as parent.

Method Detail

### addClasspath

    public void addClasspath(java.lang.String path)

  Returns all Groovy classes loaded by this class loader.

  Returns:
    all classes loaded by this class loader

### addURL

    public void addURL(java.net.URL url)

### clearCache

    public void clearCache()

### createCollector

    protected GroovyClassLoader.ClassCollector createCollector(CompilationUnit unit, SourceUnit su)

### createCompilationUnit

    protected CompilationUnit createCompilationUnit(CompilerConfiguration config, java.security.CodeSource source)

### defineClass

    public java.lang.Class defineClass(ClassNode classNode, java.lang.String file, java.lang.String newCodeBase)

  Loads the given class node returning the implementation Class.

  WARNING: this compilation is not synchronized
  Parameters:
    classNode
  Returns:
    a class

### defineClass

    public java.lang.Class defineClass(java.lang.String name, byte[] b)

### generateScriptName

    public java.lang.String generateScriptName()

### getClassCacheEntry

    protected java.lang.Class getClassCacheEntry(java.lang.String name)

### getClassPath

    protected java.lang.String[] getClassPath()

  gets the currently used classpath.
  Returns:
    a String[] containing the file information of the urls
  See Also:
    getURLs()

### getLoadedClasses

    public java.lang.Class[] getLoadedClasses()

### getPermissions

    protected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)

### getResourceLoader

    public GroovyResourceLoader getResourceLoader()

### getTimeStamp

    protected long getTimeStamp(java.lang.Class cls)

### isRecompilable

    protected boolean isRecompilable(java.lang.Class cls)

### isShouldRecompile

    public java.lang.Boolean isShouldRecompile()

### isSourceNewer

    protected boolean isSourceNewer(java.net.URL source, java.lang.Class cls)

  adds a classpath to this classloader.
  Parameters:
    `path` - is a jar file or a directory.
  See Also:
    addURL(URL)

### loadClass

    public java.lang.Class loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript)

### loadClass

    public java.lang.Class loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)

### loadClass

    protected long getTimeStamp(Class cls) {
    public java.lang.Class loadClass(java.lang.String name)

  Implemented here to check package access prior to returning an already loaded class.
  throws:
    CompilationFailedException if the compilation failed
  throws:
    ClassNotFoundException if the class was not found
  See Also:
    java.lang.ClassLoader#loadClass(java.lang.String, boolean)

### loadClass

    protected java.lang.Class loadClass(java.lang.String name, boolean resolve)

  This method will take a file name and try to "decode" any URL encoded characters. For example if the file name contains any spaces this method call will take the resulting %20 encoded values and convert them to spaces.

  This method was added specifically to fix defect: Groovy-1787. The defect involved a situation where two scripts were sitting in a directory with spaces in its name. The code would fail when the class loader tried to resolve the file name and would choke on the URLEncoded space values.

### parseClass

    public java.lang.Class parseClass(java.io.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

### parseClass

    public java.lang.Class parseClass(java.lang.String text, java.lang.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

### parseClass

    public java.lang.Class parseClass(java.lang.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

### parseClass

    public java.lang.Class parseClass(java.io.InputStream in, java.lang.String fileName)

  deprecated:
    Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.

### parseClass

    public java.lang.Class parseClass(GroovyCodeSource codeSource)

### parseClass

    public java.lang.Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)

### recompile

    protected java.lang.Class recompile(java.net.URL source, java.lang.String className, java.lang.Class oldClass)

  (Re)Compiles the given source. This method starts the compilation of a given source, if the source has changed since the class was created. For this isSourceNewer is called.
  throws:
    CompilationFailedException if the compilation failed
  throws:
    IOException if the source is not readable
  Parameters:
    `source` - the source pointer for the compilation
    `className` - the name of the class to be generated
    `oldClass` - a possible former class
  Returns:
    the old class if the source wasn't new enough, the new class else
  See Also:
    isSourceNewer(URL, Class)

### removeClassCacheEntry

    protected void removeClassCacheEntry(java.lang.String name)

### setClassCacheEntry

    protected void setClassCacheEntry(java.lang.Class cls)

### setResourceLoader

    public void setResourceLoader(GroovyResourceLoader resourceLoader)

### setShouldRecompile

    public void setShouldRecompile(java.lang.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

Groovy Documentation