Class CodeBlock

java.lang.Object
io.spring.initializr.generator.language.CodeBlock

public final class CodeBlock extends Object
A fragment of code, potentially containing declarations, or statements. CodeBlocks are not validated.

Code blocks support placeholders identified by $. The following placeholders are supported:

  • $L emits a literal value. Arguments for literals may be plain String, primitives, another CodeBlock, or any type where the toString() representation can be used.
  • $S escapes the value as a string, wraps it with double quotes, and emits that. Emit "null" if the value is null. Does not handle multi-line strings.
  • $T emits a type reference. Arguments for types may be plain classes, class names, fully qualified class names, and fully qualified functions.
  • $$ emits a dollar sign.
  • $] ends a statement and emits the configured statement separator.

Code blocks can be rendered using an IndentingWriter and CodeBlock.FormattingOptions.

This class is heavily inspired by JavaPoet.

Author:
Stephane Nicoll
  • Field Details

  • Method Details

    • getImports

      public List<String> getImports()
      Return the imports this instance contributes.
      Returns:
      the imports.
    • write

      public void write(IndentingWriter writer, CodeBlock.FormattingOptions options)
      Write this instance using the specified writer.
      Parameters:
      writer - the writer to use
      options - the formatting options to use
    • of

      public static CodeBlock of(String format, Object... args)
      Create a code block using the specified code and optional arguments. To create a single statement, consider ofStatement(String, Object...) instead.
      Parameters:
      format - the code
      args - the arguments, if any
      Returns:
      a new instance
      See Also:
    • ofStatement

      public static CodeBlock ofStatement(String format, Object... args)
      Create a code block with a single statement using the specified code and optional arguments.
      Parameters:
      format - the statement
      args - the arguments, if any
      Returns:
      a new instance
      See Also:
    • join

      public static CodeBlock join(Iterable<CodeBlock> codeBlocks, String separator)
      Joins codeBlocks into a single CodeBlock, each separated by separator. For example, joining String s, Object o and int i using ", " would produce String s, Object o, int i.
      Parameters:
      codeBlocks - the code blocks to join
      separator - the separator to use
      Returns:
      a code block joining the specified code blocks
    • joining

      public static Collector<CodeBlock,?,CodeBlock> joining(String separator)
      A Collector implementation that joins CodeBlock instances together into one separated by separator. For example, joining String s, Object o and int i using ", " would produce String s, Object o, int i.
      Parameters:
      separator - the separator to use
      Returns:
      a collector using the specified separator
    • builder

      public static CodeBlock.Builder builder()
      Initialize a new builder.
      Returns:
      a code block builder