Top Description Inners Fields Constructors Methods
com.squareup.javapoet

pack-priv final Class CodeWriter

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.ArrayList, .Collections, .EnumSet, .LinkedHashMap, .LinkedHashSet, .List, .ListIterator, .Locale, .Map, .Objects, .Set, javax.lang.model.SourceVersion, javax.lang.model.element.Modifier

Converts a JavaFile to a string suitable to both human- and javac-consumption. This honors imports, indentation, and deferred variable names.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Set<String>
private boolean
private final CodeWriter.Multiset<String>
private final Map<String, ClassName>
private final Map<String, ClassName>
private final String
private int
private boolean
private static final String
NO_PACKAGE

Sentinel value that indicates that no user-provided package has been set.

private final LineWrapper
private String
private final Set<String>
pack-priv int
statementLine

When emitting a statement, this is the line of the statement currently being written.

private final Set<String>
private final Set<String>
private boolean
private final List<TypeSpec>

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
CodeWriter(Appendable out, String indent, Set<String> staticImports, Set<String> alwaysQualify)

pack-priv
CodeWriter(Appendable out, String indent, Map<String, ClassName> importedTypes, Set<String> staticImports, Set<String> alwaysQualify)

Method Summary

Modifier and TypeMethod and Description
public CodeWriter
public CodeWriter
emit(String format, Object... args)

public CodeWriter
emit(CodeBlock codeBlock)

public CodeWriter
emit(CodeBlock codeBlock, boolean ensureTrailingNewline)

pack-priv CodeWriter
emitAndIndent(String s)

Emits s with indentation as required.

public void
emitAnnotations(List<AnnotationSpec> annotations, boolean inline)

public void
private void
public void
emitJavadoc(CodeBlock javadocCodeBlock)

private void
public void
emitModifiers(Set<Modifier> modifiers, Set<Modifier> implicitModifiers)

Emits modifiers in the standard order.

public void
private boolean
public void
emitTypeVariables(List<TypeVariableName> typeVariables)

Emit type variables with their bounds.

public CodeWriter
private static String
private void
public Map<String, ClassName>
public CodeWriter
public CodeWriter
indent(int levels)

pack-priv String
lookupName(ClassName className)

Returns the best name to identify className with in the current context.

public CodeWriter
public CodeWriter
public void
public CodeWriter
pushPackage(String packageName)

public CodeWriter
private ClassName
resolve(String simpleName)

Returns the class referenced by simpleName, using the current nesting context and imports.

private ClassName
stackClassName(int stackDepth, String simpleName)

Returns the class named simpleName when nested in the class at stackDepth.

pack-priv Map<String, ClassName>
suggestedImports()

Returns the types that should have been imported for this code.

public CodeWriter
public CodeWriter
unindent(int levels)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alwaysQualifyback to summary
private final Set<String> alwaysQualify
commentback to summary
private boolean comment
currentTypeVariablesback to summary
private final CodeWriter.Multiset<String> currentTypeVariables
importableTypesback to summary
private final Map<String, ClassName> importableTypes
importedTypesback to summary
private final Map<String, ClassName> importedTypes
indentback to summary
private final String indent
indentLevelback to summary
private int indentLevel
javadocback to summary
private boolean javadoc
NO_PACKAGEback to summary
private static final String NO_PACKAGE

Sentinel value that indicates that no user-provided package has been set.

outback to summary
private final LineWrapper out
packageNameback to summary
private String packageName
referencedNamesback to summary
private final Set<String> referencedNames
statementLineback to summary
pack-priv 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.

staticImportClassNamesback to summary
private final Set<String> staticImportClassNames
staticImportsback to summary
private final Set<String> staticImports
trailingNewlineback to summary
private boolean trailingNewline
typeSpecStackback to summary
private final List<TypeSpec> typeSpecStack

Constructor Detail

CodeWriterback to summary
pack-priv CodeWriter(Appendable out)
CodeWriterback to summary
pack-priv CodeWriter(Appendable out, String indent, Set<String> staticImports, Set<String> alwaysQualify)
CodeWriterback to summary
pack-priv CodeWriter(Appendable out, String indent, Map<String, ClassName> importedTypes, Set<String> staticImports, Set<String> alwaysQualify)

Method Detail

emitback to summary
public CodeWriter emit(String s) throws IOException
emitback to summary
public CodeWriter emit(String format, Object... args) throws IOException
emitback to summary
public CodeWriter emit(CodeBlock codeBlock) throws IOException
emitback to summary
public CodeWriter emit(CodeBlock codeBlock, boolean ensureTrailingNewline) throws IOException
emitAndIndentback to summary
pack-priv CodeWriter emitAndIndent(String s) throws IOException

Emits s with indentation as required. It's important that all code that writes to out does it through here, since we emit indentation lazily in order to avoid unnecessary trailing whitespace.

emitAnnotationsback to summary
public void emitAnnotations(List<AnnotationSpec> annotations, boolean inline) throws IOException
emitCommentback to summary
public void emitComment(CodeBlock codeBlock) throws IOException
emitIndentationback to summary
private void emitIndentation() throws IOException
emitJavadocback to summary
public void emitJavadoc(CodeBlock javadocCodeBlock) throws IOException
emitLiteralback to summary
private void emitLiteral(Object o) throws IOException
emitModifiersback to summary
public void emitModifiers(Set<Modifier> modifiers, Set<Modifier> implicitModifiers) throws IOException

Emits modifiers in the standard order. Modifiers in implicitModifiers will not be emitted.

emitModifiersback to summary
public void emitModifiers(Set<Modifier> modifiers) throws IOException
emitStaticImportMemberback to summary
private boolean emitStaticImportMember(String canonical, String part) throws IOException
emitTypeVariablesback to summary
public void emitTypeVariables(List<TypeVariableName> typeVariables) throws IOException

Emit type variables with their bounds. This should only be used when declaring type variables; everywhere else bounds are omitted.

emitWrappingSpaceback to summary
public CodeWriter emitWrappingSpace() throws IOException
extractMemberNameback to summary
private static String extractMemberName(String part)
importableTypeback to summary
private void importableType(ClassName className)
importedTypesback to summary
public Map<String, ClassName> importedTypes()
indentback to summary
public CodeWriter indent()
indentback to summary
public CodeWriter indent(int levels)
lookupNameback to summary
pack-priv String lookupName(ClassName className)

Returns the best name to identify className 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.

popPackageback to summary
public CodeWriter popPackage()
popTypeback to summary
public CodeWriter popType()
popTypeVariablesback to summary
public void popTypeVariables(List<TypeVariableName> typeVariables) throws IOException
pushPackageback to summary
public CodeWriter pushPackage(String packageName)
pushTypeback to summary
public CodeWriter pushType(TypeSpec type)
resolveback to summary
private ClassName resolve(String simpleName)

Returns the class referenced by simpleName, using the current nesting context and imports.

stackClassNameback to summary
private ClassName stackClassName(int stackDepth, String simpleName)

Returns the class named simpleName when nested in the class at stackDepth.

suggestedImportsback to summary
pack-priv Map<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.

unindentback to summary
public CodeWriter unindent()
unindentback to summary
public CodeWriter unindent(int levels)
com.squareup.javapoet back to summary

private final Class CodeWriter.Multiset<T>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Map<T, Integer>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
add(T t)

pack-priv boolean
contains(T t)

pack-priv void
remove(T t)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final Map<T, Integer> map

Constructor Detail

Multisetback to summary
private Multiset()

Method Detail

addback to summary
pack-priv void add(T t)
containsback to summary
pack-priv boolean contains(T t)
removeback to summary
pack-priv void remove(T t)