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:
$Lemits a literal value. Arguments for literals may be plain String, primitives, anotherCodeBlock, or any type where thetoString()representation can be used.$Sescapes 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.$Temits 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 classstatic interfaceStrategy interface to customize formatting of generated code block. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CodeBlock.FormattingOptionsStandardCodeBlock.FormattingOptionsfor Java. -
Method Summary
Modifier and TypeMethodDescriptionstatic CodeBlock.Builderbuilder()Initialize a new builder.Return the imports this instance contributes.static CodeBlockstatic CodeBlockCreate a code block using the specified code and optional arguments.static CodeBlockofStatement(String format, Object... args) Create a code block with a single statement using the specified code and optional arguments.voidwrite(IndentingWriter writer, CodeBlock.FormattingOptions options) Write this instance using the specified writer.
-
Field Details
-
JAVA_FORMATTING_OPTIONS
StandardCodeBlock.FormattingOptionsfor 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
JoinscodeBlocksinto a singleCodeBlock, each separated byseparator. For example, joiningString s,Object oandint iusing", "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
ACollectorimplementation that joinsCodeBlockinstances together into one separated byseparator. For example, joiningString s,Object oandint iusing", "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
-