Top Description Fields Constructors Methods
org.codehaus.janino

public Class CachingJavaSourceClassLoader

extends JavaSourceClassLoader
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .File, .IOException, .InputStream, .OutputStream, java.util.HashMap, .Map, org.codehaus.commons.compiler.util.resource.DirectoryResourceCreator, .DirectoryResourceFinder, .PathResourceFinder, .Resource, .ResourceCreator, .ResourceFinder, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.util.ClassFile

A org.codehaus.janino.JavaSourceClassLoader that uses a resource storage provided by the application to cache compiled classes and thus saving unnecessary recompilations.

The application provides access to the resource storage through a pair of a org.codehaus.commons.compiler.util.resource.ResourceFinder and a org.codehaus.commons.compiler.util.resource.ResourceCreator (see CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).

See org.codehaus.janino.JavaSourceClassLoader#main(String[]) for an example how to use this class.

Notice: You must NOT rely on that this class stores some particular data in some particular resources through the given classFileCacheResourceFinder/Creator! These serve only as a means for the CachingJavaSourceClassLoader to persistently cache some data between invocations. In other words: If you want to compile .java files into .class files, then don't use this class but Compiler instead!

Field Summary

Modifier and TypeField and Description
private final ResourceCreator
private final ResourceFinder
private final ResourceFinder

Constructor Summary

AccessConstructor and Description
public
CachingJavaSourceClassLoader(ClassLoader parentClassLoader, File[]
Directories to scan for source files
sourcePath
,
String characterEncoding, File
Directory to use for caching generated class files (see class description)
cacheDirectory
)

See CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).

public
CachingJavaSourceClassLoader(ClassLoader
Attempt to load classes through this one before looking for source files
parentClassLoader
,
ResourceFinder
Finds Java source for class pkg.Cls in resource pkg/Cls.java
sourceFinder
,
String
Encoding of Java source or null for platform default encoding
characterEncoding
,
ResourceFinder
Finds precompiled class pkg.Cls in resource pkg/Cls.class (see class description)
classFileCacheResourceFinder
,
ResourceCreator
Stores compiled class pkg.Cls in resource pkg/Cls.class (see class description)
classFileCacheResourceCreator
)

Notice that this class is thread-safe if and only if the classFileCacheResourceCreator stores its data atomically, i.e. the classFileCacheResourceFinder sees the resource written by the classFileCacheResourceCreator only after the OutputStream is closed.

Method Summary

Modifier and TypeMethod and Description
protected Map<String, byte[]>

Returns:

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

Overrides org.codehaus.janino.JavaSourceClassLoader.generateBytecodes.

Overrides JavaSourceClassLoader#generateBytecodes(String) to implement class file caching.
private static byte[]
readResource(Resource r)

Reads all bytes from the given resource.

private static void
writeResource(ResourceCreator resourceCreator, String resourceName, byte[] data)

Creates a resource with the given name and store the data in it.

Inherited from org.codehaus.janino.JavaSourceClassLoader:
findClasssetCompileErrorHandlersetDebuggingInfosetSourceCharsetsetSourceFindersetSourcePathsetTargetVersionsetWarningHandler

Field Detail

classFileCacheResourceCreatorback to summary
private final ResourceCreator classFileCacheResourceCreator
classFileCacheResourceFinderback to summary
private final ResourceFinder classFileCacheResourceFinder
sourceFinderback to summary
private final ResourceFinder sourceFinder

Constructor Detail

CachingJavaSourceClassLoaderback to summary
public CachingJavaSourceClassLoader(ClassLoader parentClassLoader, File[] sourcePath, String characterEncoding, File cacheDirectory)

See CachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).

Parameters
sourcePath:File[]

Directories to scan for source files

cacheDirectory:File

Directory to use for caching generated class files (see class description)

CachingJavaSourceClassLoaderback to summary
public CachingJavaSourceClassLoader(ClassLoader parentClassLoader, ResourceFinder sourceFinder, String characterEncoding, ResourceFinder classFileCacheResourceFinder, ResourceCreator classFileCacheResourceCreator)

Notice that this class is thread-safe if and only if the classFileCacheResourceCreator stores its data atomically, i.e. the classFileCacheResourceFinder sees the resource written by the classFileCacheResourceCreator only after the OutputStream is closed.

In order to make the caching scheme work, both the classFileCacheResourceFinder and the sourceFinder must support the org.codehaus.commons.compiler.util.resource.Resource#lastModified() method, so that the modification time of the source and the class files can be compared.

Parameters
parentClassLoader:ClassLoader

Attempt to load classes through this one before looking for source files

sourceFinder:ResourceFinder

Finds Java source for class pkg.Cls in resource pkg/Cls.java

characterEncoding:String

Encoding of Java source or null for platform default encoding

classFileCacheResourceFinder:ResourceFinder

Finds precompiled class pkg.Cls in resource pkg/Cls.class (see class description)

classFileCacheResourceCreator:ResourceCreator

Stores compiled class pkg.Cls in resource pkg/Cls.class (see class description)

Method Detail

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

Overrides org.codehaus.janino.JavaSourceClassLoader.generateBytecodes.

Overrides JavaSourceClassLoader#generateBytecodes(String) to implement class file caching.

Returns:Map<String, byte[]>

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

Annotations
@Override
@Nullable
Exceptions
ClassNotFoundException:
Compilation problems or class file cache I/O problems
readResourceback to summary
private static byte[] readResource(Resource r) throws IOException

Reads all bytes from the given resource.

writeResourceback to summary
private static void writeResource(ResourceCreator resourceCreator, String resourceName, byte[] data) throws IOException

Creates a resource with the given name and store the data in it.