Class TestCompiler

java.lang.Object
org.springframework.core.test.tools.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, Stephane Nicoll
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
    • with

      public TestCompiler with(UnaryOperator<TestCompiler> customizer)
      Apply customization to this compiler.
      Parameters:
      customizer - the customizer to call
      Returns:
      a new TestCompiler instance with the customizations applied
    • 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
    • withClasses

      public TestCompiler withClasses(Iterable<ClassFile> classFiles)
      Create a new TestCompiler instance with additional classes.
      Parameters:
      classFiles - the additional classes
      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
    • withCompilerOptions

      public TestCompiler withCompilerOptions(String... options)
      Create a new TestCompiler instance with the additional compiler options.
      Parameters:
      options - the additional compiler options
      Returns:
      a new TestCompiler instance
      Since:
      6.1
    • failOnWarning

      public TestCompiler failOnWarning()
      Create a new TestCompiler instance that fails if any warning is encountered. This sets the -Xlint:all and -Werror compiler options.
      Returns:
      a new TestCompiler instance
      Since:
      6.1
      See Also:
    • 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