Top Description Inners Fields Constructors Methods
groovy.lang

public Class GroovyClassLoader

extends URLClassLoader
Class Inheritance
Known Direct Subclasses
groovy.lang.GroovyClassLoader.InnerLoader, groovy.util.GroovyScriptEngine.ScriptClassLoader, org.codehaus.groovy.runtime.ProxyGeneratorAdapter.InnerLoader
Imports
groovy.util.CharsetToolkit, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.ClassHelper, .ClassNode, .FieldNode, .InnerClassNode, .ModuleNode, org.codehaus.groovy.ast.expr.ConstantExpression, org.codehaus.groovy.classgen.GeneratorContext, .Verifier, org.codehaus.groovy.control.BytecodeProcessor, .CompilationFailedException, .CompilationUnit, .CompilePhase, .CompilerConfiguration, .Phases, .SourceUnit, org.codehaus.groovy.runtime.EncodingGroovyMethods, .IOGroovyMethods, .InvokerHelper, org.codehaus.groovy.runtime.memoize.EvictableCache, .StampedCommonCache, .UnlimitedConcurrentCache, org.codehaus.groovy.util.URLStreams, org.objectweb.asm.ClassVisitor, .ClassWriter, .Opcodes, java.io.File, .IOException, .InputStream, .InputStreamReader, .Reader, .UnsupportedEncodingException, java.net.MalformedURLException, .URI, .URISyntaxException, .URL, .URLClassLoader, .URLConnection, .URLDecoder, java.security.AccessController, .CodeSource, .NoSuchAlgorithmException, .Permission, .PermissionCollection, .Permissions, .PrivilegedAction, .ProtectionDomain, java.util.ArrayList, .Collection, .Enumeration, .Map

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
private static class

Field Summary

Modifier and TypeField and Description
protected final EvictableCache<String, Class<T>>
classCache

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

private final CompilerConfiguration
private static final Class<T>[]
private static final URL[]
private Boolean
private GroovyResourceLoader
private static int
protected final StampedCommonCache<String, Class<T>>
sourceCache

This cache contains mappings of file name to class.

private String

Constructor Summary

AccessConstructor and Description
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.

public
GroovyClassLoader(ClassLoader
the parent class loader
parent
,
CompilerConfiguration
the compiler configuration
config
,
boolean
determines if the configurations classpath should be added
useConfigurationClasspath
)

creates a GroovyClassLoader.

public
GroovyClassLoader(ClassLoader loader, CompilerConfiguration config)

creates a GroovyClassLoader using the given ClassLoader as parent.

Method Summary

Modifier and TypeMethod and Description
public void
addClasspath(final String
is a jar file or a directory.
path
)

adds a classpath to this classloader.

public void
addURL(URL
the new classpath element
url
)

Overrides java.net.URLClassLoader.addURL.

adds a URL to the classloader.
public void
clearCache()

Removes all classes from the class cache.

public void
close()

Overrides java.net.URLClassLoader.close.

Implements java.io.Closeable.close.

Closes this GroovyClassLoader and clears any caches it maintains.
protected GroovyClassLoader.ClassCollector

Returns:

the ClassCollector
createCollector
(CompilationUnit
the compilationUnit
unit
,
SourceUnit
the SourceUnit
su
)

creates a ClassCollector for a new compilation.

protected CompilationUnit

Returns:

the CompilationUnit
createCompilationUnit
(CompilerConfiguration
the compiler configuration, usually the same as for this class loader
config
,
CodeSource
the source containing the initial file to compile, more files may follow during compilation
source
)

creates a new CompilationUnit.

private static String
decodeFileName(String fileName)

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

public Class<T>

Returns:

a class
defineClass
(ClassNode classNode, String file, String newCodeBase)

Loads the given class node returning the implementation Class.

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

open up the super class define that takes raw bytes

private void
private Class<T>
private static File
fileReallyExists(URL ret, String fileWithoutPackage)

public synchronized String
private String
protected Class<T>

Returns:

the class stored for the given name
getClassCacheEntry
(String
of the class
name
)

gets a class from the class cache.

protected String[]

Returns:

a String[] containing the file information of the urls
getClassPath
()

gets the currently used classpath.

private static File
getFileForUrl(URL ret, String filename)

public Class<T>[]

Returns:

all classes loaded by this class loader
getLoadedClasses
()

Returns all Groovy classes loaded by this class loader.

protected PermissionCollection
getPermissions(CodeSource
the codesource
codeSource
)

Overrides java.net.URLClassLoader.getPermissions.

Returns the permissions for the given codesource object.
public GroovyResourceLoader
private URL
getSourceFile(String name, String extension)

protected long

Returns:

the time stamp
getTimeStamp
(Class<T>
the class
cls
)

gets the time stamp of a given class.

public boolean

Returns:

true if the provided config is exactly the same instance of CompilerConfiguration as this loader has
hasCompatibleConfiguration
(CompilerConfiguration
the compiler configuration to test for compatibility
config
)

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

private void
private static boolean
isFile(URL ret)

protected boolean

Returns:

true if the class should be compiled again
isRecompilable
(Class<T>
the class to be tested. If null the method should return true
cls
)

Indicates if a class is recompilable.

public Boolean

Returns:

the recompilation mode
isShouldRecompile
()

gets the currently set recompilation mode.

protected boolean

Returns:

true if the source is newer, false else
isSourceNewer
(URL
the source we may want to compile
source
,
Class<T>
the former class
cls
)

Decides if the given source is newer than a class.

public Class<T>
loadClass(final String name, boolean lookupScriptFiles, boolean preferClassOverScript)

loads a class from a file or a parent classloader.

public Class<T>

Returns:

the class found or the class created from a file lookup
loadClass
(final String
of the class to be loaded
name
,
boolean
if false no lookup at files is done at all
lookupScriptFiles
,
boolean
if true the file lookup is only done if there is no class
preferClassOverScript
,
boolean resolve)

loads a class from a file or a parent classloader.

public Class<?>
loadClass(String
The binary name of the class
name
)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.
protected Class<T>
loadClass(final String
The binary name of the class
name
,
boolean
If true then resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Implemented here to check package access prior to returning an already loaded class.
public Class<T>

Returns:

the main class defined in the given script
parseClass
(File
the file name to parse
file
)

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

public Class<T>

Returns:

the main class defined in the given script
parseClass
(final String
the text of the script/class to parse
text
,
final String
the file name to use as the name of the class
fileName
)

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

public Class<T>

Returns:

the main class defined in the given script
parseClass
(String
the text of the script/class to parse
text
)

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

public Class<T>
parseClass(final Reader reader, final String fileName)

public Class<T>
parseClass(final InputStream in, final String fileName)

Deprecated Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.
public Class<T>
public Class<T>

Returns:

the main class defined in the given script
parseClass
(final GroovyCodeSource codeSource, boolean
if true then the generated class will be stored in the source cache
shouldCacheSource
)

Parses the given code source into a Java class.

protected Class<T>

Returns:

the old class if the source wasn't new enough, the new class else
recompile
(URL
the source pointer for the compilation
source
,
String
the name of the class to be generated
className
,
Class<T>
a possible former class
oldClass
)

(Re)Compiles the given source.

protected void
removeClassCacheEntry(String
of the class
name
)

removes a class from the class cache.

protected void
setClassCacheEntry(Class<T>
the class
cls
)

sets an entry in the class cache.

public void
public void
setShouldRecompile(Boolean
the recompilation mode
mode
)

sets if the recompilation should be enable.

private static void
Inherited from java.net.URLClassLoader:
definePackagefindClassfindResourcefindResourcesgetResourceAsStreamgetURLsnewInstancenewInstance