JavaPoet Cheatsheet

Last modified: March 1, 2023

Placeholders

$L - literal value with no escaping.
$L
$N emits a name, using name collision avoidance where necessary
addStatement("$N.toString()", fieldName)
// output
name.toString();
$S escapes the value as a string
addStatement("String initialValue = $S", initialValue)
// output
String initialValue = "EXCHANGE_RATE";
$T emits a type reference
addStatement("$T a = $S", String.class, "")
// output
String a = "";
$$ emits a dollar ($) sign

Control flow