com.squareup.javawriter
Class JavaWriter

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

public final class JavaWriter
extends Object
implements Closeable

A utility class which aids in generating Java source files.


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

Constructor Detail

JavaWriter

public JavaWriter(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(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(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,
                            int 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,
                            int 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,
                            int modifiers)
                     throws IOException
Emits a field declaration.

Throws:
IOException

emitField

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

beginMethod

public JavaWriter beginMethod(String returnType,
                              String name,
                              int modifiers,
                              String... parameters)
                       throws 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:
IOException

beginMethod

public JavaWriter beginMethod(String returnType,
                              String name,
                              int modifiers,
                              List<String> parameters,
                              List<String> throwsTypes)
                       throws 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.
throwsTypes - the classes to throw, or null for no throws clause.
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

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

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

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

endMethod

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

Throws:
IOException

stringLiteral

public static String stringLiteral(String data)
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.


close

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


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