com.squareup.java
Class JavaWriter

java.lang.Object
  extended by com.squareup.java.JavaWriter
All Implemented Interfaces:
java.io.Closeable

public final class JavaWriter
extends java.lang.Object
implements java.io.Closeable

A utility class which aids in generating Java source files.


Constructor Summary
JavaWriter(java.io.Writer out)
           
 
Method Summary
 JavaWriter beginControlFlow(java.lang.String controlFlow)
           
 JavaWriter beginMethod(java.lang.String returnType, java.lang.String name, int modifiers, java.lang.String... parameters)
          Emit a method declaration.
 JavaWriter beginType(java.lang.String type, java.lang.String kind, int modifiers)
          Emits a type declaration.
 JavaWriter beginType(java.lang.String type, java.lang.String kind, int modifiers, java.lang.String extendsType, java.lang.String... implementsTypes)
          Emits a type declaration.
 void close()
           
 java.lang.String compressType(java.lang.String type)
          Try to compress a fully-qualified class name to only the class name.
 JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
          Equivalent to annotation(annotationType.getName(), emptyMap()).
 JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.util.Map<java.lang.String,?> attributes)
          Equivalent to annotation(annotationType.getName(), attributes).
 JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object value)
          Annotates the next element with annotationType and a value.
 JavaWriter emitAnnotation(java.lang.String annotation)
          Equivalent to annotation(annotation, emptyMap()).
 JavaWriter emitAnnotation(java.lang.String annotation, java.util.Map<java.lang.String,?> attributes)
          Annotates the next element with annotation and attributes.
 JavaWriter emitAnnotation(java.lang.String annotation, java.lang.Object value)
          Annotates the next element with annotation and a value.
 JavaWriter emitEmptyLine()
           
 JavaWriter emitEndOfLineComment(java.lang.String comment, java.lang.Object... args)
          Emits some Javadoc comments.
 JavaWriter emitEnumValue(java.lang.String name)
           
 JavaWriter emitField(java.lang.String type, java.lang.String name, int modifiers)
          Emits a field declaration.
 JavaWriter emitField(java.lang.String type, java.lang.String name, int modifiers, java.lang.String initialValue)
           
 JavaWriter emitImports(java.util.Collection<java.lang.String> types)
          Emit an import for each type in the provided Collection.
 JavaWriter emitImports(java.lang.String... types)
          Emit an import for each type provided.
 JavaWriter emitJavadoc(java.lang.String javadoc, java.lang.Object... params)
          Emits some Javadoc comments with line separated by \n.
 JavaWriter emitPackage(java.lang.String packageName)
          Emit a package declaration and empty line.
 JavaWriter emitStatement(java.lang.String pattern, java.lang.Object... args)
           
 JavaWriter endControlFlow()
           
 JavaWriter endControlFlow(java.lang.String controlFlow)
           
 JavaWriter endMethod()
          Completes the current method declaration.
 JavaWriter endType()
          Completes the current type declaration.
 JavaWriter nextControlFlow(java.lang.String controlFlow)
           
static java.lang.String stringLiteral(java.lang.String data)
          Returns the string literal representing data, including wrapping quotes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaWriter

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

emitPackage

public JavaWriter emitPackage(java.lang.String packageName)
                       throws java.io.IOException
Emit a package declaration and empty line.

Throws:
java.io.IOException

emitImports

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

Throws:
java.io.IOException

emitImports

public JavaWriter emitImports(java.util.Collection<java.lang.String> types)
                       throws java.io.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:
java.io.IOException

compressType

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


beginType

public JavaWriter beginType(java.lang.String type,
                            java.lang.String kind,
                            int modifiers)
                     throws java.io.IOException
Emits a type declaration.

Parameters:
kind - such as "class", "interface" or "enum".
Throws:
java.io.IOException

beginType

public JavaWriter beginType(java.lang.String type,
                            java.lang.String kind,
                            int modifiers,
                            java.lang.String extendsType,
                            java.lang.String... implementsTypes)
                     throws java.io.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:
java.io.IOException

endType

public JavaWriter endType()
                   throws java.io.IOException
Completes the current type declaration.

Throws:
java.io.IOException

emitField

public JavaWriter emitField(java.lang.String type,
                            java.lang.String name,
                            int modifiers)
                     throws java.io.IOException
Emits a field declaration.

Throws:
java.io.IOException

emitField

public JavaWriter emitField(java.lang.String type,
                            java.lang.String name,
                            int modifiers,
                            java.lang.String initialValue)
                     throws java.io.IOException
Throws:
java.io.IOException

beginMethod

public JavaWriter beginMethod(java.lang.String returnType,
                              java.lang.String name,
                              int modifiers,
                              java.lang.String... parameters)
                       throws java.io.IOException
Emit a method declaration.

Parameters:
returnType - the method's return type, or null for constructors.
name - the method name, or the fully qualified class name for constructors.
parameters - alternating parameter types and names.
Throws:
java.io.IOException

emitJavadoc

public JavaWriter emitJavadoc(java.lang.String javadoc,
                              java.lang.Object... params)
                       throws java.io.IOException
Emits some Javadoc comments with line separated by \n.

Throws:
java.io.IOException

emitEndOfLineComment

public JavaWriter emitEndOfLineComment(java.lang.String comment,
                                       java.lang.Object... args)
                                throws java.io.IOException
Emits some Javadoc comments.

Throws:
java.io.IOException

emitEmptyLine

public JavaWriter emitEmptyLine()
                         throws java.io.IOException
Throws:
java.io.IOException

emitEnumValue

public JavaWriter emitEnumValue(java.lang.String name)
                         throws java.io.IOException
Throws:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.String annotation)
                          throws java.io.IOException
Equivalent to annotation(annotation, emptyMap()).

Throws:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
                          throws java.io.IOException
Equivalent to annotation(annotationType.getName(), emptyMap()).

Throws:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                 java.lang.Object value)
                          throws java.io.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:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.String annotation,
                                 java.lang.Object value)
                          throws java.io.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:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                 java.util.Map<java.lang.String,?> attributes)
                          throws java.io.IOException
Equivalent to annotation(annotationType.getName(), attributes).

Throws:
java.io.IOException

emitAnnotation

public JavaWriter emitAnnotation(java.lang.String annotation,
                                 java.util.Map<java.lang.String,?> attributes)
                          throws java.io.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:
java.io.IOException

emitStatement

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

beginControlFlow

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

nextControlFlow

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

endControlFlow

public JavaWriter endControlFlow()
                          throws java.io.IOException
Throws:
java.io.IOException

endControlFlow

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

endMethod

public JavaWriter endMethod()
                     throws java.io.IOException
Completes the current method declaration.

Throws:
java.io.IOException

stringLiteral

public static java.lang.String stringLiteral(java.lang.String data)
Returns the string literal representing data, including wrapping quotes.


close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException


Copyright © 2013 Square, Inc.. All Rights Reserved.