org.codehaus.janino

Class Compiler

public class Compiler extends Object

A 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:{lines,vars,source} ] \
           [ -warn:pattern-list ] \
           source-file ...
 java org.codehaus.janino.Compiler -help
 
Nested Class Summary
static classCompiler.SimpleWarningHandler
Field Summary
static ResourceCreatorCREATE_NEXT_TO_SOURCE_FILE
static StringPattern[]DEFAULT_WARNING_HANDLE_PATTERNS
static ResourceFinderFIND_NEXT_TO_SOURCE_FILE
static FileNO_DESTINATION_DIRECTORY
Constructor Summary
Compiler(File[] optionalSourcePath, File[] classPath, File[] optionalExtDirs, File[] optionalBootClassPath, File destinationDirectory, String optionalCharacterEncoding, boolean verbose, EnumeratorSet debuggingInformation, StringPattern[] warningHandlePatterns, boolean rebuild)
Initialize a JavaTM compiler with the given parameters.
Compiler(ResourceFinder sourceFinder, IClassLoader iClassLoader, ResourceFinder classFileFinder, ResourceCreator classFileCreator, String optionalCharacterEncoding, boolean verbose, EnumeratorSet debuggingInformation, WarningHandler optionalWarningHandler)
To mimic the behavior of JAVAC with a missing "-d" command line option, pass FIND_NEXT_TO_SOURCE_FILE as the classFileResourceFinder and CREATE_NEXT_TO_SOURCE_FILE as the classFileResourceCreator.
Method Summary
booleancompile(File[] sourceFiles)
Reads a set of JavaTM compilation units (a.k.a.
booleancompile(Resource[] sourceResources)
See (File[]).
static FilegetClassFile(String className, File sourceFile, File optionalDestinationDirectory)
Construct the name of a file that could store the byte code of the class with the given name.
static voidmain(String[] args)
Command line interface.
voidsetCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
Install a custom ErrorHandler.
voidstoreClassFile(ClassFile classFile, File sourceFile)
Store the byte code of this ClassFile in the file system.

Field Detail

CREATE_NEXT_TO_SOURCE_FILE

public static final ResourceCreator CREATE_NEXT_TO_SOURCE_FILE

DEFAULT_WARNING_HANDLE_PATTERNS

public static final StringPattern[] DEFAULT_WARNING_HANDLE_PATTERNS

FIND_NEXT_TO_SOURCE_FILE

public static final ResourceFinder FIND_NEXT_TO_SOURCE_FILE

NO_DESTINATION_DIRECTORY

public static final File NO_DESTINATION_DIRECTORY

Constructor Detail

Compiler

public Compiler(File[] optionalSourcePath, File[] classPath, File[] optionalExtDirs, File[] optionalBootClassPath, File destinationDirectory, String optionalCharacterEncoding, boolean verbose, EnumeratorSet debuggingInformation, StringPattern[] warningHandlePatterns, boolean rebuild)
Initialize a JavaTM compiler with the given parameters.

Classes are searched in the following order:

The file name of a class file that represents class "pkg.Example" is determined as follows:

See Also: DEFAULT_WARNING_HANDLE_PATTERNS

Compiler

public Compiler(ResourceFinder sourceFinder, IClassLoader iClassLoader, ResourceFinder classFileFinder, ResourceCreator classFileCreator, String optionalCharacterEncoding, boolean verbose, EnumeratorSet debuggingInformation, WarningHandler optionalWarningHandler)
To mimic the behavior of JAVAC with a missing "-d" command line option, pass FIND_NEXT_TO_SOURCE_FILE as the classFileResourceFinder and CREATE_NEXT_TO_SOURCE_FILE as the classFileResourceCreator.

If it is impossible to check whether an already-compiled class file exists, or if you want to enforce recompilation, pass EMPTY_RESOURCE_FINDER as the classFileResourceFinder.

Parameters: sourceFinder Finds extra Java compilation units that need to be compiled (a.k.a. "sourcepath") iClassLoader loads auxiliary IClasses; e.g. new ClassLoaderIClassLoader(ClassLoader) classFileFinder Where to look for up-to-date class files that need not be compiled classFileCreator Used to store generated class files optionalCharacterEncoding verbose debuggingInformation a combination of Java.DEBUGGING_... optionalWarningHandler used to issue warnings

Method Detail

compile

public boolean compile(File[] sourceFiles)
Reads a set of JavaTM 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
Notice that it does make a difference whether you pass multiple source files to (File[]) or if you invoke (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 the sourceFiles.

This method must be called exactly once after object construction.

Compile errors are reported as described at setCompileErrorHandler.

Parameters: sourceFiles Contain the compilation units to compile

Returns: true for backwards compatibility (return value can safely be ignored)

Throws: CompileException Fatal compilation error, or the CompileException thrown be the installed compile error handler ParseException Parse error ScanException Scan error IOException Occurred when reading from the sourceFiles

compile

public boolean compile(Resource[] sourceResources)
See (File[]).

Parameters: sourceResources Contain the compilation units to compile

Returns: true for backwards compatibility (return value can safely be ignored)

getClassFile

public static File getClassFile(String className, File sourceFile, File optionalDestinationDirectory)
Construct the name of a file that could store the byte code of the class with the given name.

If optionalDestinationDirectory is non-null, the returned path is the optionalDestinationDirectory plus 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 optionalDestinationDirectory is null, the returned path is the directory of the sourceFile plus 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"

main

public static void main(String[] args)
Command line interface.

setCompileErrorHandler

public void setCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
Install a custom ErrorHandler. The default ErrorHandler prints the first 20 compile errors to System#err and then throws a CompileException.

Passing null restores the default ErrorHandler.

Notice that scan and parse errors are not redirected to this ErrorHandler, instead, they cause a ScanException or a ParseException to be thrown. Also, the Compiler may choose to throw CompileExceptions in certain, fatal compile error situations, even if an ErrorHandler is installed.

In other words: In situations where compilation can reasonably continue after a compile error, the ErrorHandler is called; all other error conditions cause a CompileException, ParseException or ScanException to be thrown.

storeClassFile

public void storeClassFile(ClassFile classFile, File sourceFile)
Store the byte code of this ClassFile in the file system. Directories are created as necessary.

Parameters: classFile sourceFile Required to compute class file path if no destination directory given