Groovy 2.2.0

groovy.lang
[Java] Class GroovyClassLoader

java.lang.Object
  java.lang.ClassLoader
      java.security.SecureClassLoader
          java.net.URLClassLoader
              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.

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 Map classCache

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

protected 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(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.

 
Method Summary
void addClasspath(String path)

void addURL(URL url)

void clearCache()

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

protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source)

Class defineClass(ClassNode classNode, String file, String newCodeBase)

Loads the given class node returning the implementation Class.

Class defineClass(String name, byte[] b)

String generateScriptName()

protected Class getClassCacheEntry(String name)

protected String[] getClassPath()

gets the currently used classpath.

Class[] getLoadedClasses()

protected PermissionCollection getPermissions(CodeSource codeSource)

GroovyResourceLoader getResourceLoader()

protected long getTimeStamp(Class cls)

protected boolean isRecompilable(Class cls)

Boolean isShouldRecompile()

protected boolean isSourceNewer(URL source, Class cls)

adds a classpath to this classloader.

Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript)

Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)

Class loadClass(String name)

Implemented here to check package access prior to returning an already loaded class.

protected Class loadClass(String name, boolean resolve)

This method will take a file name and try to "decode" any URL encoded characters.

Class parseClass(File file)

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

Class parseClass(String text, String fileName)

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

Class parseClass(String text)

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

Class parseClass(InputStream in, String fileName)

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

Class parseClass(GroovyCodeSource codeSource)

Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)

protected Class recompile(URL source, String className, Class oldClass)

(Re)Compiles the given source.

protected void removeClassCacheEntry(String name)

protected void setClassCacheEntry(Class cls)

void setResourceLoader(GroovyResourceLoader resourceLoader)

void setShouldRecompile(Boolean mode)

sets if the recompilation should be enable.

 
Methods inherited from class URLClassLoader
getResourceAsStream, newInstance, newInstance, findResource, findResources, close, getURLs, loadClass, getSystemClassLoader, getResource, getSystemResource, getSystemResourceAsStream, clearAssertionStatus, getParent, getResources, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 
Methods inherited from class SecureClassLoader
loadClass, getSystemClassLoader, getResource, getResourceAsStream, getSystemResource, getSystemResourceAsStream, clearAssertionStatus, getParent, getResources, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

classCache

protected final Map classCache
this cache contains the loaded classes or PARSING, if the class is currently parsed


sourceCache

protected final 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(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(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(ClassLoader loader, CompilerConfiguration config)
creates a GroovyClassLoader using the given ClassLoader as parent.


 
Method Detail

addClasspath

public void addClasspath(String path)

Returns all Groovy classes loaded by this class loader.

Returns:
all classes loaded by this class loader


addURL

public void addURL(URL url)


clearCache

public void clearCache()


createCollector

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


createCompilationUnit

protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source)


defineClass

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


defineClass

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


generateScriptName

public String generateScriptName()


getClassCacheEntry

protected Class getClassCacheEntry(String name)


getClassPath

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


getLoadedClasses

public Class[] getLoadedClasses()


getPermissions

protected PermissionCollection getPermissions(CodeSource codeSource)


getResourceLoader

public GroovyResourceLoader getResourceLoader()


getTimeStamp

protected long getTimeStamp(Class cls)


isRecompilable

protected boolean isRecompilable(Class cls)


isShouldRecompile

public Boolean isShouldRecompile()


isSourceNewer

protected boolean isSourceNewer(URL source, Class cls)
adds a classpath to this classloader.
Parameters:
path - is a jar file or a directory.
See Also:
addURL(URL)


loadClass

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


loadClass

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


loadClass

@Overrideprotected long getTimeStamp(Class cls) {
public Class loadClass(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:
ClassLoader.loadClass


loadClass

protected Class loadClass(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 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


parseClass

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


parseClass

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


parseClass

public Class parseClass(InputStream in, String fileName)
deprecated:
Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.


parseClass

public Class parseClass(GroovyCodeSource codeSource)


parseClass

public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)


recompile

protected Class recompile(URL source, String className, 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(String name)


setClassCacheEntry

protected void setClassCacheEntry(Class cls)


setResourceLoader

public void setResourceLoader(GroovyResourceLoader resourceLoader)


setShouldRecompile

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


 

Copyright © 2003-2013 The Codehaus. All rights reserved.