Package com.squareup.javapoet
Class CodeWriter
java.lang.Object
com.squareup.javapoet.CodeWriter
Converts a
JavaFile
to a string suitable to both human- and javac-consumption. This
honors imports, indentation, and deferred variable names.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final String
private int
private boolean
private static final String
Sentinel value that indicates that no user-provided package has been set.private final Appendable
private String
(package private) int
When emitting a statement, this is the line of the statement currently being written.private boolean
-
Constructor Summary
ConstructorsConstructorDescriptionCodeWriter
(Appendable out) CodeWriter
(Appendable out, String indent, Map<String, ClassName> importedTypes, Set<String> staticImports) CodeWriter
(Appendable out, String indent, Set<String> staticImports) -
Method Summary
Modifier and TypeMethodDescription(package private) CodeWriter
Emitss
with indentation as required.void
emitAnnotations
(List<AnnotationSpec> annotations, boolean inline) void
emitComment
(CodeBlock codeBlock) private void
void
emitJavadoc
(CodeBlock javadocCodeBlock) private void
void
emitModifiers
(Set<Modifier> modifiers) void
emitModifiers
(Set<Modifier> modifiers, Set<Modifier> implicitModifiers) Emitsmodifiers
in the standard order.private boolean
emitStaticImportMember
(String canonical, String part) void
emitTypeVariables
(List<TypeVariableName> typeVariables) Emit type variables with their bounds.private static String
extractMemberName
(String part) private void
importableType
(ClassName className) indent()
indent
(int levels) (package private) String
lookupName
(ClassName className) Returns the best name to identifyclassName
with in the current context.popType()
pushPackage
(String packageName) private ClassName
Returns the class referenced bysimpleName
, using the current nesting context and imports.private ClassName
stackClassName
(int stackDepth, String simpleName) Returns the class namedsimpleName
when nested in the class atstackDepth
.Returns the types that should have been imported for this code.unindent()
unindent
(int levels)
-
Field Details
-
NO_PACKAGE
Sentinel value that indicates that no user-provided package has been set. -
indent
-
out
-
indentLevel
private int indentLevel -
javadoc
private boolean javadoc -
comment
private boolean comment -
packageName
-
typeSpecStack
-
staticImportClassNames
-
staticImports
-
importedTypes
-
importableTypes
-
referencedNames
-
trailingNewline
private boolean trailingNewline -
statementLine
int statementLineWhen emitting a statement, this is the line of the statement currently being written. The first line of a statement is indented normally and subsequent wrapped lines are double-indented. This is -1 when the currently-written line isn't part of a statement.
-
-
Constructor Details
-
CodeWriter
CodeWriter(Appendable out) -
CodeWriter
CodeWriter(Appendable out, String indent, Set<String> staticImports) -
CodeWriter
-
-
Method Details
-
importedTypes
-
indent
-
indent
-
unindent
-
unindent
-
pushPackage
-
popPackage
-
pushType
-
popType
-
emitComment
- Throws:
IOException
-
emitJavadoc
- Throws:
IOException
-
emitAnnotations
- Throws:
IOException
-
emitModifiers
public void emitModifiers(Set<Modifier> modifiers, Set<Modifier> implicitModifiers) throws IOException Emitsmodifiers
in the standard order. Modifiers inimplicitModifiers
will not be emitted.- Throws:
IOException
-
emitModifiers
- Throws:
IOException
-
emitTypeVariables
Emit type variables with their bounds. This should only be used when declaring type variables; everywhere else bounds are omitted.- Throws:
IOException
-
emit
- Throws:
IOException
-
emit
- Throws:
IOException
-
emit
- Throws:
IOException
-
extractMemberName
-
emitStaticImportMember
- Throws:
IOException
-
emitLiteral
- Throws:
IOException
-
lookupName
Returns the best name to identifyclassName
with in the current context. This uses the available imports and the current scope to find the shortest name available. It does not honor names visible due to inheritance. -
importableType
-
resolve
Returns the class referenced bysimpleName
, using the current nesting context and imports. -
stackClassName
Returns the class namedsimpleName
when nested in the class atstackDepth
. -
emitAndIndent
Emitss
with indentation as required. It's important that all code that writes toout
does it through here, since we emit indentation lazily in order to avoid unnecessary trailing whitespace.- Throws:
IOException
-
emitIndentation
- Throws:
IOException
-
suggestedImports
Returns the types that should have been imported for this code. If there were any simple name collisions, that type's first use is imported.
-