Top Description Fields Constructors Methods
org.codehaus.commons.compiler

public abstract Class AbstractCompiler

extends Object
implements ICompiler
Class Inheritance
All Implemented Interfaces
org.codehaus.commons.compiler.ICompiler
Imports
java.io.ByteArrayOutputStream, .File, .IOException, .OutputStream, java.nio.charset.Charset, java.util.regex.Pattern, org.codehaus.commons.compiler.util.Disassembler, .StringUtil, .SystemProperties, org.codehaus.commons.compiler.util.resource.DirectoryResourceCreator, .DirectoryResourceFinder, .FileResource, .PathResourceFinder, .Resource, .ResourceCreator, .ResourceFinder, org.codehaus.commons.nullanalysis.Nullable

A base class and wrapper for Compiler that implements all redundant API methods.

Field Summary

Modifier and TypeField and Description
protected File[]
bootClassPath

This is always non-null for JVMs that support BOOTCLASSPATH (1.0-1.8), and this is always null for JVMs that don't (9+).

protected ResourceCreator
classFileCreator

Implements the "write side" of the JAVAC -d option.

protected ResourceFinder
classFileFinder

Implements the "read side" of the JAVAC -d option.

protected File[]
classPath

The classpath of the currently executing JRE.

protected ErrorHandler
protected boolean
debugLines

Implements the JAVAC -g:lines option.

protected boolean
debugSource

Implements the JAVAC -g:source option.

protected boolean
debugVars

Implements the JAVAC -g:vars option.

private static final boolean
private static final Pattern
protected File[]
extensionDirectories

The list of extension directories of the currently executing JRE.

public Charset
sourceCharset

Implements the JAVAC -encoding option.

protected ResourceFinder
sourceFinder

Implements the JAVAC -sourcepath option.

protected int
protected int
protected WarningHandler
warningHandler

Stores the value configured with setWarningHandler(WarningHandler).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final boolean
compile(File[]
Contain the compilation units to compile
sourceFiles
)

Implements org.codehaus.commons.compiler.ICompiler.compile.

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
public final void
public final void
setClassFileCreator(ResourceCreator
Stores the generated class files (a.k.a. "-d"); special value CREATE_NEXT_TO_SOURCE_FILE means "create each .class file in the same directory as its source file"
classFileCreator
)

Implements org.codehaus.commons.compiler.ICompiler.setClassFileCreator.

This ResourceCreator is used to store generated .class files.
public final void
setClassFileFinder(ResourceFinder destination, boolean rebuild)

Implements org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

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
)

Implements org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

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)

Implements org.codehaus.commons.compiler.ICompiler.setClassPath.

Equivalent of --class-path.
public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
compileErrorHandler
)

Implements org.codehaus.commons.compiler.ICompiler.setCompileErrorHandler.

Installs an ErrorHandler which is invoked during compilation on each error.
public void
setDebugLines(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugLines.

Equivalent of -g:lines.
public void
setDebugSource(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugSource.

Equivalent of -g:source.
public void
setDebugVars(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugVars.

Equivalent of -g:vars.
public final 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)

Implements org.codehaus.commons.compiler.ICompiler.setDestinationDirectory.

Equivalent of -d and -rebuild.
public final void
setEncoding(Charset encoding)

Implements org.codehaus.commons.compiler.ICompiler.setEncoding.

The equivalent of JAVA's "-encoding" command line option.
public void
public void
public void
setSourceFinder(ResourceFinder sourceFinder)

Implements org.codehaus.commons.compiler.ICompiler.setSourceFinder.

Finds more .java resources that need to be compiled, i.e. implements JAVAC's -sourcepath option.
public void
setSourcePath(File[] directoriesAndArchives)

Implements org.codehaus.commons.compiler.ICompiler.setSourcePath.

Equivalent of --source-path.
public void
setSourceVersion(int version)

Implements org.codehaus.commons.compiler.ICompiler.setSourceVersion.

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option.
public void
setTargetVersion(int version)

Implements org.codehaus.commons.compiler.ICompiler.setTargetVersion.

Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option.
public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

Implements org.codehaus.commons.compiler.ICompiler.setWarningHandler.

By default, warnings are discarded, but an application my install a custom WarningHandler.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bootClassPathback to summary
protected File[] bootClassPath

This is always non-null for JVMs that support BOOTCLASSPATH (1.0-1.8), and this is always null for JVMs that don't (9+).

Annotations
@Nullable
classFileCreatorback to summary
protected ResourceCreator classFileCreator

Implements the "write side" of the JAVAC -d option.

classFileFinderback to summary
protected ResourceFinder classFileFinder

Implements the "read side" of the JAVAC -d option.

classPathback to summary
protected File[] classPath

The classpath of the currently executing JRE.

compileErrorHandlerback to summary
protected ErrorHandler compileErrorHandler

Stores the value configured with setCompileErrorHandler(ErrorHandler).

Annotations
@Nullable
debugLinesback to summary
protected boolean debugLines

Implements the JAVAC -g:lines option.

debugSourceback to summary
protected boolean debugSource

Implements the JAVAC -g:source option.

debugVarsback to summary
protected boolean debugVars

Implements the JAVAC -g:vars option.

disassembleClassFilesToStdoutback to summary
private static final boolean disassembleClassFilesToStdout
disassembleClassNamesback to summary
private static final Pattern disassembleClassNames
extensionDirectoriesback to summary
protected File[] extensionDirectories

The list of extension directories of the currently executing JRE. Empty for Java 9+, because the system property "java.ext.dirs" is not set in JRE 9+.

sourceCharsetback to summary
public Charset sourceCharset

Implements the JAVAC -encoding option.

sourceFinderback to summary
protected ResourceFinder sourceFinder

Implements the JAVAC -sourcepath option.

sourceVersionback to summary
protected int sourceVersion
targetVersionback to summary
protected int targetVersion
warningHandlerback to summary
protected WarningHandler warningHandler

Stores the value configured with setWarningHandler(WarningHandler).

Annotations
@Nullable

Constructor Detail

AbstractCompilerback to summary
public AbstractCompiler()

Method Detail

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

Implements org.codehaus.commons.compiler.ICompiler.compile.

Doc from org.codehaus.commons.compiler.ICompiler.compile.

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)

Annotations
@Override
Exceptions
CompileException:
Fatal compilation error, or the CompileException thrown be the installed compile error handler
IOException:
Occurred when reading from the sourceFiles
setBootClassPathback to summary
public void setBootClassPath(File[] directoriesAndArchives)

Implements org.codehaus.commons.compiler.ICompiler.setBootClassPath.

Doc from org.codehaus.commons.compiler.ICompiler.setBootClassPath.

Equivalent of --boot-class-path.

Annotations
@Override
setCharacterEncodingback to summary
public final void setCharacterEncoding(String characterEncoding)

Implements org.codehaus.commons.compiler.ICompiler.setCharacterEncoding.

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

Implements org.codehaus.commons.compiler.ICompiler.setClassFileCreator.

Doc from org.codehaus.commons.compiler.ICompiler.setClassFileCreator.

This ResourceCreator is used to store generated .class files.

The default is, as for JAVAC, CREATE_NEXT_TO_SOURCE_FILE.

Parameters
classFileCreator:ResourceCreator

Stores the generated class files (a.k.a. "-d"); special value CREATE_NEXT_TO_SOURCE_FILE means "create each .class file in the same directory as its source file"

Annotations
@Override
setClassFileFinderback to summary
public final void setClassFileFinder(ResourceFinder destination, boolean rebuild)

Implements org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

Doc from org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

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

Annotations
@Override
setClassFileFinderback to summary
public void setClassFileFinder(ResourceFinder classFileFinder)

Implements org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

Doc from org.codehaus.commons.compiler.ICompiler.setClassFileFinder.

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"

Annotations
@Override
setClassPathback to summary
public void setClassPath(File[] directoriesAndArchives)

Implements org.codehaus.commons.compiler.ICompiler.setClassPath.

Doc from org.codehaus.commons.compiler.ICompiler.setClassPath.

Equivalent of --class-path.

Annotations
@Override
setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler compileErrorHandler)

Implements org.codehaus.commons.compiler.ICompiler.setCompileErrorHandler.

Doc from org.codehaus.commons.compiler.ICompiler.setCompileErrorHandler.

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
compileErrorHandler:ErrorHandler

null to restore the default behavior (throwing a CompileException)

Annotations
@Override
setDebugLinesback to summary
public void setDebugLines(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugLines.

Doc from org.codehaus.commons.compiler.ICompiler.setDebugLines.

Equivalent of -g:lines.

Annotations
@Override
setDebugSourceback to summary
public void setDebugSource(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugSource.

Doc from org.codehaus.commons.compiler.ICompiler.setDebugSource.

Equivalent of -g:source.

Annotations
@Override
setDebugVarsback to summary
public void setDebugVars(boolean value)

Implements org.codehaus.commons.compiler.ICompiler.setDebugVars.

Doc from org.codehaus.commons.compiler.ICompiler.setDebugVars.

Equivalent of -g:vars.

Annotations
@Override
setDestinationDirectoryback to summary
public final void setDestinationDirectory(File destinationDirectory, boolean rebuild)

Implements org.codehaus.commons.compiler.ICompiler.setDestinationDirectory.

Doc from org.codehaus.commons.compiler.ICompiler.setDestinationDirectory.

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"

Annotations
@Override
setEncodingback to summary
public final void setEncoding(Charset encoding)

Implements org.codehaus.commons.compiler.ICompiler.setEncoding.

Doc from org.codehaus.commons.compiler.ICompiler.setEncoding.

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

Annotations
@Override
setExtensionDirectoriesback to summary
public void setExtensionDirectories(File[] directories)

Implements org.codehaus.commons.compiler.ICompiler.setExtensionDirectories.

Doc from org.codehaus.commons.compiler.ICompiler.setExtensionDirectories.

Equivalent of -extdirs.

Annotations
@Override
setSourceCharsetback to summary
public void setSourceCharset(Charset charset)

Implements org.codehaus.commons.compiler.ICompiler.setSourceCharset.

Doc from org.codehaus.commons.compiler.ICompiler.setSourceCharset.

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

Annotations
@Override
setSourceFinderback to summary
public void setSourceFinder(ResourceFinder sourceFinder)

Implements org.codehaus.commons.compiler.ICompiler.setSourceFinder.

Doc from org.codehaus.commons.compiler.ICompiler.setSourceFinder.

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

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

Implements org.codehaus.commons.compiler.ICompiler.setSourcePath.

Doc from org.codehaus.commons.compiler.ICompiler.setSourcePath.

Equivalent of --source-path.

Equivalent with setSourceFinder(new PathResourceFinder(directoriesAndArchives)).

Annotations
@Override
setSourceVersionback to summary
public void setSourceVersion(int version)

Implements org.codehaus.commons.compiler.ICompiler.setSourceVersion.

Doc from org.codehaus.commons.compiler.ICompiler.setSourceVersion.

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.

Annotations
@Override
setTargetVersionback to summary
public void setTargetVersion(int version)

Implements org.codehaus.commons.compiler.ICompiler.setTargetVersion.

Doc from org.codehaus.commons.compiler.ICompiler.setTargetVersion.

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.

Annotations
@Override
setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)

Implements org.codehaus.commons.compiler.ICompiler.setWarningHandler.

Doc from org.codehaus.commons.compiler.ICompiler.setWarningHandler.

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

Parameters
warningHandler:WarningHandler

null to indicate that no warnings be issued

Annotations
@Override