Class CachingJavaSourceClassLoader
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 ResourceFinder
and a ResourceCreator
(see CachingJavaSourceClassLoader(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 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!
-
Nested Class Summary
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ResourceCreator
private final ResourceFinder
private final ResourceFinder
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
protectionDomainFactory
-
Constructor Summary
ConstructorsConstructorDescriptionCachingJavaSourceClassLoader
(ClassLoader parentClassLoader, File[] sourcePath, String characterEncoding, File cacheDirectory) 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. -
Method Summary
Modifier and TypeMethodDescriptiongenerateBytecodes
(String className) OverridesJavaSourceClassLoader.generateBytecodes(String)
to implement class file caching.private static byte[]
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.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 Details
-
classFileCacheResourceFinder
-
classFileCacheResourceCreator
-
sourceFinder
-
-
Constructor Details
-
CachingJavaSourceClassLoader
public CachingJavaSourceClassLoader(ClassLoader parentClassLoader, @Nullable File[] sourcePath, @Nullable String characterEncoding, 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(ClassLoader parentClassLoader, ResourceFinder sourceFinder, @Nullable 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 Details
-
generateBytecodes
@Nullable protected Map<String,byte[]> generateBytecodes(String className) throws 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:
ClassNotFoundException
- Compilation problems or class file cache I/O problems
-
readResource
Reads all bytes from the given resource.- Throws:
IOException
-
writeResource
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.- Throws:
IOException
-