Class CodeBlock
java.lang.Object
io.spring.initializr.generator.language.CodeBlock
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, anotherCodeBlock
, or any type where thetoString()
representation can be used.$S
escapes the value as a string, wraps it with double quotes, and emits that. Emit"null"
if the value isnull
. 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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
static interface
Strategy interface to customize formatting of generated code block. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CodeBlock.FormattingOptions
StandardCodeBlock.FormattingOptions
for Java. -
Method Summary
Modifier and TypeMethodDescriptionstatic CodeBlock.Builder
builder()
Initialize a new builder.Return the imports this instance contributes.static CodeBlock
static CodeBlock
Create a code block using the specified code and optional arguments.static CodeBlock
ofStatement
(String format, Object... args) Create a code block with a single statement using the specified code and optional arguments.void
write
(IndentingWriter writer, CodeBlock.FormattingOptions options) Write this instance using the specified writer.
-
Field Details
-
JAVA_FORMATTING_OPTIONS
StandardCodeBlock.FormattingOptions
for Java.
-
-
Method Details
-
getImports
Return the imports this instance contributes.- Returns:
- the imports.
-
write
Write this instance using the specified writer.- Parameters:
writer
- the writer to useoptions
- the formatting options to use
-
of
Create a code block using the specified code and optional arguments. To create a single statement, considerofStatement(String, Object...)
instead.- Parameters:
format
- the codeargs
- the arguments, if any- Returns:
- a new instance
- See Also:
-
ofStatement
Create a code block with a single statement using the specified code and optional arguments.- Parameters:
format
- the statementargs
- the arguments, if any- Returns:
- a new instance
- See Also:
-
join
JoinscodeBlocks
into a singleCodeBlock
, each separated byseparator
. For example, joiningString s
,Object o
andint i
using", "
would produceString s, Object o, int i
.- Parameters:
codeBlocks
- the code blocks to joinseparator
- the separator to use- Returns:
- a code block joining the specified code blocks
-
joining
ACollector
implementation that joinsCodeBlock
instances together into one separated byseparator
. For example, joiningString s
,Object o
andint i
using", "
would produceString s, Object o, int i
.- Parameters:
separator
- the separator to use- Returns:
- a collector using the specified separator
-
builder
Initialize a new builder.- Returns:
- a code block builder
-