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 ClassLoaders, 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 AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Set<UnitCompiler> private booleanprivate booleanprivate booleanprivate final JavaSourceIClassLoaderThisMapkeeps those classes which were already compiled, but not yet defined i.e.Fields inherited from class AbstractJavaSourceClassLoader
protectionDomainFactory -
Constructor Summary
ConstructorsConstructorDescriptionJavaSourceClassLoader(ClassLoader parentClassLoader) JavaSourceClassLoader(ClassLoader parentClassLoader, File[] sourcePath, String characterEncoding) Sets up aJavaSourceClassLoaderthat 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 aJavaSourceClassLoaderthat finds Java source code through a givenResourceFinder.JavaSourceClassLoader(ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader) Constructs aJavaSourceClassLoaderthat 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.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler) voidsetDebuggingInfo(boolean debugSource, boolean debugLines, boolean debugVars) voidsetSourceCharset(Charset charset) voidsetSourceFinder(ResourceFinder sourceFinder) voidsetSourcePath(File[] sourcePath) voidsetTargetVersion(int version) voidsetWarningHandler(WarningHandler warningHandler) Methods inherited from class AbstractJavaSourceClassLoader
getResourceAsStream, main, setProtectionDomainFactory, setSourceFileCharacterEncodingMethods inherited from class 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
ThisMapkeeps 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 aJavaSourceClassLoaderthat finds Java source code in a file that resides in either of the directories specified by the given source path.- Parameters:
parentClassLoader- SeeClassLoadersourcePath- A collection of directories that are searched for Java source files in the given ordercharacterEncoding- The encoding of the Java source files (nullfor platform default encoding)
-
JavaSourceClassLoader
public JavaSourceClassLoader(ClassLoader parentClassLoader, ResourceFinder sourceFinder, @Nullable String characterEncoding) Constructs aJavaSourceClassLoaderthat 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- SeeClassLoadersourceFinder- Used to locate additional source filescharacterEncoding- The encoding of the Java source files (nullfor platform default encoding)
-
JavaSourceClassLoader
Constructs aJavaSourceClassLoaderthat finds classes through anJavaSourceIClassLoader.
-
-
Method Details
-
setSourcePath
- Specified by:
setSourcePathin classAbstractJavaSourceClassLoader- Parameters:
sourcePath- The sequence of directories to search for Java source files
-
setSourceFinder
- Specified by:
setSourceFinderin classAbstractJavaSourceClassLoader- Parameters:
sourceFinder- Is used when searching for Java source files
-
setSourceCharset
- Specified by:
setSourceCharsetin 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:
setDebuggingInfoin 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:
findClassin 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
nullif no source code could be found - Throws:
ClassNotFoundException- on compilation problems
-
defineBytecode
- Throws:
ClassFormatError- See Also:
-