Package com.squareup.javapoet
Class CodeBlock
- java.lang.Object
-
- com.squareup.javapoet.CodeBlock
-
public final class CodeBlock extends java.lang.Object
A fragment of a .java file, potentially containing declarations, statements, and documentation. Code blocks are not necessarily well-formed Java code, and are not validated. This class assumes javac will check correctness later!Code blocks support placeholders like
Format
. WhereString.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.$>
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CodeBlock.Builder
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.List<java.lang.Object>
args
(package private) java.util.List<java.lang.String>
formatParts
A heterogeneous list containing string literals and value placeholders.
-
Constructor Summary
Constructors Modifier Constructor Description private
CodeBlock(CodeBlock.Builder builder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static CodeBlock.Builder
builder()
boolean
equals(java.lang.Object o)
int
hashCode()
boolean
isEmpty()
static CodeBlock
of(java.lang.String format, java.lang.Object... args)
CodeBlock.Builder
toBuilder()
java.lang.String
toString()
-
-
-
Constructor Detail
-
CodeBlock
private CodeBlock(CodeBlock.Builder builder)
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
of
public static CodeBlock of(java.lang.String format, java.lang.Object... args)
-
builder
public static CodeBlock.Builder builder()
-
toBuilder
public CodeBlock.Builder toBuilder()
-
-