Class TestCompiler

java.lang.Object
org.springframework.aot.test.generate.compile.TestCompiler

public final class TestCompiler extends Object
Utility that can be used to dynamically compile and test Java source code.
Since:
6.0
Author:
Phillip Webb, Scott Frederick
See Also:
  • Method Details

    • forSystem

      public static TestCompiler forSystem()
      Create a new TestCompiler backed by the system java compiler.
      Returns:
      a new TestCompiler instance
    • forCompiler

      public static TestCompiler forCompiler(JavaCompiler javaCompiler)
      Create a new TestCompiler backed by the given JavaCompiler.
      Parameters:
      javaCompiler - the java compiler to use
      Returns:
      a new TestCompiler instance
    • withFiles

      public TestCompiler withFiles(InMemoryGeneratedFiles generatedFiles)
      Create a new TestCompiler instance with additional generated source and resource files.
      Parameters:
      generatedFiles - the generated files to add
      Returns:
      a new TestCompiler instance
    • withSources

      public TestCompiler withSources(SourceFile... sourceFiles)
      Create a new TestCompiler instance with additional source files.
      Parameters:
      sourceFiles - the additional source files
      Returns:
      a new TestCompiler instance
    • withSources

      public TestCompiler withSources(Iterable<SourceFile> sourceFiles)
      Create a new TestCompiler instance with additional source files.
      Parameters:
      sourceFiles - the additional source files
      Returns:
      a new TestCompiler instance
    • withSources

      public TestCompiler withSources(SourceFiles sourceFiles)
      Create a new TestCompiler instance with additional source files.
      Parameters:
      sourceFiles - the additional source files
      Returns:
      a new TestCompiler instance
    • withResources

      public TestCompiler withResources(ResourceFile... resourceFiles)
      Create a new TestCompiler instance with additional resource files.
      Parameters:
      resourceFiles - the additional resource files
      Returns:
      a new TestCompiler instance
    • withResources

      public TestCompiler withResources(Iterable<ResourceFile> resourceFiles)
      Create a new TestCompiler instance with additional source files.
      Parameters:
      resourceFiles - the additional source files
      Returns:
      a new TestCompiler instance
    • withResources

      public TestCompiler withResources(ResourceFiles resourceFiles)
      Create a new TestCompiler instance with additional resource files.
      Parameters:
      resourceFiles - the additional resource files
      Returns:
      a new TestCompiler instance
    • withProcessors

      public TestCompiler withProcessors(Processor... processors)
      Create a new TestCompiler instance with additional annotation processors.
      Parameters:
      processors - the additional annotation processors
      Returns:
      a new TestCompiler instance
    • withProcessors

      public TestCompiler withProcessors(Iterable<Processor> processors)
      Create a new TestCompiler instance with additional annotation processors.
      Parameters:
      processors - the additional annotation processors
      Returns:
      a new TestCompiler instance
    • compile

      public void compile(WritableContent content, Consumer<Compiled> compiled)
      Compile content from this instance along with the additional provided content.
      Parameters:
      content - the additional content to compile
      compiled - a consumed used to further assert the compiled code
      Throws:
      CompilationException - if source cannot be compiled
    • compile

      public void compile(SourceFile sourceFile, Consumer<Compiled> compiled)
      Compile content from this instance along with the additional provided source file.
      Parameters:
      sourceFile - the additional source file to compile
      compiled - a consumed used to further assert the compiled code
      Throws:
      CompilationException - if source cannot be compiled
    • compile

      public void compile(SourceFiles sourceFiles, Consumer<Compiled> compiled)
      Compile content from this instance along with the additional provided source files.
      Parameters:
      sourceFiles - the additional source files to compile
      compiled - a consumed used to further assert the compiled code
      Throws:
      CompilationException - if source cannot be compiled
    • compile

      public void compile(SourceFiles sourceFiles, ResourceFiles resourceFiles, Consumer<Compiled> compiled)
      Compile content from this instance along with the additional provided source and resource files.
      Parameters:
      sourceFiles - the additional source files to compile
      resourceFiles - the additional resource files to include
      compiled - a Consumer used to further assert the compiled code
      Throws:
      CompilationException - if source cannot be compiled
    • compile

      public void compile(Consumer<Compiled> compiled) throws CompilationException
      Compile content from this instance.
      Parameters:
      compiled - a Consumer used to further assert the compiled code
      Throws:
      CompilationException - if source cannot be compiled
    • printFiles

      public TestCompiler printFiles(PrintStream printStream)
      Print the contents of the source and resource files to the specified PrintStream.
      Parameters:
      printStream - the destination print stream
      Returns:
      this instance