Class CodeBlock.Builder
- Enclosing class:
- CodeBlock
-
Method Summary
Modifier and TypeMethodDescriptionAdd code with positional or relative arguments.Adds code using named arguments.addStatement
(CodeBlock codeBlock) addStatement
(String format, Object... args) beginControlFlow
(String controlFlow, Object... args) build()
clear()
endControlFlow
(String controlFlow, Object... args) indent()
boolean
isEmpty()
nextControlFlow
(String controlFlow, Object... args) unindent()
-
Method Details
-
isEmpty
public boolean isEmpty() -
addNamed
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
Integer
with the argument nameclazz
use a format string containing$clazz:T
and include the keyclazz
with valuejava.lang.Integer.class
in the argument map. -
add
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.
-
beginControlFlow
- Parameters:
controlFlow
- the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.
-
nextControlFlow
- Parameters:
controlFlow
- the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.
-
endControlFlow
-
endControlFlow
- Parameters:
controlFlow
- the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.
-
addStatement
-
addStatement
-
add
-
indent
-
unindent
-
clear
-
build
-