Package com.squareup.javapoet
Class CodeWriter
- java.lang.Object
-
- com.squareup.javapoet.CodeWriter
-
final class CodeWriter extends java.lang.ObjectConverts aJavaFileto 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 classCodeWriter.Multiset<T>
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<java.lang.String>alwaysQualifyprivate booleancommentprivate CodeWriter.Multiset<java.lang.String>currentTypeVariablesprivate java.util.Map<java.lang.String,ClassName>importableTypesprivate java.util.Map<java.lang.String,ClassName>importedTypesprivate java.lang.Stringindentprivate intindentLevelprivate booleanjavadocprivate static java.lang.StringNO_PACKAGESentinel value that indicates that no user-provided package has been set.private LineWrapperoutprivate java.lang.StringpackageNameprivate java.util.Set<java.lang.String>referencedNames(package private) intstatementLineWhen emitting a statement, this is the line of the statement currently being written.private java.util.Set<java.lang.String>staticImportClassNamesprivate java.util.Set<java.lang.String>staticImportsprivate booleantrailingNewlineprivate 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 CodeWriteremit(CodeBlock codeBlock)CodeWriteremit(CodeBlock codeBlock, boolean ensureTrailingNewline)CodeWriteremit(java.lang.String s)CodeWriteremit(java.lang.String format, java.lang.Object... args)(package private) CodeWriteremitAndIndent(java.lang.String s)Emitsswith indentation as required.voidemitAnnotations(java.util.List<AnnotationSpec> annotations, boolean inline)voidemitComment(CodeBlock codeBlock)private voidemitIndentation()voidemitJavadoc(CodeBlock javadocCodeBlock)private voidemitLiteral(java.lang.Object o)voidemitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)voidemitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.Set<javax.lang.model.element.Modifier> implicitModifiers)Emitsmodifiersin the standard order.private booleanemitStaticImportMember(java.lang.String canonical, java.lang.String part)voidemitTypeVariables(java.util.List<TypeVariableName> typeVariables)Emit type variables with their bounds.CodeWriteremitWrappingSpace()private static java.lang.StringextractMemberName(java.lang.String part)private voidimportableType(ClassName className)java.util.Map<java.lang.String,ClassName>importedTypes()CodeWriterindent()CodeWriterindent(int levels)(package private) java.lang.StringlookupName(ClassName className)Returns the best name to identifyclassNamewith in the current context.CodeWriterpopPackage()CodeWriterpopType()voidpopTypeVariables(java.util.List<TypeVariableName> typeVariables)CodeWriterpushPackage(java.lang.String packageName)CodeWriterpushType(TypeSpec type)private ClassNameresolve(java.lang.String simpleName)Returns the class referenced bysimpleName, using the current nesting context and imports.private ClassNamestackClassName(int stackDepth, java.lang.String simpleName)Returns the class namedsimpleNamewhen 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.CodeWriterunindent()CodeWriterunindent(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.IOExceptionEmitsmodifiersin the standard order. Modifiers inimplicitModifierswill 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 identifyclassNamewith 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 namedsimpleNamewhen nested in the class atstackDepth.
-
emitAndIndent
CodeWriter emitAndIndent(java.lang.String s) throws java.io.IOException
Emitsswith indentation as required. It's important that all code that writes tooutdoes 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.
-
-