Class Compiler
- java.lang.Object
-
- org.codehaus.janino.Compiler
-
public class Compiler extends java.lang.ObjectA simplified substitute for the javac tool. Usage:java org.codehaus.janino.Compiler \ [ -d destination-dir ] \ [ -sourcepath dirlist ] \ [ -classpath dirlist ] \ [ -extdirs dirlist ] \ [ -bootclasspath dirlist ] \ [ -encoding encoding ] \ [ -verbose ] \ [ -g:none ] \ [ -g:{source,lines,vars} ] \ [ -warn:pattern-list ] \ source-file ... java org.codehaus.janino.Compiler -help
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCompiler.SimpleWarningHandler
-
Field Summary
Fields Modifier and Type Field Description static ResourceCreatorCREATE_NEXT_TO_SOURCE_FILESpecial value for "classFileResourceCreator".static StringPattern[]DEFAULT_WARNING_HANDLE_PATTERNSstatic ResourceFinderFIND_NEXT_TO_SOURCE_FILESpecial value for "classFileResourceFinder".static java.io.FileNO_DESTINATION_DIRECTORYBackwards compatibility -- previously, "null" was officially documented.
-
Constructor Summary
Constructors Constructor Description Compiler(java.io.File[] optionalSourcePath, java.io.File[] classPath, java.io.File[] optionalExtDirs, java.io.File[] optionalBootClassPath, java.io.File destinationDirectory, java.lang.String optionalCharacterEncoding, boolean verbose, boolean debugSource, boolean debugLines, boolean debugVars, StringPattern[] warningHandlePatterns, boolean rebuild)Initialize a Java™ compiler with the given parameters.Compiler(ResourceFinder sourceFinder, IClassLoader iClassLoader, ResourceFinder classFileFinder, ResourceCreator classFileCreator, java.lang.String optionalCharacterEncoding, boolean verbose, boolean debugSource, boolean debugLines, boolean debugVars, WarningHandler optionalWarningHandler)To mimic the behavior of JAVAC with a missing "-d" command line option, passFIND_NEXT_TO_SOURCE_FILEas theclassFileResourceFinderandCREATE_NEXT_TO_SOURCE_FILEas theclassFileResourceCreator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancompile(java.io.File[] sourceFiles)Reads a set of Java™ compilation units (a.k.a.booleancompile(Resource[] sourceResources)Seecompile(File[]).static java.io.FilegetClassFile(java.lang.String className, java.io.File sourceFile, java.io.File optionalDestinationDirectory)Construct the name of a file that could store the byte code of the class with the given name.static voidmain(java.lang.String[] args)Command line interface.voidsetCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)Install a customUnitCompiler.ErrorHandler.voidstoreClassFile(ClassFile classFile, java.io.File sourceFile)Store the byte code of thisClassFilein the file system.
-
-
-
Field Detail
-
FIND_NEXT_TO_SOURCE_FILE
public static final ResourceFinder FIND_NEXT_TO_SOURCE_FILE
Special value for "classFileResourceFinder".
-
CREATE_NEXT_TO_SOURCE_FILE
public static final ResourceCreator CREATE_NEXT_TO_SOURCE_FILE
Special value for "classFileResourceCreator".
-
NO_DESTINATION_DIRECTORY
public static final java.io.File NO_DESTINATION_DIRECTORY
Backwards compatibility -- previously, "null" was officially documented.
-
DEFAULT_WARNING_HANDLE_PATTERNS
public static final StringPattern[] DEFAULT_WARNING_HANDLE_PATTERNS
-
-
Constructor Detail
-
Compiler
public Compiler(java.io.File[] optionalSourcePath, java.io.File[] classPath, java.io.File[] optionalExtDirs, java.io.File[] optionalBootClassPath, java.io.File destinationDirectory, java.lang.String optionalCharacterEncoding, boolean verbose, boolean debugSource, boolean debugLines, boolean debugVars, StringPattern[] warningHandlePatterns, boolean rebuild)Initialize a Java™ compiler with the given parameters.Classes are searched in the following order:
- If
optionalBootClassPathisnull:- Through the system class loader of the JVM that runs JANINO
- If
optionalBootClassPathis notnull:- Through the
optionalBootClassPath
- Through the
- If
optionalExtDirsis notnull:- Through the
optionalExtDirs
- Through the
- Through the
classPath - If
optionalSourcePathisnull:- Through source files found on the
classPath
- Through source files found on the
- If
optionalSourcePathis notnull:- Through source files found on the
sourcePath
- Through source files found on the
The file name of a class file that represents class "pkg.Example" is determined as follows:
-
If
optionalDestinationDirectoryis notNO_DESTINATION_DIRECTORY:<i>optionalDestinationDirectory</i>/pkg/Example.class -
If
optionalDestinationDirectoryisNO_DESTINATION_DIRECTORY:dir1/dir2/Example.class(Assuming that the file name of the source file that declares the class wasdir1/dir2/Any.java.)
- See Also:
DEFAULT_WARNING_HANDLE_PATTERNS
- If
-
Compiler
public Compiler(ResourceFinder sourceFinder, IClassLoader iClassLoader, ResourceFinder classFileFinder, ResourceCreator classFileCreator, java.lang.String optionalCharacterEncoding, boolean verbose, boolean debugSource, boolean debugLines, boolean debugVars, WarningHandler optionalWarningHandler)
To mimic the behavior of JAVAC with a missing "-d" command line option, passFIND_NEXT_TO_SOURCE_FILEas theclassFileResourceFinderandCREATE_NEXT_TO_SOURCE_FILEas theclassFileResourceCreator.If it is impossible to check whether an already-compiled class file exists, or if you want to enforce recompilation, pass
ResourceFinder.EMPTY_RESOURCE_FINDERas theclassFileResourceFinder.- Parameters:
sourceFinder- Finds extra Java compilation units that need to be compiled (a.k.a. "-sourcepath")iClassLoader- Loads auxiliaryIClasses (a.k.a. "-classpath"), e.g.new ClassLoaderIClassLoader(ClassLoader)classFileFinder- Where to look for up-to-date class files that need not be compiled (a.k.a. "-d")classFileCreator- Used to store generated class files (a.k.a. "-d")optionalWarningHandler- Used to issue warnings
-
-
Method Detail
-
main
public static void main(java.lang.String[] args)
Command line interface.
-
setCompileErrorHandler
public void setCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
Install a customUnitCompiler.ErrorHandler. The defaultUnitCompiler.ErrorHandlerprints the first 20 compile errors toSystem.errand then throws aCompileException.Passing
nullrestores the defaultUnitCompiler.ErrorHandler.Notice that scan and parse errors are not redirected to this
UnitCompiler.ErrorHandler, instead, they cause aCompileExceptionto be thrown. Also, theCompilermay choose to throwCompileExceptions in certain, fatal compile error situations, even if anUnitCompiler.ErrorHandleris installed.In other words: In situations where compilation can reasonably continue after a compile error, the
UnitCompiler.ErrorHandleris called; all other error conditions cause aCompileExceptionto be thrown.
-
compile
public boolean compile(java.io.File[] sourceFiles) throws CompileException, java.io.IOExceptionReads a set of Java™ compilation units (a.k.a. "source files") from the file system, compiles them into a set of "class files" and stores these in the file system. Additional source files are parsed and compiled on demand through the "source path" set of directories.For example, if the source path comprises the directories "A/B" and "../C", then the source file for class "com.acme.Main" is searched in
- A/B/com/acme/Main.java
- ../C/com/acme/Main.java
compile(File[])or if you invokecompile(File[])multiply: In the former case, the source files may contain arbitrary references among each other (even circular ones). In the latter case, only the source files on the source path may contain circular references, not thesourceFiles.This method must be called exactly once after object construction.
Compile errors are reported as described at
setCompileErrorHandler(UnitCompiler.ErrorHandler).- Parameters:
sourceFiles- Contain the compilation units to compile- Returns:
truefor backwards compatibility (return value can safely be ignored)- Throws:
CompileException- Fatal compilation error, or theCompileExceptionthrown be the installed compile error handlerjava.io.IOException- Occurred when reading from thesourceFiles
- A/B/com/acme/Main.java
-
compile
public boolean compile(Resource[] sourceResources) throws CompileException, java.io.IOException
Seecompile(File[]).- Parameters:
sourceResources- Contain the compilation units to compile- Returns:
truefor backwards compatibility (return value can safely be ignored)- Throws:
CompileExceptionjava.io.IOException
-
getClassFile
public static java.io.File getClassFile(java.lang.String className, java.io.File sourceFile, java.io.File optionalDestinationDirectory)Construct the name of a file that could store the byte code of the class with the given name.If
optionalDestinationDirectoryis non-null, the returned path is theoptionalDestinationDirectoryplus the package of the class (with dots replaced with file separators) plus the class name plus ".class". Example: "destdir/pkg1/pkg2/Outer$Inner.class"If
optionalDestinationDirectoryis null, the returned path is the directory of thesourceFileplus the class name plus ".class". Example: "srcdir/Outer$Inner.class"- Parameters:
className- E.g. "pkg1.pkg2.Outer$Inner"sourceFile- E.g. "srcdir/Outer.java"optionalDestinationDirectory- E.g. "destdir"
-
storeClassFile
public void storeClassFile(ClassFile classFile, java.io.File sourceFile) throws java.io.IOException
Store the byte code of thisClassFilein the file system. Directories are created as necessary.- Parameters:
classFile-sourceFile- Required to compute class file path if no destination directory given- Throws:
java.io.IOException
-
-