Top Description Fields Constructors Methods
org.codehaus.janino

public Class JavaSourceClassLoader

extends AbstractJavaSourceClassLoader
Class Inheritance
Known Direct Subclasses
org.codehaus.janino.CachingJavaSourceClassLoader
Imports
java.io.File, .Reader, java.nio.charset.Charset, java.util.HashMap, .HashSet, .Map, .Set, org.codehaus.commons.compiler.AbstractJavaSourceClassLoader, .CompileException, .ErrorHandler, .InternalCompilerException, .WarningHandler, org.codehaus.commons.compiler.lang.ClassLoaders, org.codehaus.commons.compiler.util.Disassembler, org.codehaus.commons.compiler.util.resource.DirectoryResourceFinder, .PathResourceFinder, .ResourceFinder, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.UnitCompiler.ClassFileConsumer, org.codehaus.janino.util.ClassFile

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
ClassLoaders

Field Summary

Modifier and TypeField and Description
private final Set<UnitCompiler>
private boolean
private boolean
private boolean
private final JavaSourceIClassLoader
private final Map<String, byte[]>
precompiledClasses

This Map keeps those classes which were already compiled, but not yet defined i.e. which were not yet passed to ClassLoader#defineClass(java.lang.String, byte[], int, int).

Inherited from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader:
protectionDomainFactory

Constructor Summary

AccessConstructor and Description
public
public
public
JavaSourceClassLoader(ClassLoader parentClassLoader, File[]
A collection of directories that are searched for Java source files in the given order
sourcePath
,
String
The encoding of the Java source files (null for platform default encoding)
characterEncoding
)

Sets up a JavaSourceClassLoader that finds Java source code in a file that resides in either of the directories specified by the given source path.

public
JavaSourceClassLoader(ClassLoader parentClassLoader, ResourceFinder
Used to locate additional source files
sourceFinder
,
String
The encoding of the Java source files (null for platform default encoding)
characterEncoding
)

Constructs a JavaSourceClassLoader that finds Java source code through a given ResourceFinder.

public
JavaSourceClassLoader(ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader)

Constructs a JavaSourceClassLoader that finds classes through an JavaSourceIClassLoader.

Method Summary

Modifier and TypeMethod and Description
private Class<?>
defineBytecode(String className, byte[] ba)

protected Class<?>
protected Map<String, byte[]>

Returns:

String name => byte[] bytecode, or null if no source code could be found
generateBytecodes
(String name)

Finds, scans, parses the right compilation unit.

public void
setCompileErrorHandler(ErrorHandler compileErrorHandler)

public void
setDebuggingInfo(boolean
Whether line number debugging information should be generated
debugSource
,
boolean
Whether variables debugging information should be generated
debugLines
,
boolean
Whether source file debugging information should be generated
debugVars
)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setDebuggingInfo.

public void
setSourceCharset(Charset
The character set to using when reading characters from a source file
charset
)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceCharset.

public void
setSourceFinder(ResourceFinder
Is used when searching for Java source files
sourceFinder
)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceFinder.

public void
setSourcePath(File[]
The sequence of directories to search for Java source files
sourcePath
)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourcePath.

public void
setTargetVersion(int version)

public void
Inherited from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader:
getResourceAsStreammainsetProtectionDomainFactorysetSourceFileCharacterEncoding

Field Detail

compiledUnitCompilersback to summary
private final Set<UnitCompiler> compiledUnitCompilers
debugLinesback to summary
private boolean debugLines
debugSourceback to summary
private boolean debugSource
debugVarsback to summary
private boolean debugVars
iClassLoaderback to summary
private final JavaSourceIClassLoader iClassLoader
precompiledClassesback to summary
private final Map<String, byte[]> precompiledClasses

This Map keeps those classes which were already compiled, but not yet defined i.e. which were not yet passed to ClassLoader#defineClass(java.lang.String, byte[], int, int).

Constructor Detail

JavaSourceClassLoaderback to summary
public JavaSourceClassLoader()
JavaSourceClassLoaderback to summary
public JavaSourceClassLoader(ClassLoader parentClassLoader)
JavaSourceClassLoaderback to summary
public JavaSourceClassLoader(ClassLoader parentClassLoader, File[] sourcePath, String characterEncoding)

Sets up a JavaSourceClassLoader that finds Java source code in a file that resides in either of the directories specified by the given source path.

Parameters
parentClassLoader:ClassLoader

See ClassLoader

sourcePath:File[]

A collection of directories that are searched for Java source files in the given order

characterEncoding:String

The encoding of the Java source files (null for platform default encoding)

JavaSourceClassLoaderback to summary
public JavaSourceClassLoader(ClassLoader parentClassLoader, ResourceFinder sourceFinder, String characterEncoding)

Constructs a JavaSourceClassLoader that finds Java source code through a given ResourceFinder.

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#Scanner(String, Reader)).

Parameters
parentClassLoader:ClassLoader

See ClassLoader

sourceFinder:ResourceFinder

Used to locate additional source files

characterEncoding:String

The encoding of the Java source files (null for platform default encoding)

JavaSourceClassLoaderback to summary
public JavaSourceClassLoader(ClassLoader parentClassLoader, JavaSourceIClassLoader iClassLoader)

Constructs a JavaSourceClassLoader that finds classes through an JavaSourceIClassLoader.

Method Detail

defineBytecodeback to summary
private Class<?> defineBytecode(String className, byte[] ba)
See Also
setProtectionDomainFactory
findClassback to summary
protected Class<?> findClass(String name) throws ClassNotFoundException

Overrides java.lang.ClassLoader.findClass.

Implementation of ClassLoader#findClass(String).

Parameters
name:String

Doc from java.lang.ClassLoader.findClass.

The binary name of the class

Returns:Class<?>

Doc from java.lang.ClassLoader.findClass.

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:

Doc from java.lang.ClassLoader.findClass.

If the class could not be found

generateBytecodesback to summary
protected Map<String, byte[]> generateBytecodes(String name) throws ClassNotFoundException

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:Map<String, byte[]>

String name => byte[] bytecode, or null if no source code could be found

Annotations
@Nullable
Exceptions
ClassNotFoundException:
on compilation problems
setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler compileErrorHandler)
See Also
UnitCompiler#setCompileErrorHandler
setDebuggingInfoback to summary
public void setDebuggingInfo(boolean debugSource, boolean debugLines, boolean debugVars)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setDebuggingInfo.

Parameters
debugSource:boolean

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setDebuggingInfo.

Whether line number debugging information should be generated

debugLines:boolean

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setDebuggingInfo.

Whether variables debugging information should be generated

debugVars:boolean

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setDebuggingInfo.

Whether source file debugging information should be generated

Annotations
@Override
setSourceCharsetback to summary
public void setSourceCharset(Charset charset)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceCharset.

Parameters
charset:Charset

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceCharset.

The character set to using when reading characters from a source file

Annotations
@Override
setSourceFinderback to summary
public void setSourceFinder(ResourceFinder sourceFinder)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceFinder.

Parameters
sourceFinder:ResourceFinder

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourceFinder.

Is used when searching for Java source files

Annotations
@Override
setSourcePathback to summary
public void setSourcePath(File[] sourcePath)

Implements abstract org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourcePath.

Parameters
sourcePath:File[]

Doc from org.codehaus.commons.compiler.AbstractJavaSourceClassLoader.setSourcePath.

The sequence of directories to search for Java source files

Annotations
@Override
setTargetVersionback to summary
public void setTargetVersion(int version)
setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)
See Also
Parser#setWarningHandler(WarningHandler), UnitCompiler#setCompileErrorHandler