public final class CodeBlock
extends java.lang.Object
Code blocks support placeholders like Format
. Where String.format(java.lang.String, java.lang.Object...)
uses percent %
to reference target values, this class uses dollar sign $
and has
its own set of permitted placeholders:
$L
emits a literal value with no escaping. Arguments for literals may be
strings, primitives, type declarations, annotations and even other code blocks.
$N
emits a name, using name collision avoidance where necessary. Arguments
for names may be strings (actually any character sequence),
parameters, fields, methods, and types.
$S
escapes the value as a string, wraps it with double quotes, and emits
that. For example, 6" sandwich
is emitted "6\" sandwich"
.
$T
emits a type reference. Types will be imported if possible. Arguments
for types may be classes, ,* type mirrors, and elements.
$$
emits a dollar sign.
$W
emits a space or a newline, depending on its position on the line. This prefers
to wrap lines before 100 columns.
$Z
acts as a zero-width space. This prefers to wrap lines before 100 columns.
$>
increases the indentation level.
$<
decreases the indentation level.
$[
begins a statement. For multiline statements, every line after the first line
is double-indented.
$]
ends a statement.
Modifier and Type | Class and Description |
---|---|
static class |
CodeBlock.Builder |
Modifier and Type | Method and Description |
---|---|
static CodeBlock.Builder |
builder() |
boolean |
equals(java.lang.Object o) |
int |
hashCode() |
boolean |
isEmpty() |
static CodeBlock |
join(java.lang.Iterable<CodeBlock> codeBlocks,
java.lang.String separator)
|
static java.util.stream.Collector<CodeBlock,?,CodeBlock> |
joining(java.lang.String separator)
|
static java.util.stream.Collector<CodeBlock,?,CodeBlock> |
joining(java.lang.String separator,
java.lang.String prefix,
java.lang.String suffix)
|
static CodeBlock |
of(java.lang.String format,
java.lang.Object... args) |
CodeBlock.Builder |
toBuilder() |
java.lang.String |
toString() |
public boolean isEmpty()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static CodeBlock of(java.lang.String format, java.lang.Object... args)
public static CodeBlock join(java.lang.Iterable<CodeBlock> codeBlocks, java.lang.String separator)
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
.public static java.util.stream.Collector<CodeBlock,?,CodeBlock> joining(java.lang.String separator)
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
.public static java.util.stream.Collector<CodeBlock,?,CodeBlock> joining(java.lang.String separator, java.lang.String prefix, java.lang.String suffix)
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
.public static CodeBlock.Builder builder()
public CodeBlock.Builder toBuilder()
Copyright © 2020 Square, Inc.. All Rights Reserved.