Class CachingJavaSourceClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
-
- org.codehaus.janino.JavaSourceClassLoader
-
- org.codehaus.janino.CachingJavaSourceClassLoader
-
public class CachingJavaSourceClassLoader extends JavaSourceClassLoader
AJavaSourceClassLoader
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
ResourceFinder
and aResourceCreator
(seeCachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator)
.See
AbstractJavaSourceClassLoader.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 theCachingJavaSourceClassLoader
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 butCompiler
instead!
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
-
Field Summary
Fields Modifier and Type Field Description private ResourceCreator
classFileCacheResourceCreator
private ResourceFinder
classFileCacheResourceFinder
private ResourceFinder
sourceFinder
-
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
protectionDomainFactory
-
-
Constructor Summary
Constructors Constructor Description CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, java.io.File[] sourcePath, java.lang.String characterEncoding, java.io.File cacheDirectory)
CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, ResourceFinder sourceFinder, java.lang.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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Map<java.lang.String,byte[]>
generateBytecodes(java.lang.String className)
OverridesJavaSourceClassLoader.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, java.lang.String resourceName, byte[] data)
Creates a resource with the given name and store the data in it.-
Methods inherited from class org.codehaus.janino.JavaSourceClassLoader
findClass, setCompileErrorHandler, setDebuggingInfo, setSourceCharset, setSourceFinder, setSourcePath, setWarningHandler
-
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 Detail
-
classFileCacheResourceFinder
private final ResourceFinder classFileCacheResourceFinder
-
classFileCacheResourceCreator
private final ResourceCreator classFileCacheResourceCreator
-
sourceFinder
private final ResourceFinder sourceFinder
-
-
Constructor Detail
-
CachingJavaSourceClassLoader
public CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, @Nullable java.io.File[] sourcePath, @Nullable java.lang.String characterEncoding, java.io.File cacheDirectory)
SeeCachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator)
.- Parameters:
sourcePath
- Directories to scan for source filescacheDirectory
- Directory to use for caching generated class files (see class description)
-
CachingJavaSourceClassLoader
public CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, ResourceFinder sourceFinder, @Nullable java.lang.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 theclassFileCacheResourceCreator
only after theOutputStream
is closed.In order to make the caching scheme work, both the classFileCacheResourceFinder and the
sourceFinder
must support theResource.lastModified()
method, so that the modification time of the source and the class files can be compared.- Parameters:
parentClassLoader
- Attempt to load classes through this one before looking for source filessourceFinder
- Finds Java source for classpkg.Cls
in resourcepkg/Cls.java
characterEncoding
- Encoding of Java source ornull
for platform default encodingclassFileCacheResourceFinder
- Finds precompiled classpkg.Cls
in resourcepkg/Cls.class
(see class description)classFileCacheResourceCreator
- Stores compiled classpkg.Cls
in resourcepkg/Cls.class
(see class description)
-
-
Method Detail
-
generateBytecodes
@Nullable protected java.util.Map<java.lang.String,byte[]> generateBytecodes(java.lang.String className) throws java.lang.ClassNotFoundException
OverridesJavaSourceClassLoader.generateBytecodes(String)
to implement class file caching.- Overrides:
generateBytecodes
in classJavaSourceClassLoader
- Returns:
- String name => byte[] bytecode, or
null
if no source code could be found - Throws:
java.lang.ClassNotFoundException
- Compilation problems or class file cache I/O problems
-
readResource
private static byte[] readResource(Resource r) throws java.io.IOException
Reads all bytes from the given resource.- Throws:
java.io.IOException
-
writeResource
private static void writeResource(ResourceCreator resourceCreator, java.lang.String resourceName, byte[] data) throws java.io.IOException
Creates a resource with the given name and store the data in it.- Throws:
java.io.IOException
-
-