Package org.codehaus.janino
Class JavaSourceClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
-
- org.codehaus.janino.JavaSourceClassLoader
-
- Direct Known Subclasses:
CachingJavaSourceClassLoader
public class JavaSourceClassLoader extends AbstractJavaSourceClassLoader
AClassLoader
that, unlike usualClassLoader
s, does not load byte code, but reads Java™ source code and then scans, parses, compiles and loads it into the virtual machine.As with any
ClassLoader
, it is not possible to "update" classes after they've been loaded. The way to achieve this is to give up on theJavaSourceClassLoader
and create a new one.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
debugLines
private boolean
debugSource
private boolean
debugVars
private JavaSourceIClassLoader
iClassLoader
private java.util.Map<java.lang.String,byte[]>
precompiledClasses
ThisMap
keeps those classes which were already compiled, but not yet defined i.e.-
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
optionalProtectionDomainFactory
-
-
Constructor Summary
Constructors Constructor Description JavaSourceClassLoader()
JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader)
JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, java.io.File[] optionalSourcePath, java.lang.String optionalCharacterEncoding)
Set up aJavaSourceClassLoader
that finds Java™ source code in a file that resides in either of the directories specified by the given source path.JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader)
Constructs aJavaSourceClassLoader
that finds classes through anJavaSourceIClassLoader
.JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, ResourceFinder sourceFinder, java.lang.String optionalCharacterEncoding)
Constructs aJavaSourceClassLoader
that finds Java™ source code through a givenResourceFinder
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.Class
defineBytecode(java.lang.String className, byte[] ba)
protected java.lang.Class
findClass(java.lang.String name)
Implementation ofClassLoader.findClass(String)
.protected java.util.Map<java.lang.String,byte[]>
generateBytecodes(java.lang.String name)
Find, scan, parse the right compilation unit.void
setCompileErrorHandler(ErrorHandler optionalCompileErrorHandler)
void
setDebuggingInfo(boolean debugSource, boolean debugLines, boolean debugVars)
void
setSourceFileCharacterEncoding(java.lang.String optionalCharacterEncoding)
void
setSourcePath(java.io.File[] sourcePath)
void
setWarningHandler(WarningHandler optionalWarningHandler)
-
Methods inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
main, setProtectionDomainFactory
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Field Detail
-
precompiledClasses
private final java.util.Map<java.lang.String,byte[]> precompiledClasses
ThisMap
keeps those classes which were already compiled, but not yet defined i.e. which were not yet passed toClassLoader.defineClass(java.lang.String, byte[], int, int)
.
-
iClassLoader
private final JavaSourceIClassLoader iClassLoader
-
debugSource
private boolean debugSource
-
debugLines
private boolean debugLines
-
debugVars
private boolean debugVars
-
-
Constructor Detail
-
JavaSourceClassLoader
public JavaSourceClassLoader()
-
JavaSourceClassLoader
public JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader)
-
JavaSourceClassLoader
public JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, java.io.File[] optionalSourcePath, java.lang.String optionalCharacterEncoding)
Set up aJavaSourceClassLoader
that finds Java™ source code in a file that resides in either of the directories specified by the given source path.- Parameters:
parentClassLoader
- SeeClassLoader
optionalSourcePath
- A collection of directories that are searched for Java™ source files in the given orderoptionalCharacterEncoding
- The encoding of the Java™ source files (null
for platform default encoding)
-
JavaSourceClassLoader
public JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, ResourceFinder sourceFinder, java.lang.String optionalCharacterEncoding)
Constructs aJavaSourceClassLoader
that finds Java™ source code through a givenResourceFinder
.You can specify to include certain debugging information in the generated class files, which is useful if you want to debug through the generated classes (see
Scanner(String, Reader)
).- Parameters:
parentClassLoader
- SeeClassLoader
sourceFinder
- Used to locate additional source filesoptionalCharacterEncoding
- The encoding of the Java™ source files (null
for platform default encoding)
-
JavaSourceClassLoader
public JavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader)
Constructs aJavaSourceClassLoader
that finds classes through anJavaSourceIClassLoader
.
-
-
Method Detail
-
setSourcePath
public void setSourcePath(java.io.File[] sourcePath)
- Specified by:
setSourcePath
in classAbstractJavaSourceClassLoader
- Parameters:
sourcePath
- The sequence of directories to search for Java™ source files
-
setSourceFileCharacterEncoding
public void setSourceFileCharacterEncoding(java.lang.String optionalCharacterEncoding)
- Specified by:
setSourceFileCharacterEncoding
in classAbstractJavaSourceClassLoader
- Parameters:
optionalCharacterEncoding
- ifnull
, use platform default encoding
-
setDebuggingInfo
public void setDebuggingInfo(boolean debugSource, boolean debugLines, boolean debugVars)
- Specified by:
setDebuggingInfo
in classAbstractJavaSourceClassLoader
- Parameters:
debugSource
- Whether line number debugging information should be generateddebugLines
- Whether variables debugging information should be generateddebugVars
- Whether source file debugging information should be generated
-
setCompileErrorHandler
public void setCompileErrorHandler(ErrorHandler optionalCompileErrorHandler)
-
setWarningHandler
public void setWarningHandler(WarningHandler optionalWarningHandler)
-
findClass
protected java.lang.Class findClass(java.lang.String name) throws java.lang.ClassNotFoundException
Implementation ofClassLoader.findClass(String)
.- Overrides:
findClass
in classjava.lang.ClassLoader
- Throws:
java.lang.ClassNotFoundException
-
generateBytecodes
protected java.util.Map<java.lang.String,byte[]> generateBytecodes(java.lang.String name) throws java.lang.ClassNotFoundException
Find, scan, parse the right compilation unit. Compile the parsed compilation unit to bytecode. This may cause more compilation units being scanned and parsed. Continue until all compilation units are compiled.- Returns:
- String name => byte[] bytecode, or
null
if no source code could be found - Throws:
java.lang.ClassNotFoundException
- on compilation problems
-
defineBytecode
private java.lang.Class defineBytecode(java.lang.String className, byte[] ba)
- Throws:
java.lang.ClassFormatError
- See Also:
AbstractJavaSourceClassLoader.setProtectionDomainFactory(org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.ProtectionDomainFactory)
-
-