Top Description Inners Fields Constructors Methods
org.springframework.javapoet

public final Class CodeBlock

extends Object
Class Inheritance
Imports
java.io.IOException, java.lang.reflect.Type, java.util.ArrayList, .List, .Map, java.util.regex.Matcher, .Pattern, java.util.stream.Collector, .StreamSupport, javax.lang.model.element.Element, javax.lang.model.type.TypeMirror

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 java.text.Format. Where String#format uses percent % to reference target values, this class uses dollar sign $ and has its own set of permitted placeholders:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class

Field Summary

Modifier and TypeField and Description
pack-priv final List<Object>
pack-priv final List<String>
formatParts

A heterogeneous list containing string literals and value placeholders.

private static final Pattern
private static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CodeBlock.Builder
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
public static CodeBlock
join(Iterable<CodeBlock> codeBlocks, String separator)

Joins codeBlocks into a single CodeBlock, each separated by separator.

public static Collector<CodeBlock, ?, CodeBlock>
joining(String separator)

A Collector implementation that joins CodeBlock instances together into one separated by separator.

public static Collector<CodeBlock, ?, CodeBlock>
joining(String separator, String prefix, String suffix)

A Collector implementation that joins CodeBlock instances together into one separated by separator.

public static CodeBlock
of(String format, Object... args)

public CodeBlock.Builder
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

argsback to summary
pack-priv final List<Object> args
formatPartsback to summary
pack-priv final List<String> formatParts

A heterogeneous list containing string literals and value placeholders.

LOWERCASEback to summary
private static final Pattern LOWERCASE
NAMED_ARGUMENTback to summary
private static final Pattern NAMED_ARGUMENT

Constructor Detail

CodeBlockback to summary
private CodeBlock(CodeBlock.Builder builder)

Method Detail

builderback to summary
public static CodeBlock.Builder builder()
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()
joinback to summary
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.

joiningback to summary
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.

joiningback to summary
public static Collector<CodeBlock, ?, CodeBlock> joining(String separator, String prefix, String suffix)

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.

ofback to summary
public static CodeBlock of(String format, Object... args)
toBuilderback to summary
public CodeBlock.Builder toBuilder()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.javapoet back to summary

public final Class CodeBlock.Builder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final List<Object>
pack-priv final List<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public CodeBlock.Builder
add(String format, Object... args)

Add code with positional or relative arguments.

public CodeBlock.Builder
add(CodeBlock codeBlock)

private void
addArgument(String format, char c, Object arg)

public CodeBlock.Builder
addNamed(String format, Map<String, ?> arguments)

Adds code using named arguments.

public CodeBlock.Builder
addStatement(String format, Object... args)

public CodeBlock.Builder
private Object
private String
private String
private TypeName
public CodeBlock.Builder
beginControlFlow(String
the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.
controlFlow
,
Object... args)

public CodeBlock
public CodeBlock.Builder
public CodeBlock.Builder
public CodeBlock.Builder
endControlFlow(String
the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.
controlFlow
,
Object... args)

public CodeBlock.Builder
public boolean
private boolean
public CodeBlock.Builder
nextControlFlow(String
the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.
controlFlow
,
Object... args)

public CodeBlock.Builder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

argsback to summary
pack-priv final List<Object> args
formatPartsback to summary
pack-priv final List<String> formatParts

Constructor Detail

Builderback to summary
private Builder()

Method Detail

addback to summary
public CodeBlock.Builder add(String format, Object... args)

Add code with positional or relative arguments.

Relative arguments map 1:1 with the placeholders in the format string.

Positional arguments use an index after the placeholder to identify which argument index to use. For example, for a literal to reference the 3rd argument: "$3L" (1 based index)

Mixing relative and positional arguments in a call to add is invalid and will result in an error.

addback to summary
public CodeBlock.Builder add(CodeBlock codeBlock)
addArgumentback to summary
private void addArgument(String format, char c, Object arg)
addNamedback to summary
public CodeBlock.Builder addNamed(String format, Map<String, ?> arguments)

Adds code using named arguments.

Named arguments specify their name after the '$' followed by : and the corresponding type character. Argument names consist of characters in a-z, A-Z, 0-9, and _ and must start with a lowercase character.

For example, to refer to the type java.lang.Integer with the argument name clazz use a format string containing $clazz:T and include the key clazz with value java.lang.Integer.class in the argument map.

addStatementback to summary
public CodeBlock.Builder addStatement(String format, Object... args)
addStatementback to summary
public CodeBlock.Builder addStatement(CodeBlock codeBlock)
argToLiteralback to summary
private Object argToLiteral(Object o)
argToNameback to summary
private String argToName(Object o)
argToStringback to summary
private String argToString(Object o)
argToTypeback to summary
private TypeName argToType(Object o)
beginControlFlowback to summary
public CodeBlock.Builder beginControlFlow(String controlFlow, Object... args)
Parameters
controlFlow:String

the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.

buildback to summary
public CodeBlock build()
clearback to summary
public CodeBlock.Builder clear()
endControlFlowback to summary
public CodeBlock.Builder endControlFlow()
endControlFlowback to summary
public CodeBlock.Builder endControlFlow(String controlFlow, Object... args)
Parameters
controlFlow:String

the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.

indentback to summary
public CodeBlock.Builder indent()
isEmptyback to summary
public boolean isEmpty()
isNoArgPlaceholderback to summary
private boolean isNoArgPlaceholder(char c)
nextControlFlowback to summary
public CodeBlock.Builder nextControlFlow(String controlFlow, Object... args)
Parameters
controlFlow:String

the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.

unindentback to summary
public CodeBlock.Builder unindent()
org.springframework.javapoet back to summary

private final Class CodeBlock.CodeBlockJoiner

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final CodeBlock.Builder
private final String
private boolean

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv CodeBlock.CodeBlockJoiner
add(CodeBlock codeBlock)

pack-priv CodeBlock
join()

pack-priv CodeBlock.CodeBlockJoiner
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

builderback to summary
private final CodeBlock.Builder builder
delimiterback to summary
private final String delimiter
firstback to summary
private boolean first

Constructor Detail

CodeBlockJoinerback to summary
pack-priv CodeBlockJoiner(String delimiter, CodeBlock.Builder builder)

Method Detail

addback to summary
pack-priv CodeBlock.CodeBlockJoiner add(CodeBlock codeBlock)
joinback to summary
pack-priv CodeBlock join()
mergeback to summary
pack-priv CodeBlock.CodeBlockJoiner merge(CodeBlock.CodeBlockJoiner other)