Top Description Fields Methods
org.codehaus.commons.compiler

public Interface ICompiler

Known Direct Implementers
org.codehaus.commons.compiler.AbstractCompiler
Imports
java.io.File, .IOException, .OutputStream, java.nio.charset.Charset, java.util.Collections, org.codehaus.commons.compiler.util.resource.ListableResourceFinder, .Resource, .ResourceCreator, .ResourceFinder, org.codehaus.commons.nullanalysis.Nullable

A simplified substitute for the javac tool.

Field Summary

Modifier and TypeField and Description
public static final ResourceCreator
CREATE_NEXT_TO_SOURCE_FILE

Special value for setClassFileCreator(ResourceCreator): Indicates that .class resources are to be created in the directory of the .java resource from which they are generated.

public static final ResourceFinder
public static final File
NO_DESTINATION_DIRECTORY

Special value for setDestinationDirectory(File, boolean)'s first parameter: Indicates that .class files are to be created in the directory of the .java file from which they are generated.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true for backwards compatibility (return value can safely be ignored)
compile
(File[]
Contain the compilation units to compile
sourceFiles
)

Reads 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.

public void
compile(Resource[]
Contain the compilation units to compile
sourceResources
)

See compile(File[]).

public void
setBootClassPath(File[] directoriesAndArchives)

Equivalent of --boot-class-path.

public void
setCharacterEncoding(String characterEncoding)

Deprecated Use setSourceCharset(Charset) instead
public void
setClassFileCreator(ResourceCreator
Special value CREATE_NEXT_TO_SOURCE_FILE means "create .class file next to its source file, not in the destination directory"
classFileCreator
)

This ResourceCreator is used to store generated .class files.

public void
setClassFileFinder(ResourceFinder classFileFinder, boolean rebuild)

Equivalent with setClassFileFinder(rebuild ?

public void
setClassFileFinder(ResourceFinder
Special value FIND_NEXT_TO_SOURCE_FILE means ".class file is next to its source file, not in the destination directory"
classFileFinder
)

This ResourceFinder is used to check whether a .class resource already exists and is younger than the .java resource from which it was generated.

public void
setClassPath(File[] directoriesAndArchives)

Equivalent of --class-path.

public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
errorHandler
)

Installs an ErrorHandler which is invoked during compilation on each error.

public void
setDebugLines(boolean value)

Equivalent of -g:lines.

public void
setDebugSource(boolean value)

Equivalent of -g:source.

public void
setDebugVars(boolean value)

Equivalent of -g:vars.

public void
setDestinationDirectory(File
NO_DESTINATION_DIRECTORY means "create .class resources in the directory of the .java resource from which they are generated"
destinationDirectory
,
boolean rebuild)

Equivalent of -d and -rebuild.

public void
setEncoding(Charset encoding)

The equivalent of JAVA's "-encoding" command line option.

public void
setExtensionDirectories(File[] directories)

Equivalent of -extdirs.

public void
setSourceCharset(Charset charset)

Same as setEncoding(Charset), but with a more precise name.

public void
setSourceFinder(ResourceFinder sourceFinder)

Finds more .java resources that need to be compiled, i.e. implements JAVAC's -sourcepath option.

public void
setSourcePath(File[] directoriesAndArchives)

Equivalent of --source-path.

public void
setSourceVersion(int version)

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option.

public void
setTargetVersion(int version)

Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option.

public void
setVerbose(boolean verbose)

Equivalent of -verbose.

public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

By default, warnings are discarded, but an application my install a custom WarningHandler.

Field Detail

CREATE_NEXT_TO_SOURCE_FILEback to summary
public static final ResourceCreator CREATE_NEXT_TO_SOURCE_FILE

Special value for setClassFileCreator(ResourceCreator): Indicates that .class resources are to be created in the directory of the .java resource from which they are generated.

FIND_NEXT_TO_SOURCE_FILEback to summary
public static final ResourceFinder FIND_NEXT_TO_SOURCE_FILE

Special value for setClassFileFinder(ResourceFinder).

See Also
setClassFileFinder(ResourceFinder)
NO_DESTINATION_DIRECTORYback to summary
public static final File NO_DESTINATION_DIRECTORY

Special value for setDestinationDirectory(File, boolean)'s first parameter: Indicates that .class files are to be created in the directory of the .java file from which they are generated.

Annotations
@Nullable

Method Detail

compileback to summary
public boolean compile(File[] sourceFiles) throws CompileException, IOException

Reads 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

Notice that it does make a difference whether you pass multiple source files to compile(File[]) or if you invoke compile(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(ErrorHandler).

Parameters
sourceFiles:File[]

Contain the compilation units to compile

Returns:boolean

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

Exceptions
CompileException:
Fatal compilation error, or the CompileException thrown be the installed compile error handler
IOException:
Occurred when reading from the sourceFiles
compileback to summary
public void compile(Resource[] sourceResources) throws CompileException, IOException

See compile(File[]).

Parameters
sourceResources:Resource[]

Contain the compilation units to compile

setBootClassPathback to summary
public void setBootClassPath(File[] directoriesAndArchives)

Equivalent of --boot-class-path.

setCharacterEncodingback to summary
public void setCharacterEncoding(String characterEncoding)

Deprecated

Use setSourceCharset(Charset) instead

Annotations
@Deprecated
setClassFileCreatorback to summary
public void setClassFileCreator(ResourceCreator classFileCreator)

This ResourceCreator is used to store generated .class files.

The default is, as for JAVAC, CREATE_NEXT_TO_SOURCE_FILE.

Parameters
classFileCreator:ResourceCreator

Special value CREATE_NEXT_TO_SOURCE_FILE means "create .class file next to its source file, not in the destination directory"

See Also
CREATE_NEXT_TO_SOURCE_FILE
setClassFileFinderback to summary
public void setClassFileFinder(ResourceFinder classFileFinder, boolean rebuild)

Equivalent with setClassFileFinder(rebuild ? ResourceFinder.EMPTY_RESOURCE_FINDER : classFileFinder).

setClassFileFinderback to summary
public void setClassFileFinder(ResourceFinder classFileFinder)

This ResourceFinder is used to check whether a .class resource already exists and is younger than the .java resource from which it was generated.

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

The default is, as for JAVAC, FIND_NEXT_TO_SOURCE_FILE.

Parameters
classFileFinder:ResourceFinder

Special value FIND_NEXT_TO_SOURCE_FILE means ".class file is next to its source file, not in the destination directory"

See Also
FIND_NEXT_TO_SOURCE_FILE
setClassPathback to summary
public void setClassPath(File[] directoriesAndArchives)

Equivalent of --class-path.

setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler errorHandler)

Installs an ErrorHandler which is invoked during compilation on each error. (By default, the compilation throws a CompileException on the first error and terminates.)

If the given ErrorHandler throws a CompileException, then the compilation terminates and the exception is propagated.

If the given ErrorHandler does not throw a CompileException but completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation of a compilation unit completes normally but errors were reported, then it will throw a CompileException indicating the number of errors.

In other words: The ErrorHandler may throw a CompileException or not, but the compilation will definitely throw a CompileException if one or more compile errors have occurred.

Parameters
errorHandler:ErrorHandler

null to restore the default behavior (throwing a CompileException)

setDebugLinesback to summary
public void setDebugLines(boolean value)

Equivalent of -g:lines.

setDebugSourceback to summary
public void setDebugSource(boolean value)

Equivalent of -g:source.

setDebugVarsback to summary
public void setDebugVars(boolean value)

Equivalent of -g:vars.

setDestinationDirectoryback to summary
public void setDestinationDirectory(File destinationDirectory, boolean rebuild)

Equivalent of -d and -rebuild.

Overrides any previously configured class file finder and class file creator.

Parameters
destinationDirectory:File

NO_DESTINATION_DIRECTORY means "create .class resources in the directory of the .java resource from which they are generated"

See Also
NO_DESTINATION_DIRECTORY, setClassFileFinder(ResourceFinder), setClassFileCreator(ResourceCreator)
setEncodingback to summary
public void setEncoding(Charset encoding)

The equivalent of JAVA's "-encoding" command line option.

See Also
setSourceCharset(Charset)
setExtensionDirectoriesback to summary
public void setExtensionDirectories(File[] directories)

Equivalent of -extdirs.

setSourceCharsetback to summary
public void setSourceCharset(Charset charset)

Same as setEncoding(Charset), but with a more precise name.

setSourceFinderback to summary
public void setSourceFinder(ResourceFinder sourceFinder)

Finds more .java resources that need to be compiled, i.e. implements JAVAC's -sourcepath option.

setSourcePathback to summary
public void setSourcePath(File[] directoriesAndArchives)

Equivalent of --source-path.

Equivalent with setSourceFinder(new PathResourceFinder(directoriesAndArchives)).

setSourceVersionback to summary
public void setSourceVersion(int version)

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option. May be ignored by an implementation (e.g. the janino implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation. -1 means to use a default version.

setTargetVersionback to summary
public void setTargetVersion(int version)

Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM. -1 means to use a default version.

setVerboseback to summary
public void setVerbose(boolean verbose)

Equivalent of -verbose.

setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)

By default, warnings are discarded, but an application my install a custom WarningHandler.

Parameters
warningHandler:WarningHandler

null to indicate that no warnings be issued