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
A
ClassLoader
that, unlike usual ClassLoader
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 the JavaSourceClassLoader
and create
a new one.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
Field Summary
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
optionalProtectionDomainFactory
-
Constructor Summary
ConstructorsConstructorDescriptionJavaSourceClassLoader
(ClassLoader parentClassLoader) JavaSourceClassLoader
(ClassLoader parentClassLoader, File[] optionalSourcePath, 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
(ClassLoader parentClassLoader, ResourceFinder sourceFinder, String optionalCharacterEncoding) Set up aJavaSourceClassLoader
that finds Java™ source code through a givenResourceFinder
. -
Method Summary
Modifier and TypeMethodDescriptionprotected Class
Implementation ofClassLoader.findClass(String)
.protected Map
generateBytecodes
(String name) Find, scan, parse the right compilation unit.void
setCompileErrorHandler
(UnitCompiler.ErrorHandler optionalCompileErrorHandler) void
setDebuggingInfo
(boolean debugSource, boolean debugLines, boolean debugVars) void
setSourceFileCharacterEncoding
(String optionalCharacterEncoding) void
setSourcePath
(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
-
Constructor Details
-
JavaSourceClassLoader
public JavaSourceClassLoader() -
JavaSourceClassLoader
-
JavaSourceClassLoader
public JavaSourceClassLoader(ClassLoader parentClassLoader, File[] optionalSourcePath, 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(ClassLoader parentClassLoader, ResourceFinder sourceFinder, String optionalCharacterEncoding) Set up 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)
-
-
Method Details
-
setSourcePath
- Specified by:
setSourcePath
in classAbstractJavaSourceClassLoader
- Parameters:
sourcePath
- The sequence of directories to search for Java™ source files
-
setSourceFileCharacterEncoding
- 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
- See Also:
-
setWarningHandler
- See Also:
-
findClass
Implementation ofClassLoader.findClass(String)
.- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
generateBytecodes
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:
ClassNotFoundException
- on compilation problems
-