Class CodeWriter


  • final class CodeWriter
    extends java.lang.Object
    Converts a JavaFile to a string suitable to both human- and javac-consumption. This honors imports, indentation, and deferred variable names.
    • 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 java.lang.Appendable 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
      • 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
      • 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)
      • 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)
    • Method Detail

      • importedTypes

        public java.util.Map<java.lang.String,​ClassName> importedTypes()
      • indent

        public CodeWriter indent​(int levels)
      • unindent

        public CodeWriter unindent​(int levels)
      • pushPackage

        public CodeWriter pushPackage​(java.lang.String packageName)
      • 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
        Emits modifiers in the standard order. Modifiers in implicitModifiers 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
      • 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
      • 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 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.
      • importableType

        private void importableType​(ClassName className)
      • resolve

        private ClassName resolve​(java.lang.String simpleName)
        Returns the class referenced by simpleName, using the current nesting context and imports.
      • stackClassName

        private ClassName stackClassName​(int stackDepth,
                                         java.lang.String simpleName)
        Returns the class named simpleName when nested in the class at stackDepth.
      • emitAndIndent

        CodeWriter emitAndIndent​(java.lang.String s)
                          throws java.io.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.
        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.