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.
Modifiers | Name | Description |
---|---|---|
static class |
GroovyClassLoader.ClassCollector |
|
static class |
GroovyClassLoader.InnerLoader |
Modifiers | Name | Description |
---|---|---|
protected EvictableCache<String, Class> |
classCache |
This cache contains the loaded classes or PARSING, if the class is currently parsed. |
protected StampedCommonCache<String, Class> |
sourceCache |
This cache contains mappings of file name to class. |
Constructor and description |
---|
GroovyClassLoader() Creates a GroovyClassLoader using the current Thread's context ClassLoader as parent. |
GroovyClassLoader(ClassLoader parent) 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) Creates a GroovyClassLoader using the given ClassLoader as parent. |
GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath) Creates a GroovyClassLoader. |
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. |
|
protected GroovyClassLoader.ClassCollector |
createCollector(CompilationUnit unit, SourceUnit su) creates a ClassCollector for a new compilation. |
|
protected CompilationUnit |
createCompilationUnit(CompilerConfiguration config, CodeSource source) creates a new CompilationUnit. |
|
public Class |
defineClass(String name, byte[] bytes) Converts an array of bytes into an instance of Class . |
|
public Class |
defineClass(ClassNode classNode, String file, String newCodeBase) Compiles the given ClassNode returning the resulting Class . |
|
public String |
genEncodingString(CharSequence chars) Generates an encoded string based on the specified characters and the defined encoding algorithm. |
|
public String |
generateScriptName() |
|
protected Class |
getClassCacheEntry(String name) gets a class from the class cache. |
|
protected String[] |
getClassPath() gets the currently used classpath. |
|
public Class[] |
getLoadedClasses() |
|
protected PermissionCollection |
getPermissions(CodeSource codeSource) |
|
public GroovyResourceLoader |
getResourceLoader() |
|
protected long |
getTimeStamp(Class cls) Gets the time stamp of a given class. |
|
public boolean |
hasCompatibleConfiguration(CompilerConfiguration config) Checks if this class loader has compatible CompilerConfiguration with the provided one. |
|
protected boolean |
isRecompilable(Class cls) Indicates if a class is recompilable. |
|
public 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. |
|
public Class<?> |
loadClass(String name) {@inheritDoc} |
|
protected Class<?> |
loadClass(String name, boolean resolve) Implemented here to check package access prior to returning an already-loaded class. |
|
public Class |
loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript) Loads a class from a file or a parent loader. |
|
public Class |
loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) Loads a class from a file or a parent loader. |
|
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. |
|
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. |
|
public void |
setResourceLoader(GroovyResourceLoader resourceLoader) |
|
public void |
setShouldRecompile(Boolean mode) sets if the recompilation should be enabled. |
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 |
This cache contains the loaded classes or PARSING, if the class is currently parsed.
This cache contains mappings of file name to class. It is used to bypass compilation.
Creates a GroovyClassLoader using the current Thread's context ClassLoader as parent.
Creates a GroovyClassLoader using the given ClassLoader as parent.
Creates a GroovyClassLoader using the given GroovyClassLoader as parent. The new loader will get the parent's CompilerConfiguration.
Creates a GroovyClassLoader using the given ClassLoader as parent.
Creates a GroovyClassLoader.
parent
- the parent class loaderconfig
- the compiler configurationuseConfigurationClasspath
- determines if the configurations classpath should be addedadds a classpath to this classloader.
path
- is a jar file or a directory.adds a URL to the classloader.
url
- the new classpath elementRemoves 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.
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.
creates a ClassCollector for a new compilation.
unit
- the compilationUnitsu
- the SourceUnitcreates 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.
config
- the compiler configuration, usually the same as for this class loadersource
- the source containing the initial file to compile, more files may follow during compilation Converts an array of bytes into an instance of Class
. Before the
class can be used it must be resolved.
Compiles the given ClassNode
returning the resulting Class
.
WARNING: compilation is not synchronized
Generates an encoded string based on the specified characters and the defined encoding algorithm. Supported algorithms currently are "md5" and sha256". An exception is throw for an unknown algorithm or if the JVM doesn't support the algorithm.
chars
- The characters to encode.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.
name
- of the classgets the currently used classpath.
Returns all Groovy classes loaded by this class loader.
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
cls
- the classChecks if this class loader has compatible CompilerConfiguration with the provided one.
config
- the compiler configuration to test for compatibilitytrue
if the provided config is exactly the same instance
of CompilerConfiguration as this loader hasIndicates if a class is recompilable. Recompilable 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 compilable 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.
cls
- the class to be tested. If null the method should return truegets 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.
Decides if the given source is newer than a class.
source
- the source we may want to compilecls
- the former class{@inheritDoc}
Implemented here to check package access prior to returning an already-loaded class.
Loads a class from a file or a parent loader. This method delegates to:
loadClass(name, lookupScriptFiles, preferClassOverScript, false);
Loads a class from a file or a parent loader.
name
- of the class to be loadedlookupScriptFiles
- if false no lookup at files is done at allpreferClassOverScript
- if true the file lookup is only done if there is no classresolve
- see ClassLoader.loadClassParses the given file into a Java class capable of being run
file
- the file name to parseParses the given text into a Java class capable of being run
text
- the text of the script/class to parsefileName
- the file name to use as the name of the classParses the given text into a Java class capable of being run
text
- the text of the script/class to parseParses 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.
shouldCacheSource
- if true then the generated class will be stored in the source cache(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.
source
- the source pointer for the compilationclassName
- the name of the class to be generatedoldClass
- a possible former classremoves a class from the class cache.
name
- of the classsets an entry in the class cache.
cls
- the classsets if the recompilation should be enabled. There are 3 possible values for this. Any value different from null overrides the value from the compiler configuration. true means to recompile if needed false means to never recompile.
mode
- the recompilation modeCopyright © 2003-2024 The Apache Software Foundation. All rights reserved.