Package groovy.lang
Class GroovyClassLoader
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
groovy.lang.GroovyClassLoader
- All Implemented Interfaces:
- java.io.Closeable,- java.lang.AutoCloseable
- Direct Known Subclasses:
- GroovyClassLoader.InnerLoader
public class GroovyClassLoader
extends java.net.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.
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classGroovyClassLoader.ClassCollectorstatic classGroovyClassLoader.InnerLoader
- 
Field SummaryFields Modifier and Type Field Description protected EvictableCache<java.lang.String,java.lang.Class>classCachethis cache contains the loaded classes or PARSING, if the class is currently parsedprotected StampedCommonCache<java.lang.String,java.lang.Class>sourceCacheThis cache contains mappings of file name to class.
- 
Constructor SummaryConstructors Constructor Description GroovyClassLoader()creates 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(java.lang.ClassLoader loader)creates a GroovyClassLoader using the given ClassLoader as parentGroovyClassLoader(java.lang.ClassLoader loader, CompilerConfiguration config)creates a GroovyClassLoader using the given ClassLoader as parent.GroovyClassLoader(java.lang.ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath)creates a GroovyClassLoader.
- 
Method SummaryModifier and Type Method Description voidaddClasspath(java.lang.String path)adds a classpath to this classloader.voidaddURL(java.net.URL url)adds a URL to the classloader.voidclearCache()Removes all classes from the class cache.voidclose()Closes this GroovyClassLoader and clears any caches it maintains.protected GroovyClassLoader.ClassCollectorcreateCollector(CompilationUnit unit, SourceUnit su)creates a ClassCollector for a new compilation.protected CompilationUnitcreateCompilationUnit(CompilerConfiguration config, java.security.CodeSource source)creates a new CompilationUnit.java.lang.ClassdefineClass(java.lang.String name, byte[] b)open up the super class define that takes raw bytesjava.lang.ClassdefineClass(ClassNode classNode, java.lang.String file, java.lang.String newCodeBase)Loads the given class node returning the implementation Class.java.lang.StringgenerateScriptName()protected java.lang.ClassgetClassCacheEntry(java.lang.String name)gets a class from the class cache.protected java.lang.String[]getClassPath()gets the currently used classpath.java.lang.Class[]getLoadedClasses()Returns all Groovy classes loaded by this class loader.protected java.security.PermissionCollectiongetPermissions(java.security.CodeSource codeSource)GroovyResourceLoadergetResourceLoader()protected longgetTimeStamp(java.lang.Class cls)gets the time stamp of a given class.booleanhasCompatibleConfiguration(CompilerConfiguration config)Check if this class loader has compatibleCompilerConfigurationwith the provided one.protected booleanisRecompilable(java.lang.Class cls)Indicates if a class is recompilable.java.lang.BooleanisShouldRecompile()gets the currently set recompilation mode.protected booleanisSourceNewer(java.net.URL source, java.lang.Class cls)Decides if the given source is newer than a class.java.lang.Class<?>loadClass(java.lang.String name)protected java.lang.ClassloadClass(java.lang.String name, boolean resolve)Implemented here to check package access prior to returning an already loaded class.java.lang.ClassloadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript)loads a class from a file or a parent classloader.java.lang.ClassloadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)loads a class from a file or a parent classloader.java.lang.ClassparseClass(GroovyCodeSource codeSource)java.lang.ClassparseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)Parses the given code source into a Java class.java.lang.ClassparseClass(java.io.File file)Parses the given file into a Java class capable of being runjava.lang.ClassparseClass(java.io.InputStream in, java.lang.String fileName)Deprecated.Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.java.lang.ClassparseClass(java.io.Reader reader, java.lang.String fileName)java.lang.ClassparseClass(java.lang.String text)Parses the given text into a Java class capable of being runjava.lang.ClassparseClass(java.lang.String text, java.lang.String fileName)Parses the given text into a Java class capable of being runprotected java.lang.Classrecompile(java.net.URL source, java.lang.String className, java.lang.Class oldClass)(Re)Compiles the given source.protected voidremoveClassCacheEntry(java.lang.String name)removes a class from the class cache.protected voidsetClassCacheEntry(java.lang.Class cls)sets an entry in the class cache.voidsetResourceLoader(GroovyResourceLoader resourceLoader)voidsetShouldRecompile(java.lang.Boolean mode)sets if the recompilation should be enable.Methods inherited from class java.net.URLClassLoaderdefinePackage, findClass, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstanceMethods inherited from class java.security.SecureClassLoaderdefineClass, defineClassMethods inherited from class java.lang.ClassLoaderclearAssertionStatus, 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, setSignersMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
- 
Field Details- 
classCachethis cache contains the loaded classes or PARSING, if the class is currently parsed
- 
sourceCacheThis cache contains mappings of file name to class. It is used to bypass compilation.
 
- 
- 
Constructor Details- 
GroovyClassLoaderpublic GroovyClassLoader()creates a GroovyClassLoader using the current Thread's context Class loader as parent.
- 
GroovyClassLoaderpublic GroovyClassLoader(java.lang.ClassLoader loader)creates a GroovyClassLoader using the given ClassLoader as parent
- 
GroovyClassLoadercreates a GroovyClassLoader using the given GroovyClassLoader as parent. This loader will get the parent's CompilerConfiguration
- 
GroovyClassLoaderpublic GroovyClassLoader(java.lang.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
 
- 
GroovyClassLoadercreates a GroovyClassLoader using the given ClassLoader as parent.
 
- 
- 
Method Details- 
setResourceLoader
- 
getResourceLoader
- 
defineClasspublic java.lang.Class defineClass(ClassNode classNode, java.lang.String file, java.lang.String newCodeBase)Loads the given class node returning the implementation Class.WARNING: this compilation is not synchronized - Parameters:
- classNode-
- Returns:
- a class
 
- 
hasCompatibleConfigurationCheck if this class loader has compatibleCompilerConfigurationwith the provided one.- Parameters:
- config- the compiler configuration to test for compatibility
- Returns:
- trueif the provided config is exactly the same instance of- CompilerConfigurationas this loader has
 
- 
parseClasspublic java.lang.Class parseClass(java.io.File file) throws CompilationFailedException, java.io.IOExceptionParses 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
- java.io.IOException
 
- 
parseClasspublic java.lang.Class parseClass(java.lang.String text, java.lang.String fileName) throws CompilationFailedExceptionParses 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
- Throws:
- CompilationFailedException
 
- 
parseClassParses 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
 
- 
generateScriptNamepublic java.lang.String generateScriptName()
- 
parseClasspublic java.lang.Class parseClass(java.io.Reader reader, java.lang.String fileName) throws CompilationFailedException- Throws:
- CompilationFailedException
 
- 
parseClass@Deprecated public java.lang.Class parseClass(java.io.InputStream in, java.lang.String fileName) throws CompilationFailedExceptionDeprecated.Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues. UseparseClassinstead- Throws:
- CompilationFailedException
 
- 
parseClass- Throws:
- CompilationFailedException
 
- 
parseClasspublic java.lang.Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedExceptionParses 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
 
- 
getClassPathprotected java.lang.String[] getClassPath()gets the currently used classpath.- Returns:
- a String[] containing the file information of the urls
- See Also:
- URLClassLoader.getURLs()
 
- 
getPermissionsprotected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)- Overrides:
- getPermissionsin class- java.net.URLClassLoader
 
- 
createCompilationUnitprotected CompilationUnit createCompilationUnit(CompilerConfiguration config, java.security.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.- Parameters:
- config- the compiler configuration, usually the same as for this class loader
- source- the source containing the initial file to compile, more files may follow during compilation
- Returns:
- the CompilationUnit
 
- 
createCollectorcreates a ClassCollector for a new compilation.- Parameters:
- unit- the compilationUnit
- su- the SourceUnit
- Returns:
- the ClassCollector
 
- 
defineClasspublic java.lang.Class defineClass(java.lang.String name, byte[] b)open up the super class define that takes raw bytes
- 
loadClasspublic java.lang.Class loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript) throws java.lang.ClassNotFoundException, CompilationFailedExceptionloads 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
- java.lang.ClassNotFoundException
 
- 
getClassCacheEntryprotected java.lang.Class getClassCacheEntry(java.lang.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.- Parameters:
- name- of the class
- Returns:
- the class stored for the given name
- See Also:
- removeClassCacheEntry(String),- setClassCacheEntry(Class),- clearCache()
 
- 
setClassCacheEntryprotected void setClassCacheEntry(java.lang.Class cls)sets an entry in the class cache.- Parameters:
- cls- the class
- See Also:
- removeClassCacheEntry(String),- getClassCacheEntry(String),- clearCache()
 
- 
removeClassCacheEntryprotected void removeClassCacheEntry(java.lang.String name)removes a class from the class cache.- Parameters:
- name- of the class
- See Also:
- getClassCacheEntry(String),- setClassCacheEntry(Class),- clearCache()
 
- 
addURLpublic void addURL(java.net.URL url)adds a URL to the classloader.- Overrides:
- addURLin class- java.net.URLClassLoader
- Parameters:
- url- the new classpath element
 
- 
isRecompilableprotected boolean isRecompilable(java.lang.Class cls)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:
- isSourceNewer(URL, Class)
 
- 
setShouldRecompilepublic void setShouldRecompile(java.lang.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
 
- 
isShouldRecompilepublic java.lang.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.- Returns:
- the recompilation mode
 
- 
loadClasspublic java.lang.Class loadClass(java.lang.String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) throws java.lang.ClassNotFoundException, CompilationFailedExceptionloads a class from a file or a parent classloader.- Parameters:
- name- of the class to be loaded
- lookupScriptFiles- if false no lookup at files is done at all
- preferClassOverScript- if true the file lookup is only done if there is no class
- resolve- see- ClassLoader.loadClass(java.lang.String, boolean)
- Returns:
- the class found or the class created from a file lookup
- Throws:
- java.lang.ClassNotFoundException- if the class could not be found
- CompilationFailedException- if the source file could not be compiled
 
- 
recompileprotected java.lang.Class recompile(java.net.URL source, java.lang.String className, java.lang.Class oldClass) throws CompilationFailedException, java.io.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 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
- Throws:
- CompilationFailedException- if the compilation failed
- java.io.IOException- if the source is not readable
- See Also:
- isSourceNewer(URL, Class)
 
- 
loadClasspublic java.lang.Class<?> loadClass(java.lang.String name) throws java.lang.ClassNotFoundException- Overrides:
- loadClassin class- java.lang.ClassLoader
- Throws:
- java.lang.ClassNotFoundException
 
- 
loadClassprotected java.lang.Class loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundExceptionImplemented here to check package access prior to returning an already loaded class.- Overrides:
- loadClassin class- java.lang.ClassLoader
- Throws:
- CompilationFailedException- if the compilation failed
- java.lang.ClassNotFoundException- if the class was not found
- See Also:
- ClassLoader.loadClass(java.lang.String, boolean)
 
- 
getTimeStampprotected long getTimeStamp(java.lang.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- Parameters:
- cls- the class
- Returns:
- the time stamp
 
- 
isSourceNewerprotected boolean isSourceNewer(java.net.URL source, java.lang.Class cls) throws java.io.IOExceptionDecides if the given source is newer than a class.- Parameters:
- source- the source we may want to compile
- cls- the former class
- Returns:
- true if the source is newer, false else
- Throws:
- java.io.IOException- if it is not possible to open an connection for the given source
- See Also:
- getTimeStamp(Class)
 
- 
addClasspathpublic void addClasspath(java.lang.String path)adds a classpath to this classloader.- Parameters:
- path- is a jar file or a directory.
- See Also:
- addURL(URL)
 
- 
getLoadedClassespublic java.lang.Class[] getLoadedClasses()Returns all Groovy classes loaded by this class loader. - Returns:
- all classes loaded by this class loader
 
- 
clearCachepublic 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. 
- 
closepublic void close() throws java.io.IOExceptionCloses 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:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.io.Closeable
- Overrides:
- closein class- java.net.URLClassLoader
- Throws:
- java.io.IOException
- Since:
- 2.5.0
- See Also:
- URLClassLoader.close(),- clearCache()
 
 
-