Class TestCompiler

java.lang.Object
be.imgn.gaia.processing.testing.TestCompiler

public final class TestCompiler extends Object
Compiles in-memory sources with the system Java compiler, in process, running the given annotation processors.
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 Details

    • javac

      public static TestCompiler javac()
      Returns a compiler with no annotation processor and no option.
      Returns:
      a compiler
    • source

      public static JavaFileObject source(String qualifiedName, String text)
      Returns an in-memory compilation unit.
      Parameters:
      qualifiedName - the fully qualified name of the top-level type the source declares; or module-info for a module declaration; or package-info after the name of the package, if any, for a package declaration
      text - the source text
      Returns:
      the compilation unit, named mem:///<package path>/<simple name>.java
      Throws:
      IllegalArgumentException - if qualifiedName is not a qualified name, module-info or package-info
    • withProcessors

      public TestCompiler withProcessors(Processor... processors)
      Returns a compiler that runs processors, 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

      public TestCompiler withOptions(String... options)
      Returns a compiler that passes options to 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"): the processing lint 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 with Filer.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_OUTPUT is 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 in StandardLocation.SOURCE_PATH is in a source path of its own, so the options must not give one, and the sources must not have a module-info: javac would then look for them in that source path.

      Parameters:
      location - StandardLocation.CLASS_OUTPUT or StandardLocation.SOURCE_PATH
      relativePath - the path of the resource relative to the location, separated by /, such as example/MainView.toml
      text - the content of the resource, written in UTF-8
      Returns:
      a compiler giving that resource too
      Throws:
      IllegalArgumentException - if location is another location, or if relativePath is 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, as withResource(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_OUTPUT or StandardLocation.SOURCE_PATH
      relativePath - the path of the resource relative to the location, separated by /, such as example/main.png
      content - the content of the resource, which is copied
      Returns:
      a compiler giving that resource too
      Throws:
      IllegalArgumentException - if location is another location, or if relativePath is empty, starts or ends with /, contains \, or has an empty, . or .. segment
    • compile

      public Compilation compile(JavaFileObject... sources)
      Compiles sources.

      What javac writes besides its diagnostics, such as the output of -verbose, is logged at System.Logger.Level.INFO with System.Logger.

      Parameters:
      sources - the compilation units, such as the ones source(String, String) returns
      Returns:
      the outcome; a compilation error is reported in it, not thrown
      Throws:
      IllegalArgumentException - if sources is 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 in StandardLocation.SOURCE_PATH, or if the sources have a module-info while a resource is in StandardLocation.SOURCE_PATH
      IllegalStateException - 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 message
      RuntimeException - if an annotation processor, or a file given to javac, throws an exception, which is the cause of the one thrown
      UncheckedIOException - 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

      public Compilation compile(List<? extends JavaFileObject> sources)
      Compiles sources.

      What javac writes besides its diagnostics, such as the output of -verbose, is logged at System.Logger.Level.INFO with System.Logger.

      Parameters:
      sources - the compilation units, such as the ones source(String, String) returns
      Returns:
      the outcome; a compilation error is reported in it, not thrown
      Throws:
      IllegalArgumentException - if sources is 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 in StandardLocation.SOURCE_PATH, or if the sources have a module-info while a resource is in StandardLocation.SOURCE_PATH
      IllegalStateException - 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 message
      RuntimeException - if an annotation processor, or a file given to javac, throws an exception, which is the cause of the one thrown
      UncheckedIOException - 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