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
AJavaSourceClassLoaderthat uses a resource storage provided by the application to cache compiled classes and thus saving unnecessary recompilations.The application provides access to the resource storeage through a pair of a
ResourceFinderand 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 theCachingJavaSourceClassLoaderto persistently cache some data between invocations. In other words: If you want to compile.javafiles into.classfiles, then don't use this class butCompilerinstead!
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
AbstractJavaSourceClassLoader.ProtectionDomainFactory
-
-
Field Summary
-
Fields inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
optionalProtectionDomainFactory
-
-
Constructor Summary
Constructors Constructor Description CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, java.io.File[] optionalSourcePath, java.lang.String optionalCharacterEncoding, java.io.File cacheDirectory)CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, ResourceFinder sourceFinder, java.lang.String optionalCharacterEncoding, ResourceFinder classFileCacheResourceFinder, ResourceCreator classFileCacheResourceCreator)Notice that this class is thread-safe if and only if theclassFileCacheResourceCreatorstores its data atomically, i.e.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.MapgenerateBytecodes(java.lang.String className)OverrideJavaSourceClassLoader.generateBytecodes(String)to implement class file caching.-
Methods inherited from class org.codehaus.janino.JavaSourceClassLoader
findClass, setCompileErrorHandler, setDebuggingInfo, setSourceFileCharacterEncoding, setSourcePath, setWarningHandler
-
Methods inherited from class org.codehaus.commons.compiler.AbstractJavaSourceClassLoader
main, setProtectionDomainFactory
-
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, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Constructor Detail
-
CachingJavaSourceClassLoader
public CachingJavaSourceClassLoader(java.lang.ClassLoader parentClassLoader, java.io.File[] optionalSourcePath, java.lang.String optionalCharacterEncoding, java.io.File cacheDirectory)SeeCachingJavaSourceClassLoader(ClassLoader, ResourceFinder, String, ResourceFinder, ResourceCreator).- Parameters:
optionalSourcePath- 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, java.lang.String optionalCharacterEncoding, ResourceFinder classFileCacheResourceFinder, ResourceCreator classFileCacheResourceCreator)Notice that this class is thread-safe if and only if theclassFileCacheResourceCreatorstores its data atomically, i.e. theclassFileCacheResourceFindersees the resource written by theclassFileCacheResourceCreatoronly after theOutputStreamis closed.In order to make the caching scheme work, both the
classFileCacheResourceFinderand thesourceFindermust 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.Clsin resourcepkg/Cls.javaoptionalCharacterEncoding- Encoding of Java™ source ornullfor platform default encodingclassFileCacheResourceFinder- Finds precompiled classpkg.Clsin resourcepkg/Cls.class(see class description)classFileCacheResourceCreator- Stores compiled classpkg.Clsin resourcepkg/Cls.class(see class description)
-
-
Method Detail
-
generateBytecodes
protected java.util.Map generateBytecodes(java.lang.String className) throws java.lang.ClassNotFoundExceptionOverrideJavaSourceClassLoader.generateBytecodes(String)to implement class file caching.- Overrides:
generateBytecodesin classJavaSourceClassLoader- Returns:
- String name => byte[] bytecode, or
nullif no source code could be found - Throws:
java.lang.ClassNotFoundException- Compilation problems or class file cache I/O problems
-
-