Package groovy.lang
Class GroovyClassLoader
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
groovy.lang.GroovyClassLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
GroovyClassLoader.InnerLoader
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 Class Summary
Modifier and TypeClassDescriptionstatic class
static class
-
Field Summary
Modifier and TypeFieldDescriptionprotected final EvictableCache<String,
Class> this cache contains the loaded classes or PARSING, if the class is currently parsedprotected final StampedCommonCache<String,
Class> This cache contains mappings of file name to class. -
Constructor Summary
ConstructorDescriptioncreates a GroovyClassLoader using the current Thread's context Class loader as parent.GroovyClassLoader
(GroovyClassLoader parent) creates a GroovyClassLoader using the given GroovyClassLoader as parent.GroovyClassLoader
(ClassLoader loader) creates a GroovyClassLoader using the given ClassLoader as parentGroovyClassLoader
(ClassLoader loader, CompilerConfiguration config) creates a GroovyClassLoader using the given ClassLoader as parent.GroovyClassLoader
(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath) creates a GroovyClassLoader. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClasspath
(String path) adds a classpath to this classloader.void
adds a URL to the classloader.void
Removes all classes from the class cache.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.defineClass
(String name, byte[] bytes) Converts an array of bytes into an instance ofClass
.defineClass
(ClassNode classNode, String file, String newCodeBase) Loads the given class node returning the implementation Class.protected Class
getClassCacheEntry
(String name) gets a class from the class cache.protected String[]
gets the currently used classpath.Class[]
Returns all Groovy classes loaded by this class loader.protected PermissionCollection
getPermissions
(CodeSource codeSource) protected long
getTimeStamp
(Class cls) gets the time stamp of a given class.boolean
Check if this class loader has compatibleCompilerConfiguration
with the provided one.protected boolean
isRecompilable
(Class cls) Indicates if a class is recompilable.gets the currently set recompilation mode.protected boolean
isSourceNewer
(URL source, Class cls) Decides if the given source is newer than a class.Class<?>
protected Class
Implemented here to check package access prior to returning an already loaded class.loads a class from a file or a parent classloader.loads a class from a file or a parent classloader.parseClass
(GroovyCodeSource codeSource) parseClass
(GroovyCodeSource codeSource, boolean shouldCacheSource) Parses the given code source into a Java class.parseClass
(File file) Parses the given file into a Java class capable of being runparseClass
(Reader reader, String fileName) parseClass
(String text) Parses the given text into a Java class capable of being runparseClass
(String text, String fileName) Parses the given text into a Java class capable of being runprotected Class
(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 java.net.URLClassLoader
definePackage, findClass, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstance
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
classCache
this cache contains the loaded classes or PARSING, if the class is currently parsed -
sourceCache
This cache contains mappings of file name to class. It is used to bypass compilation.
-
-
Constructor Details
-
GroovyClassLoader
public GroovyClassLoader()creates a GroovyClassLoader using the current Thread's context Class loader as parent. -
GroovyClassLoader
creates a GroovyClassLoader using the given ClassLoader as parent -
GroovyClassLoader
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 loaderconfig
- the compiler configurationuseConfigurationClasspath
- determines if the configurations classpath should be added
-
GroovyClassLoader
creates a GroovyClassLoader using the given ClassLoader as parent.
-
-
Method Details
-
setResourceLoader
-
getResourceLoader
-
defineClass
Loads the given class node returning the implementation Class.WARNING: this compilation is not synchronized
- Parameters:
classNode
-- Returns:
- a class
-
hasCompatibleConfiguration
Check if this class loader has compatibleCompilerConfiguration
with the provided one.- Parameters:
config
- the compiler configuration to test for compatibility- Returns:
true
if the provided config is exactly the same instance ofCompilerConfiguration
as this loader has
-
parseClass
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
- Throws:
CompilationFailedException
IOException
-
parseClass
Parses the given text into a Java class capable of being run- Parameters:
text
- the text of the script/class to parsefileName
- the file name to use as the name of the class- Returns:
- the main class defined in the given script
- Throws:
CompilationFailedException
-
parseClass
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
- Throws:
CompilationFailedException
-
generateScriptName
-
parseClass
- Throws:
CompilationFailedException
-
parseClass
- Throws:
CompilationFailedException
-
parseClass
public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException 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.- Parameters:
shouldCacheSource
- if true then the generated class will be stored in the source cache- Returns:
- the main class defined in the given script
- Throws:
CompilationFailedException
-
getClassPath
gets the currently used classpath.- Returns:
- a String[] containing the file information of the urls
- See Also:
-
getPermissions
- Overrides:
getPermissions
in classURLClassLoader
-
createCompilationUnit
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.- Parameters:
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- Returns:
- the CompilationUnit
-
createCollector
creates a ClassCollector for a new compilation.- Parameters:
unit
- the compilationUnitsu
- the SourceUnit- Returns:
- the ClassCollector
-
defineClass
Converts an array of bytes into an instance ofClass
.- Throws:
ClassFormatError
-
loadClass
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript) throws ClassNotFoundException, CompilationFailedException 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 successfulClassNotFoundException
-
getClassCacheEntry
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.- Parameters:
name
- of the class- Returns:
- the class stored for the given name
- See Also:
-
setClassCacheEntry
sets an entry in the class cache.- Parameters:
cls
- the class- See Also:
-
removeClassCacheEntry
removes a class from the class cache.- Parameters:
name
- of the class- See Also:
-
addURL
adds a URL to the classloader.- Overrides:
addURL
in classURLClassLoader
- Parameters:
url
- the new classpath element
-
isRecompilable
Indicates 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.
- Parameters:
cls
- the class to be tested. If null the method should return true- Returns:
- true if the class should be compiled again
- See Also:
-
setShouldRecompile
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:
-
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.- Returns:
- the recompilation mode
-
loadClass
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) throws ClassNotFoundException, CompilationFailedException loads a class from a file or a parent classloader.- Parameters:
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
- seeClassLoader.loadClass(java.lang.String, boolean)
- Returns:
- the class found or the class created from a file lookup
- Throws:
ClassNotFoundException
- if the class could not be foundCompilationFailedException
- if the source file could not be compiled
-
recompile
protected Class recompile(URL source, String className, Class oldClass) throws CompilationFailedException, IOException (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.- Parameters:
source
- the source pointer for the compilationclassName
- the name of the class to be generatedoldClass
- a possible former class- Returns:
- the old class if the source wasn't new enough, the new class else
- Throws:
CompilationFailedException
- if the compilation failedIOException
- if the source is not readable- See Also:
-
loadClass
- Overrides:
loadClass
in classClassLoader
- Throws:
ClassNotFoundException
-
loadClass
Implemented here to check package access prior to returning an already loaded class.- Overrides:
loadClass
in classClassLoader
- Throws:
CompilationFailedException
- if the compilation failedClassNotFoundException
- if the class was not found- See Also:
-
getTimeStamp
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- Parameters:
cls
- the class- Returns:
- the time stamp
-
isSourceNewer
Decides if the given source is newer than a class.- Parameters:
source
- the source we may want to compilecls
- the former class- Returns:
- true if the source is newer, false else
- Throws:
IOException
- if it is not possible to open an connection for the given source- See Also:
-
addClasspath
adds a classpath to this classloader.- Parameters:
path
- is a jar file or a directory.- See Also:
-
getLoadedClasses
Returns all Groovy classes loaded by this class loader.
- Returns:
- all classes loaded by this class loader
-
clearCache
public void clearCache()Removes 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.
-
close
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.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classURLClassLoader
- Throws:
IOException
- Since:
- 2.5.0
- See Also:
-