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.
Notice that this class loader does not support resoures in the sense of ClassLoader.getResource(String)
,
ClassLoader.getResourceAsStream(String)
nd ClassLoader.getResources(String)
.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Set
<UnitCompiler> private boolean
private boolean
private boolean
private final JavaSourceIClassLoader
ThisMap
keeps those classes which were already compiled, but not yet defined i.e.Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
protectionDomainFactory
-
Constructor Summary
ConstructorsConstructorDescriptionJavaSourceClassLoader
(ClassLoader parentClassLoader) JavaSourceClassLoader
(ClassLoader parentClassLoader, File[] sourcePath, String characterEncoding) Sets 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 characterEncoding) Constructs aJavaSourceClassLoader
that finds Java source code through a givenResourceFinder
.JavaSourceClassLoader
(ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader) Constructs aJavaSourceClassLoader
that finds classes through anJavaSourceIClassLoader
. -
Method Summary
Modifier and TypeMethodDescriptionprivate Class
<?> defineBytecode
(String className, byte[] ba) protected Class
<?> Implementation ofClassLoader.findClass(String)
.generateBytecodes
(String name) Finds, scans, parses the right compilation unit.void
setCompileErrorHandler
(ErrorHandler compileErrorHandler) void
setDebuggingInfo
(boolean debugSource, boolean debugLines, boolean debugVars) void
setSourceCharset
(Charset charset) void
setSourceFinder
(ResourceFinder sourceFinder) void
setSourcePath
(File[] sourcePath) void
setTargetVersion
(int version) void
setWarningHandler
(WarningHandler warningHandler) Methods inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
getResourceAsStream, main, setProtectionDomainFactory, setSourceFileCharacterEncoding
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, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
compiledUnitCompilers
-
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
-
debugSource
private boolean debugSource -
debugLines
private boolean debugLines -
debugVars
private boolean debugVars
-
-
Constructor Details
-
JavaSourceClassLoader
public JavaSourceClassLoader() -
JavaSourceClassLoader
-
JavaSourceClassLoader
public JavaSourceClassLoader(ClassLoader parentClassLoader, @Nullable File[] sourcePath, @Nullable String characterEncoding) Sets 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
sourcePath
- A collection of directories that are searched for Java source files in the given ordercharacterEncoding
- The encoding of the Java source files (null
for platform default encoding)
-
JavaSourceClassLoader
public JavaSourceClassLoader(ClassLoader parentClassLoader, ResourceFinder sourceFinder, @Nullable String characterEncoding) 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 filescharacterEncoding
- The encoding of the Java source files (null
for platform default encoding)
-
JavaSourceClassLoader
Constructs aJavaSourceClassLoader
that finds classes through anJavaSourceIClassLoader
.
-
-
Method Details
-
setSourcePath
- Specified by:
setSourcePath
in classAbstractJavaSourceClassLoader
- Parameters:
sourcePath
- The sequence of directories to search for Java source files
-
setSourceFinder
- Specified by:
setSourceFinder
in classAbstractJavaSourceClassLoader
- Parameters:
sourceFinder
- Is used when searching for Java source files
-
setSourceCharset
- Specified by:
setSourceCharset
in classAbstractJavaSourceClassLoader
- Parameters:
charset
- The character set to using when reading characters from a source file
-
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
-
setTargetVersion
public void setTargetVersion(int version) -
setCompileErrorHandler
- See Also:
-
setWarningHandler
- See Also:
-
findClass
Implementation ofClassLoader.findClass(String)
.- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
generateBytecodes
Finds, scans, parses 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
-
defineBytecode
- Throws:
ClassFormatError
- See Also:
-