Package com.squareup.javapoet
Class CodeWriter
- java.lang.Object
-
- com.squareup.javapoet.CodeWriter
-
final class CodeWriter extends java.lang.Object
Converts aJavaFile
to a string suitable to both human- and javac-consumption. This honors imports, indentation, and deferred variable names.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CodeWriter.Multiset<T>
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<java.lang.String>
alwaysQualify
private boolean
comment
private CodeWriter.Multiset<java.lang.String>
currentTypeVariables
private java.util.Map<java.lang.String,ClassName>
importableTypes
private java.util.Map<java.lang.String,ClassName>
importedTypes
private java.lang.String
indent
private int
indentLevel
private boolean
javadoc
private static java.lang.String
NO_PACKAGE
Sentinel value that indicates that no user-provided package has been set.private LineWrapper
out
private java.lang.String
packageName
private java.util.Set<java.lang.String>
referencedNames
(package private) int
statementLine
When emitting a statement, this is the line of the statement currently being written.private java.util.Set<java.lang.String>
staticImportClassNames
private java.util.Set<java.lang.String>
staticImports
private boolean
trailingNewline
private java.util.List<TypeSpec>
typeSpecStack
-
Constructor Summary
Constructors Constructor Description CodeWriter(java.lang.Appendable out)
CodeWriter(java.lang.Appendable out, java.lang.String indent, java.util.Map<java.lang.String,ClassName> importedTypes, java.util.Set<java.lang.String> staticImports, java.util.Set<java.lang.String> alwaysQualify)
CodeWriter(java.lang.Appendable out, java.lang.String indent, java.util.Set<java.lang.String> staticImports, java.util.Set<java.lang.String> alwaysQualify)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CodeWriter
emit(CodeBlock codeBlock)
CodeWriter
emit(CodeBlock codeBlock, boolean ensureTrailingNewline)
CodeWriter
emit(java.lang.String s)
CodeWriter
emit(java.lang.String format, java.lang.Object... args)
(package private) CodeWriter
emitAndIndent(java.lang.String s)
Emitss
with indentation as required.void
emitAnnotations(java.util.List<AnnotationSpec> annotations, boolean inline)
void
emitComment(CodeBlock codeBlock)
private void
emitIndentation()
void
emitJavadoc(CodeBlock javadocCodeBlock)
private void
emitLiteral(java.lang.Object o)
void
emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)
void
emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.Set<javax.lang.model.element.Modifier> implicitModifiers)
Emitsmodifiers
in the standard order.private boolean
emitStaticImportMember(java.lang.String canonical, java.lang.String part)
void
emitTypeVariables(java.util.List<TypeVariableName> typeVariables)
Emit type variables with their bounds.CodeWriter
emitWrappingSpace()
private static java.lang.String
extractMemberName(java.lang.String part)
private void
importableType(ClassName className)
java.util.Map<java.lang.String,ClassName>
importedTypes()
CodeWriter
indent()
CodeWriter
indent(int levels)
(package private) java.lang.String
lookupName(ClassName className)
Returns the best name to identifyclassName
with in the current context.CodeWriter
popPackage()
CodeWriter
popType()
void
popTypeVariables(java.util.List<TypeVariableName> typeVariables)
CodeWriter
pushPackage(java.lang.String packageName)
CodeWriter
pushType(TypeSpec type)
private ClassName
resolve(java.lang.String simpleName)
Returns the class referenced bysimpleName
, using the current nesting context and imports.private ClassName
stackClassName(int stackDepth, java.lang.String simpleName)
Returns the class namedsimpleName
when nested in the class atstackDepth
.(package private) java.util.Map<java.lang.String,ClassName>
suggestedImports()
Returns the types that should have been imported for this code.CodeWriter
unindent()
CodeWriter
unindent(int levels)
-
-
-
Field Detail
-
NO_PACKAGE
private static final java.lang.String NO_PACKAGE
Sentinel value that indicates that no user-provided package has been set.
-
indent
private final java.lang.String indent
-
out
private final LineWrapper out
-
indentLevel
private int indentLevel
-
javadoc
private boolean javadoc
-
comment
private boolean comment
-
packageName
private java.lang.String packageName
-
typeSpecStack
private final java.util.List<TypeSpec> typeSpecStack
-
staticImportClassNames
private final java.util.Set<java.lang.String> staticImportClassNames
-
staticImports
private final java.util.Set<java.lang.String> staticImports
-
alwaysQualify
private final java.util.Set<java.lang.String> alwaysQualify
-
importedTypes
private final java.util.Map<java.lang.String,ClassName> importedTypes
-
importableTypes
private final java.util.Map<java.lang.String,ClassName> importableTypes
-
referencedNames
private final java.util.Set<java.lang.String> referencedNames
-
currentTypeVariables
private final CodeWriter.Multiset<java.lang.String> currentTypeVariables
-
trailingNewline
private boolean trailingNewline
-
statementLine
int statementLine
When 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 Detail
-
CodeWriter
CodeWriter(java.lang.Appendable out)
-
CodeWriter
CodeWriter(java.lang.Appendable out, java.lang.String indent, java.util.Set<java.lang.String> staticImports, java.util.Set<java.lang.String> alwaysQualify)
-
CodeWriter
CodeWriter(java.lang.Appendable out, java.lang.String indent, java.util.Map<java.lang.String,ClassName> importedTypes, java.util.Set<java.lang.String> staticImports, java.util.Set<java.lang.String> alwaysQualify)
-
-
Method Detail
-
importedTypes
public java.util.Map<java.lang.String,ClassName> importedTypes()
-
indent
public CodeWriter indent()
-
indent
public CodeWriter indent(int levels)
-
unindent
public CodeWriter unindent()
-
unindent
public CodeWriter unindent(int levels)
-
pushPackage
public CodeWriter pushPackage(java.lang.String packageName)
-
popPackage
public CodeWriter popPackage()
-
pushType
public CodeWriter pushType(TypeSpec type)
-
popType
public CodeWriter popType()
-
emitComment
public void emitComment(CodeBlock codeBlock) throws java.io.IOException
- Throws:
java.io.IOException
-
emitJavadoc
public void emitJavadoc(CodeBlock javadocCodeBlock) throws java.io.IOException
- Throws:
java.io.IOException
-
emitAnnotations
public void emitAnnotations(java.util.List<AnnotationSpec> annotations, boolean inline) throws java.io.IOException
- Throws:
java.io.IOException
-
emitModifiers
public void emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.Set<javax.lang.model.element.Modifier> implicitModifiers) throws java.io.IOException
Emitsmodifiers
in the standard order. Modifiers inimplicitModifiers
will not be emitted.- Throws:
java.io.IOException
-
emitModifiers
public void emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
- Throws:
java.io.IOException
-
emitTypeVariables
public void emitTypeVariables(java.util.List<TypeVariableName> typeVariables) throws java.io.IOException
Emit type variables with their bounds. This should only be used when declaring type variables; everywhere else bounds are omitted.- Throws:
java.io.IOException
-
popTypeVariables
public void popTypeVariables(java.util.List<TypeVariableName> typeVariables) throws java.io.IOException
- Throws:
java.io.IOException
-
emit
public CodeWriter emit(java.lang.String s) throws java.io.IOException
- Throws:
java.io.IOException
-
emit
public CodeWriter emit(java.lang.String format, java.lang.Object... args) throws java.io.IOException
- Throws:
java.io.IOException
-
emit
public CodeWriter emit(CodeBlock codeBlock) throws java.io.IOException
- Throws:
java.io.IOException
-
emit
public CodeWriter emit(CodeBlock codeBlock, boolean ensureTrailingNewline) throws java.io.IOException
- Throws:
java.io.IOException
-
emitWrappingSpace
public CodeWriter emitWrappingSpace() throws java.io.IOException
- Throws:
java.io.IOException
-
extractMemberName
private static java.lang.String extractMemberName(java.lang.String part)
-
emitStaticImportMember
private boolean emitStaticImportMember(java.lang.String canonical, java.lang.String part) throws java.io.IOException
- Throws:
java.io.IOException
-
emitLiteral
private void emitLiteral(java.lang.Object o) throws java.io.IOException
- Throws:
java.io.IOException
-
lookupName
java.lang.String lookupName(ClassName className)
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
private void importableType(ClassName className)
-
resolve
private ClassName resolve(java.lang.String simpleName)
Returns the class referenced bysimpleName
, using the current nesting context and imports.
-
stackClassName
private ClassName stackClassName(int stackDepth, java.lang.String simpleName)
Returns the class namedsimpleName
when nested in the class atstackDepth
.
-
emitAndIndent
CodeWriter emitAndIndent(java.lang.String s) throws java.io.IOException
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:
java.io.IOException
-
emitIndentation
private void emitIndentation() throws java.io.IOException
- Throws:
java.io.IOException
-
suggestedImports
java.util.Map<java.lang.String,ClassName> 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.
-
-