Class TestCompiler
Compilation compilation = TestCompiler.javac()
.withProcessors(new MyProcessor())
.withOptions("-Xlint:all,-processing", "-Werror")
.compile(TestCompiler.source("example.Foo", """
package example;
class Foo {}
"""));
The sources are compiled against the class path and the module path of the running JVM, unless the options give a
module path of their own. Sources outside a module read every module of that path; sources that include a
module-info.java read what it requires. Generated sources, class files and resources are written to temporary
directories, read back into the Compilation, and the directories are deleted.
A TestCompiler is immutable: withProcessors(Processor...), withOptions(String...) and
the withResource overloads return a new one.
-
Method Summary
Modifier and TypeMethodDescriptioncompile(List<? extends JavaFileObject> sources) Compilessources.compile(JavaFileObject... sources) Compilessources.static TestCompilerjavac()Returns a compiler with no annotation processor and no option.static JavaFileObjectReturns an in-memory compilation unit.withOptions(String... options) Returns a compiler that passesoptionsto javac, instead of the options this one passes.withProcessors(Processor... processors) Returns a compiler that runsprocessors, instead of the processors this one runs.withResource(JavaFileManager.Location location, String relativePath, byte[] content) Returns a compiler that also gives the compilation a binary resource, such as an image, aswithResource(JavaFileManager.Location, String, String)does with text.withResource(JavaFileManager.Location location, String relativePath, String text) Returns a compiler that also gives the compilation a resource, such as a file an annotation processor reads withFiler.getResource(JavaFileManager.Location, CharSequence, CharSequence), besides the resources this one gives.
-
Method Details
-
javac
Returns a compiler with no annotation processor and no option.- Returns:
- a compiler
-
source
Returns an in-memory compilation unit.- Parameters:
qualifiedName- the fully qualified name of the top-level type the source declares; ormodule-infofor a module declaration; orpackage-infoafter the name of the package, if any, for a package declarationtext- the source text- Returns:
- the compilation unit, named
mem:///<package path>/<simple name>.java - Throws:
IllegalArgumentException- ifqualifiedNameis not a qualified name,module-infoorpackage-info
-
withProcessors
Returns a compiler that runsprocessors, instead of the processors this one runs. Processors are stateful: give each compilation new instances.- Parameters:
processors- the annotation processors- Returns:
- a compiler running
processors
-
withOptions
Returns a compiler that passesoptionsto javac, instead of the options this one passes. They come after-proc:full, which is always passed.To check that generated code compiles cleanly, use
withOptions("-Xlint:all,-processing", "-Werror"): theprocessinglint warns whenever a processor does not claim its annotations, which well-behaved processors never do.javac checks the options when
compile(List)runs: telling an option from the argument of another takes its file manager.- Parameters:
options- the javac options- Returns:
- a compiler passing
options
-
withResource
public TestCompiler withResource(JavaFileManager.Location location, String relativePath, String text) Returns a compiler that also gives the compilation a resource, such as a file an annotation processor reads withFiler.getResource(JavaFileManager.Location, CharSequence, CharSequence), besides the resources this one gives. It replaces the resource this one gives at the same location and path, if any.Compilation compilation = TestCompiler.javac() .withProcessors(new ViewProcessor()) .withResource(StandardLocation.CLASS_OUTPUT, "example/MainView.toml", """ title = "Main" """) .compile(TestCompiler.source("example.MainView", "..."));A resource in
StandardLocation.CLASS_OUTPUTis there before javac starts, as are the resources a build tool copies to the class output before compiling. It is not among the generated files, unless the compilation writes other content to it. A resource inStandardLocation.SOURCE_PATHis in a source path of its own, so the options must not give one, and the sources must not have amodule-info: javac would then look for them in that source path.- Parameters:
location-StandardLocation.CLASS_OUTPUTorStandardLocation.SOURCE_PATHrelativePath- the path of the resource relative to the location, separated by/, such asexample/MainView.tomltext- the content of the resource, written in UTF-8- Returns:
- a compiler giving that resource too
- Throws:
IllegalArgumentException- iflocationis another location, or ifrelativePathis empty, starts or ends with/, contains\, or has an empty,.or..segment
-
withResource
public TestCompiler withResource(JavaFileManager.Location location, String relativePath, byte[] content) Returns a compiler that also gives the compilation a binary resource, such as an image, aswithResource(JavaFileManager.Location, String, String)does with text.Compilation compilation = TestCompiler.javac() .withProcessors(new ViewProcessor()) .withResource(StandardLocation.CLASS_OUTPUT, "example/main.png", Files.readAllBytes(icon)) .compile(TestCompiler.source("example.MainView", "..."));- Parameters:
location-StandardLocation.CLASS_OUTPUTorStandardLocation.SOURCE_PATHrelativePath- the path of the resource relative to the location, separated by/, such asexample/main.pngcontent- the content of the resource, which is copied- Returns:
- a compiler giving that resource too
- Throws:
IllegalArgumentException- iflocationis another location, or ifrelativePathis empty, starts or ends with/, contains\, or has an empty,.or..segment
-
compile
Compilessources.What javac writes besides its diagnostics, such as the output of
-verbose, is logged atSystem.Logger.Level.INFOwithSystem.Logger.- Parameters:
sources- the compilation units, such as the onessource(String, String)returns- Returns:
- the outcome; a compilation error is reported in it, not thrown
- Throws:
IllegalArgumentException- ifsourcesis empty or holds a file that is not a source file, if javac rejects an option: unknown, without its argument, or with an invalid value, or if the options give a source path while a resource is inStandardLocation.SOURCE_PATH, or if the sources have amodule-infowhile a resource is inStandardLocation.SOURCE_PATHIllegalStateException- if the running Java has no system compiler, if javac finds options that cannot be used together, or if javac fails without reporting an error, as when it crashes, with the output of javac as its messageRuntimeException- if an annotation processor, or a file given to javac, throws an exception, which is the cause of the one thrownUncheckedIOException- if the temporary directories cannot be written, read or deleted; a failure to delete them while another exception is thrown is added to that exception as a suppressed one
-
compile
Compilessources.What javac writes besides its diagnostics, such as the output of
-verbose, is logged atSystem.Logger.Level.INFOwithSystem.Logger.- Parameters:
sources- the compilation units, such as the onessource(String, String)returns- Returns:
- the outcome; a compilation error is reported in it, not thrown
- Throws:
IllegalArgumentException- ifsourcesis empty or holds a file that is not a source file, if javac rejects an option: unknown, without its argument, or with an invalid value, or if the options give a source path while a resource is inStandardLocation.SOURCE_PATH, or if the sources have amodule-infowhile a resource is inStandardLocation.SOURCE_PATHIllegalStateException- if the running Java has no system compiler, if javac finds options that cannot be used together, or if javac fails without reporting an error, as when it crashes, with the output of javac as its messageRuntimeException- if an annotation processor, or a file given to javac, throws an exception, which is the cause of the one thrownUncheckedIOException- if the temporary directories cannot be written, read or deleted; a failure to delete them while another exception is thrown is added to that exception as a suppressed one
-