public class GroovyClassLoader extends URLClassLoader
Modifier and Type | Class and Description |
---|---|
static class |
GroovyClassLoader.ClassCollector |
static class |
GroovyClassLoader.InnerLoader |
Modifier and Type | Field and Description |
---|---|
protected Map<String,Class> |
classCache
this cache contains the loaded classes or PARSING, if the class is currently parsed
|
protected Map<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
Class loader as parent.
|
GroovyClassLoader(ClassLoader loader)
creates a GroovyClassLoader using the given ClassLoader as parent
|
GroovyClassLoader(ClassLoader loader,
CompilerConfiguration config)
creates a GroovyClassLoader using the given ClassLoader as parent.
|
GroovyClassLoader(ClassLoader parent,
CompilerConfiguration config,
boolean useConfigurationClasspath)
creates a GroovyClassLoader.
|
GroovyClassLoader(GroovyClassLoader parent)
creates a GroovyClassLoader using the given GroovyClassLoader as parent.
|
Modifier and Type | Method and Description |
---|---|
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 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.
|
Class |
defineClass(ClassNode classNode,
String file,
String newCodeBase)
Loads the given class node returning the implementation Class.
|
Class |
defineClass(String name,
byte[] b)
open up the super class define that takes raw bytes
|
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.
|
protected boolean |
isSourceNewer(URL source,
Class cls)
Decides if the given source is newer than a class.
|
Class<?> |
loadClass(String name) |
protected Class |
loadClass(String name,
boolean resolve)
Implemented here to check package access prior to returning an
already loaded 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.
|
Class |
parseClass(File file)
Parses the given file into a Java class capable of being run
|
Class |
parseClass(GroovyCodeSource codeSource) |
Class |
parseClass(GroovyCodeSource codeSource,
boolean shouldCacheSource)
Parses the given code source into a Java class.
|
Class |
parseClass(InputStream in,
String fileName)
Deprecated.
Prefer using methods taking a Reader rather than an InputStream to avoid wrong encoding issues.
|
Class |
parseClass(String text)
Parses the given text 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
|
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.
|
close, definePackage, findClass, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstance
defineClass, defineClass
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
protected final Map<String,Class> classCache
public GroovyClassLoader()
public GroovyClassLoader(ClassLoader loader)
public GroovyClassLoader(GroovyClassLoader parent)
public GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath)
parent
- the parent class loaderconfig
- the compiler configurationuseConfigurationClasspath
- determines if the configurations classpath should be addedpublic GroovyClassLoader(ClassLoader loader, CompilerConfiguration config)
public void setResourceLoader(GroovyResourceLoader resourceLoader)
public GroovyResourceLoader getResourceLoader()
public Class defineClass(ClassNode classNode, String file, String newCodeBase)
WARNING: this compilation is not synchronized
classNode
- public Class parseClass(File file) throws CompilationFailedException, IOException
file
- the file name to parseCompilationFailedException
IOException
public Class parseClass(String text, String fileName) throws CompilationFailedException
text
- the text of the script/class to parsefileName
- the file name to use as the name of the classCompilationFailedException
public Class parseClass(String text) throws CompilationFailedException
text
- the text of the script/class to parseCompilationFailedException
public String generateScriptName()
@Deprecated public Class parseClass(InputStream in, String fileName) throws CompilationFailedException
CompilationFailedException
public Class parseClass(GroovyCodeSource codeSource) throws CompilationFailedException
CompilationFailedException
public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException
shouldCacheSource
- if true then the generated class will be stored in the source cacheCompilationFailedException
protected String[] getClassPath()
URLClassLoader.getURLs()
protected PermissionCollection getPermissions(CodeSource codeSource)
getPermissions
in class URLClassLoader
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source)
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 compilationprotected GroovyClassLoader.ClassCollector createCollector(CompilationUnit unit, SourceUnit su)
unit
- the compilationUnitsu
- the SourceUnitpublic Class defineClass(String name, byte[] b)
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript) throws ClassNotFoundException, CompilationFailedException
CompilationFailedException
- if compilation was not successfulClassNotFoundException
protected Class getClassCacheEntry(String name)
name
- of the classremoveClassCacheEntry(String)
,
setClassCacheEntry(Class)
,
clearCache()
protected void setClassCacheEntry(Class cls)
cls
- the classremoveClassCacheEntry(String)
,
getClassCacheEntry(String)
,
clearCache()
protected void removeClassCacheEntry(String name)
name
- of the classgetClassCacheEntry(String)
,
setClassCacheEntry(Class)
,
clearCache()
public void addURL(URL url)
addURL
in class URLClassLoader
url
- the new classpath elementprotected boolean isRecompilable(Class cls)
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 trueisSourceNewer(URL, Class)
public void setShouldRecompile(Boolean mode)
mode
- the recompilation modeCompilerConfiguration
public Boolean isShouldRecompile()
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) throws ClassNotFoundException, CompilationFailedException
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.loadClass(java.lang.String, boolean)
ClassNotFoundException
- if the class could not be foundCompilationFailedException
- if the source file could not be compiledprotected Class recompile(URL source, String className, Class oldClass) throws CompilationFailedException, IOException
source
- the source pointer for the compilationclassName
- the name of the class to be generatedoldClass
- a possible former classCompilationFailedException
- if the compilation failedIOException
- if the source is not readableisSourceNewer(URL, Class)
public Class<?> loadClass(String name) throws ClassNotFoundException
loadClass
in class ClassLoader
ClassNotFoundException
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
loadClass
in class ClassLoader
CompilationFailedException
- if the compilation failedClassNotFoundException
- if the class was not foundClassLoader.loadClass(java.lang.String, boolean)
protected long getTimeStamp(Class cls)
cls
- the classprotected boolean isSourceNewer(URL source, Class cls) throws IOException
source
- the source we may want to compilecls
- the former classIOException
- if it is not possible to open an
connection for the given sourcegetTimeStamp(Class)
public void addClasspath(String path)
path
- is a jar file or a directory.addURL(URL)
public Class[] getLoadedClasses()
Returns all Groovy classes loaded by this class loader.
public void clearCache()