Groovy 1.7.0

groovy.lang
Class GroovyClassLoader

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

class GroovyClassLoader
extends URLClassLoader

todo:
multi threaded compiling of the same class but with different roots for compilation... T1 compiles A, which uses B, T2 compiles B... mark A and B as parsed and then synchronize compilation. Problems: How to synchronize? How to get error messages?


Nested Class Summary
class GroovyClassLoader.ClassCollector

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 GroovyClassLaoder.

GroovyClassLoader(ClassLoader loader, CompilerConfiguration config)

creates a GroovyClassLoader using the given ClassLoader as parent.

 
Method Summary
void addClasspath(String path)

adds a classpath to this classloader.

void addURL(URL url)

adds a URL to the classloader.

void clearCache()

removes all classes from the class cache.

protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su)

creates a ClassCollector for a new compilation.

protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source)

creates a new CompilationUnit.

Class defineClass(ClassNode classNode, String file)

Loads the given class node returning the implementation Class

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

Loads the given class node returning the implementation Class.

protected Class defineClass(String name, byte[] bytecode, ProtectionDomain domain)

A helper method to allow bytecode to be loaded. spg changed name to defineClass to make it more consistent with other ClassLoader methods

Class defineClass(String name, byte[] b)

open up the super class define that takes raw bytes

protected void expandClassPath(List pathList, String base, String classpath, boolean isManifestClasspath)

expands the classpath

String generateScriptName()

protected Class getClassCacheEntry(String name)

gets a class from the class cache.

protected String[] getClassPath()

gets the currently used classpath.

Class[] getLoadedClasses()

Returns all Groovy classes loaded by this class loader.

protected PermissionCollection getPermissions(CodeSource codeSource)

GroovyResourceLoader getResourceLoader()

protected long getTimeStamp(Class cls)

gets the time stamp of a given class.

protected boolean isRecompilable(Class cls)

Indicates if a class is recompilable.

Boolean isShouldRecompile()

gets the currently set recompilation mode. null means, the compiler configuration is used.

protected boolean isSourceNewer(URL source, Class cls)

Decides if the given source is newer than a class.

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

loads a class from a file or a parent classloader.

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

loads a class from a file or a parent classloader.

protected Class loadClass(String name, boolean resolve)

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

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)

Parses the given character stream into a Java class capable of being run

Class parseClass(InputStream in, String fileName)

Class parseClass(GroovyCodeSource codeSource)

Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)

Parses the given code source into a Java class.

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

(Re)Compiles the given source.

protected void removeClassCacheEntry(String name)

removes a class from the class cache.

protected void setClassCacheEntry(Class cls)

sets an entry in the class cache.

void setResourceLoader(GroovyResourceLoader resourceLoader)

void setShouldRecompile(Boolean mode)

sets if the recompilation should be enable.

 
Methods inherited from class URLClassLoader
newInstance, newInstance, findResource, findResources, getURLs, loadClass, getSystemClassLoader, getResource, getResourceAsStream, getSystemResource, getSystemResourceAsStream, clearAssertionStatus, getParent, getResources, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 
Methods inherited from class SecureClassLoader
loadClass, getSystemClassLoader, getResource, getResourceAsStream, getSystemResource, getSystemResourceAsStream, clearAssertionStatus, getParent, getResources, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 
Methods inherited from class ClassLoader
loadClass, getSystemClassLoader, getResource, getResourceAsStream, getSystemResource, getSystemResourceAsStream, clearAssertionStatus, getParent, getResources, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, 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 GroovyClassLaoder.
param:
parent the parent class loader
param:
config the compiler configuration
param:
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)
adds a classpath to this classloader.
param:
path is a jar file or a directory.
see:
#addURL(URL)


addURL

public void addURL(URL url)
adds a URL to the classloader.
param:
url the new classpath element


clearCache

public void clearCache()
removes all classes from the class cache.
see:
#getClassCacheEntry(String)
see:
#setClassCacheEntry(Class)
see:
#removeClassCacheEntry(String)


createCollector

protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su)
creates a ClassCollector for a new compilation.
param:
unit the compilationUnit
param:
su the SoruceUnit
return:
the ClassCollector


createCompilationUnit

protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source)
creates a new CompilationUnit. If you want to add additional phase operations to the CompilationUnit (for example to inject additional methods, variables, fields), then you should overwrite this method.
param:
config the compiler configuration, usually the same as for this class loader
param:
source the source containing the initial file to compile, more files may follow during compilation
return:
the CompilationUnit


defineClass

public Class defineClass(ClassNode classNode, String file)
Loads the given class node returning the implementation Class
param:
classNode
return:
a class
deprecated:


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

param:
classNode
return:
a class


defineClass

protected Class defineClass(String name, byte[] bytecode, ProtectionDomain domain)
A helper method to allow bytecode to be loaded. spg changed name to defineClass to make it more consistent with other ClassLoader methods
deprecated:


defineClass

public Class defineClass(String name, byte[] b)
open up the super class define that takes raw bytes


expandClassPath

protected void expandClassPath(List pathList, String base, String classpath, boolean isManifestClasspath)
expands the classpath
param:
pathList an empty list that will contain the elements of the classpath
param:
classpath the classpath specified as a single string
deprecated:


generateScriptName

public String generateScriptName()


getClassCacheEntry

protected Class getClassCacheEntry(String name)
gets a class from the class cache. This cache contains only classes loaded through this class loader or an InnerLoader instance. If no class is stored for a specific name, then the method should return null.
param:
name of the class
return:
the class stored for the given name
see:
#removeClassCacheEntry(String)
see:
#setClassCacheEntry(Class)
see:
#clearCache()


getClassPath

protected String[] getClassPath()
gets the currently used classpath.
return:
a String[] containing the file information of the urls
see:
#getURLs()


getLoadedClasses

public Class[] getLoadedClasses()

Returns all Groovy classes loaded by this class loader.

return:
all classes loaded by this class loader


getPermissions

protected PermissionCollection getPermissions(CodeSource codeSource)


getResourceLoader

public GroovyResourceLoader getResourceLoader()


getTimeStamp

protected long getTimeStamp(Class cls)
gets the time stamp of a given class. For groovy generated classes this usually means to return the value of the static field __timeStamp. If the parameter doesn't have such a field, then Long.MAX_VALUE is returned
param:
cls the class
return:
the time stamp


isRecompilable

protected boolean isRecompilable(Class cls)
Indicates if a class is recompilable. Recompileable means, that the classloader will try to locate a groovy source file for this class and then compile it again, adding the resulting class as entry to the cache. Giving null as class is like a recompilation, so the method should always return true here. Only classes that are implementing GroovyObject are compileable and only if the timestamp in the class is lower than Long.MAX_VALUE.

NOTE: First the parent loaders will be asked and only if they don't return a class the recompilation will happen. Recompilation also only happen if the source file is newer.

param:
cls the class to be tested. If null the method should return true
return:
true if the class should be compiled again
see:
#isSourceNewer(URL, Class)


isShouldRecompile

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.
return:
the recompilation mode


isSourceNewer

protected boolean isSourceNewer(URL source, Class cls)
Decides if the given source is newer than a class.
param:
source the source we may want to compile
param:
cls the former class
return:
true if the source is newer, false else
throws:
IOException if it is not possible to open an connection for the given source
see:
#getTimeStamp(Class)


loadClass

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


loadClass

public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)
loads a class from a file or a parent classloader.
param:
name of the class to be loaded
param:
lookupScriptFiles if false no lookup at files is done at all
param:
preferClassOverScript if true the file lookup is only done if there is no class
param:
resolve @see ClassLoader#loadClass(java.lang.String, boolean)
return:
the class found or the class created from a file lookup
throws:
ClassNotFoundException if the class could not be found
throws:
CompilationFailedException if the source file could not be compiled


loadClass

protected Class loadClass(String name, boolean resolve)
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:
ClassLoader#loadClass


parseClass

public Class parseClass(File file)
Parses the given file into a Java class capable of being run
param:
file the file name to parse
return:
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
param:
text the text of the script/class to parse
param:
fileName the file name to use as the name of the class
return:
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
param:
text the text of the script/class to parse
return:
the main class defined in the given script


parseClass

public Class parseClass(InputStream in)
Parses the given character stream into a Java class capable of being run
param:
in an InputStream
return:
the main class defined in the given script
deprecated:
Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.


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)
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.
param:
shouldCacheSource if true then the generated class will be stored in the source cache
return:
the main class defined in the given script


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.
param:
source the source pointer for the compilation
param:
className the name of the class to be generated
param:
oldClass a possible former class
return:
the old class if the source wasn't new enough, the new class else
throws:
CompilationFailedException if the compilation failed
throws:
IOException if the source is not readable
see:
#isSourceNewer(URL, Class)


removeClassCacheEntry

protected void removeClassCacheEntry(String name)
removes a class from the class cache.
param:
name of the class
see:
#getClassCacheEntry(String)
see:
#setClassCacheEntry(Class)
see:
#clearCache()


setClassCacheEntry

protected void setClassCacheEntry(Class cls)
sets an entry in the class cache.
param:
cls the class
see:
#removeClassCacheEntry(String)
see:
#getClassCacheEntry(String)
see:
#clearCache()


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.
param:
mode the recompilation mode
see:
CompilerConfiguration


 

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