- Groovy load java class
- Nested Class Summary
- Field Summary
- Constructor Summary
- Method Summary
- Methods inherited from class java.net.URLClassLoader
- Methods inherited from class java.security.SecureClassLoader
- Methods inherited from class java.lang.ClassLoader
- Methods inherited from class java.lang.Object
- Field Detail
- classCache
- sourceCache
- Constructor Detail
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- Method Detail
- setResourceLoader
- getResourceLoader
- defineClass
- parseClass
- parseClass
- parseClass
- generateScriptName
- parseClass
- parseClass
- parseClass
- getClassPath
- getPermissions
- createCompilationUnit
- createCollector
- defineClass
- loadClass
- getClassCacheEntry
- setClassCacheEntry
- removeClassCacheEntry
- addURL
- isRecompilable
- setShouldRecompile
- isShouldRecompile
- loadClass
- recompile
- loadClass
- loadClass
- getTimeStamp
- isSourceNewer
- addClasspath
- getLoadedClasses
- clearCache
- Class GroovyClassLoader
- Nested Class Summary
- Field Summary
- Constructor Summary
- Method Summary
- Methods inherited from class java.net.URLClassLoader
- Methods inherited from class java.security.SecureClassLoader
- Methods inherited from class java.lang.ClassLoader
- Methods inherited from class java.lang.Object
- Field Details
- classCache
- sourceCache
- Constructor Details
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- GroovyClassLoader
- Method Details
- setResourceLoader
- getResourceLoader
- defineClass
- hasCompatibleConfiguration
- parseClass
- parseClass
- parseClass
- generateScriptName
- parseClass
- parseClass
- parseClass
- getClassPath
- getPermissions
- createCompilationUnit
- createCollector
- defineClass
- loadClass
- getClassCacheEntry
- setClassCacheEntry
- removeClassCacheEntry
- addURL
- isRecompilable
- setShouldRecompile
- isShouldRecompile
- loadClass
- recompile
- loadClass
- loadClass
- getTimeStamp
- isSourceNewer
- addClasspath
- getLoadedClasses
- clearCache
- close
Groovy load java class
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
Field Summary
Constructor Summary
Method Summary
Methods inherited from class java.net.URLClassLoader
Methods inherited from class java.security.SecureClassLoader
Methods inherited from class java.lang.ClassLoader
Methods inherited from class java.lang.Object
Field Detail
classCache
protected final java.util.Map classCache
sourceCache
protected final java.util.Map sourceCache
Constructor Detail
GroovyClassLoader
GroovyClassLoader
public GroovyClassLoader(java.lang.ClassLoader loader)
GroovyClassLoader
creates a GroovyClassLoader using the given GroovyClassLoader as parent. This loader will get the parent’s CompilerConfiguration
GroovyClassLoader
public GroovyClassLoader(java.lang.ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath)
GroovyClassLoader
Method Detail
setResourceLoader
getResourceLoader
defineClass
public 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
parseClass
parseClass
parseClass
generateScriptName
public java.lang.String generateScriptName()
parseClass
Deprecated. Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.
parseClass
public java.lang.Class parseClass(GroovyCodeSource codeSource) throws CompilationFailedException
parseClass
public java.lang.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.
getClassPath
protected java.lang.String[] getClassPath()
getPermissions
protected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
createCompilationUnit
protected 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.
createCollector
protected GroovyClassLoader.ClassCollector createCollector(CompilationUnit unit, SourceUnit su)
defineClass
public java.lang.Class defineClass(java.lang.String name, byte[] b)
loadClass
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.
getClassCacheEntry
protected 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.
setClassCacheEntry
protected void setClassCacheEntry(java.lang.Class cls)
removeClassCacheEntry
protected void removeClassCacheEntry(java.lang.String name)
addURL
public void addURL(java.net.URL url)
isRecompilable
protected 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.
setShouldRecompile
public 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.
isShouldRecompile
public 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.
loadClass
recompile
(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.
loadClass
public java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException
loadClass
protected java.lang.Class loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
getTimeStamp
protected 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
isSourceNewer
protected boolean isSourceNewer(java.net.URL source, java.lang.Class cls) throws java.io.IOException
addClasspath
public void addClasspath(java.lang.String path)
getLoadedClasses
public java.lang.Class[] getLoadedClasses()
clearCache
Class GroovyClassLoader
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
Field Summary
Constructor Summary
Method Summary
Methods inherited from class java.net.URLClassLoader
Methods inherited from class java.security.SecureClassLoader
Methods inherited from class java.lang.ClassLoader
Methods inherited from class java.lang.Object
Field Details
classCache
sourceCache
Constructor Details
GroovyClassLoader
GroovyClassLoader
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)
GroovyClassLoader
Method Details
setResourceLoader
getResourceLoader
defineClass
Loads the given class node returning the implementation Class. WARNING: this compilation is not synchronized
hasCompatibleConfiguration
parseClass
parseClass
parseClass
generateScriptName
parseClass
parseClass
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.
getClassPath
getPermissions
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.
createCollector
defineClass
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.
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.
setClassCacheEntry
removeClassCacheEntry
addURL
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.
setShouldRecompile
sets 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.
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.
loadClass
public Class loadClass (String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) throws ClassNotFoundException, CompilationFailedException
recompile
(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.
loadClass
loadClass
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
isSourceNewer
addClasspath
getLoadedClasses
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.
Copyright © 2003-2022 The Apache Software Foundation. All rights reserved.