Class JavaWriter

java.lang.Object
com.squareup.javawriter.JavaWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public class JavaWriter extends Object implements Closeable
A utility class which aids in generating Java source files.
  • Constructor Details

    • JavaWriter

      public JavaWriter(Writer out)
      Parameters:
      out - the stream to which Java source will be written. This should be a buffered stream.
  • Method Details

    • setCompressingTypes

      public void setCompressingTypes(boolean isCompressingTypes)
    • isCompressingTypes

      public boolean isCompressingTypes()
    • setIndent

      public void setIndent(String indent)
    • getIndent

      public String getIndent()
    • emitPackage

      public JavaWriter emitPackage(String packageName) throws IOException
      Emit a package declaration and empty line.
      Throws:
      IOException
    • emitImports

      public JavaWriter emitImports(String... types) throws IOException
      Emit an import for each type provided. For the duration of the file, all references to these classes will be automatically shortened.
      Throws:
      IOException
    • emitImports

      public JavaWriter emitImports(Class<?>... types) throws IOException
      Emit an import for each type provided. For the duration of the file, all references to these classes will be automatically shortened.
      Throws:
      IOException
    • emitImports

      public JavaWriter emitImports(Collection<String> types) throws IOException
      Emit an import for each type in the provided Collection. For the duration of the file, all references to these classes will be automatically shortened.
      Throws:
      IOException
    • emitStaticImports

      public JavaWriter emitStaticImports(String... types) throws IOException
      Emit a static import for each type provided. For the duration of the file, all references to these classes will be automatically shortened.
      Throws:
      IOException
    • emitStaticImports

      public JavaWriter emitStaticImports(Collection<String> types) throws IOException
      Emit a static import for each type in the provided Collection. For the duration of the file, all references to these classes will be automatically shortened.
      Throws:
      IOException
    • compressType

      public String compressType(String type)
      Try to compress a fully-qualified class name to only the class name.
    • beginInitializer

      public JavaWriter beginInitializer(boolean isStatic) throws IOException
      Emits an initializer declaration.
      Parameters:
      isStatic - true if it should be an static initializer, false for an instance initializer.
      Throws:
      IOException
    • endInitializer

      public JavaWriter endInitializer() throws IOException
      Ends the current initializer declaration.
      Throws:
      IOException
    • beginType

      public JavaWriter beginType(String type, String kind) throws IOException
      Emits a type declaration.
      Parameters:
      kind - such as "class", "interface" or "enum".
      Throws:
      IOException
    • beginType

      public JavaWriter beginType(String type, String kind, Set<Modifier> modifiers) throws IOException
      Emits a type declaration.
      Parameters:
      kind - such as "class", "interface" or "enum".
      Throws:
      IOException
    • beginType

      public JavaWriter beginType(String type, String kind, Set<Modifier> modifiers, String extendsType, String... implementsTypes) throws IOException
      Emits a type declaration.
      Parameters:
      kind - such as "class", "interface" or "enum".
      extendsType - the class to extend, or null for no extends clause.
      Throws:
      IOException
    • endType

      public JavaWriter endType() throws IOException
      Completes the current type declaration.
      Throws:
      IOException
    • emitField

      public JavaWriter emitField(String type, String name) throws IOException
      Emits a field declaration.
      Throws:
      IOException
    • emitField

      public JavaWriter emitField(String type, String name, Set<Modifier> modifiers) throws IOException
      Emits a field declaration.
      Throws:
      IOException
    • emitField

      public JavaWriter emitField(String type, String name, Set<Modifier> modifiers, String initialValue) throws IOException
      Emits a field declaration.
      Throws:
      IOException
    • beginMethod

      public JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, String... parameters) throws IOException
      Emit a method declaration.

      A null return type may be used to indicate a constructor, but beginConstructor(Set, String...) should be preferred. This behavior may be removed in a future release.

      Parameters:
      returnType - the method's return type, or null for constructors
      name - the method name, or the fully qualified class name for constructors.
      modifiers - the set of modifiers to be applied to the method
      parameters - alternating parameter types and names.
      Throws:
      IOException
    • beginMethod

      public JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
      Emit a method declaration.

      A null return type may be used to indicate a constructor, but beginConstructor(Set, List, List) should be preferred. This behavior may be removed in a future release.

      Parameters:
      returnType - the method's return type, or null for constructors.
      name - the method name, or the fully qualified class name for constructors.
      modifiers - the set of modifiers to be applied to the method
      parameters - alternating parameter types and names.
      throwsTypes - the classes to throw, or null for no throws clause.
      Throws:
      IOException
    • beginConstructor

      public JavaWriter beginConstructor(Set<Modifier> modifiers, String... parameters) throws IOException
      Throws:
      IOException
    • beginConstructor

      public JavaWriter beginConstructor(Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
      Throws:
      IOException
    • emitJavadoc

      public JavaWriter emitJavadoc(String javadoc, Object... params) throws IOException
      Emits some Javadoc comments with line separated by \n.
      Throws:
      IOException
    • emitSingleLineComment

      public JavaWriter emitSingleLineComment(String comment, Object... args) throws IOException
      Emits a single line comment.
      Throws:
      IOException
    • emitEmptyLine

      public JavaWriter emitEmptyLine() throws IOException
      Throws:
      IOException
    • emitEnumValue

      public JavaWriter emitEnumValue(String name) throws IOException
      Throws:
      IOException
    • emitEnumValue

      public JavaWriter emitEnumValue(String name, boolean isLast) throws IOException
      A simple switch to emit the proper enum depending if its last causing it to be terminated by a semi-colon (;).
      Throws:
      IOException
    • emitEnumValues

      public JavaWriter emitEnumValues(Iterable<String> names) throws IOException
      Emit a list of enum values followed by a semi-colon (;).
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(String annotation) throws IOException
      Equivalent to annotation(annotation, emptyMap()).
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(Class<? extends Annotation> annotationType) throws IOException
      Equivalent to annotation(annotationType.getName(), emptyMap()).
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(Class<? extends Annotation> annotationType, Object value) throws IOException
      Annotates the next element with annotationType and a value.
      Parameters:
      value - an object used as the default (value) parameter of the annotation. The value will be encoded using Object.toString(); use stringLiteral(java.lang.String) for String values. Object arrays are written one element per line.
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(String annotation, Object value) throws IOException
      Annotates the next element with annotation and a value.
      Parameters:
      value - an object used as the default (value) parameter of the annotation. The value will be encoded using Object.toString(); use stringLiteral(java.lang.String) for String values. Object arrays are written one element per line.
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(Class<? extends Annotation> annotationType, Map<String,?> attributes) throws IOException
      Equivalent to annotation(annotationType.getName(), attributes).
      Throws:
      IOException
    • emitAnnotation

      public JavaWriter emitAnnotation(String annotation, Map<String,?> attributes) throws IOException
      Annotates the next element with annotation and attributes.
      Parameters:
      attributes - a map from annotation attribute names to their values. Values are encoded using Object.toString(); use stringLiteral(java.lang.String) for String values. Object arrays are written one element per line.
      Throws:
      IOException
    • emitStatement

      public JavaWriter emitStatement(String pattern, Object... args) throws IOException
      Parameters:
      pattern - a code pattern like "int i = %s". Newlines will be further indented. Should not contain trailing semicolon.
      Throws:
      IOException
    • beginControlFlow

      public JavaWriter beginControlFlow(String controlFlow) throws IOException
      Parameters:
      controlFlow - the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.
      Throws:
      IOException
    • beginControlFlow

      public JavaWriter beginControlFlow(String controlFlow, Object... args) throws IOException
      Parameters:
      controlFlow - the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.
      Throws:
      IOException
    • nextControlFlow

      public JavaWriter nextControlFlow(String controlFlow) throws IOException
      Parameters:
      controlFlow - the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.
      Throws:
      IOException
    • nextControlFlow

      public JavaWriter nextControlFlow(String controlFlow, Object... args) throws IOException
      Parameters:
      controlFlow - the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.
      Throws:
      IOException
    • endControlFlow

      public JavaWriter endControlFlow() throws IOException
      Throws:
      IOException
    • endControlFlow

      public JavaWriter endControlFlow(String controlFlow) throws IOException
      Parameters:
      controlFlow - the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.
      Throws:
      IOException
    • endControlFlow

      public JavaWriter endControlFlow(String controlFlow, Object... args) throws IOException
      Parameters:
      controlFlow - the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.
      Throws:
      IOException
    • endMethod

      public JavaWriter endMethod() throws IOException
      Completes the current method declaration.
      Throws:
      IOException
    • endConstructor

      public JavaWriter endConstructor() throws IOException
      Completes the current constructor declaration.
      Throws:
      IOException
    • stringLiteral

      @Deprecated public static String stringLiteral(String data)
      Deprecated.
      use StringLiteral and its StringLiteral.literal() method instead.
      Returns the string literal representing data, including wrapping quotes.
    • type

      public static String type(Class<?> raw, String... parameters)
      Build a string representation of a type and optionally its generic type arguments.
    • rawType

      public static String rawType(String type)
      Build a string representation of the raw type for a (optionally generic) type.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException