public final class JavaWriter extends Object implements Closeable
| Constructor and Description |
|---|
JavaWriter(Writer out) |
| Modifier and Type | Method and Description |
|---|---|
JavaWriter |
beginControlFlow(String controlFlow) |
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() |
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 |
emitEndOfLineComment(String comment,
Object... args)
Emits some Javadoc comments.
|
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 |
emitStatement(String pattern,
Object... args) |
JavaWriter |
endControlFlow() |
JavaWriter |
endControlFlow(String controlFlow) |
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. |
public JavaWriter(Writer out)
out - the stream to which Java source will be written. This should be a buffered stream.public JavaWriter emitPackage(String packageName) throws IOException
IOExceptionpublic JavaWriter emitImports(String... types) throws IOException
type provided. For the duration of the file, all references to
these classes will be automatically shortened.IOExceptionpublic JavaWriter emitImports(Collection<String> types) throws IOException
type in the provided Collection. For the duration of
the file, all references to these classes will be automatically shortened.IOExceptionpublic JavaWriter beginType(String type, String kind, int modifiers) throws IOException
kind - such as "class", "interface" or "enum".IOExceptionpublic JavaWriter beginType(String type, String kind, int modifiers, String extendsType, String... implementsTypes) throws IOException
kind - such as "class", "interface" or "enum".extendsType - the class to extend, or null for no extends clause.IOExceptionpublic JavaWriter endType() throws IOException
IOExceptionpublic JavaWriter emitField(String type, String name, int modifiers) throws IOException
IOExceptionpublic JavaWriter emitField(String type, String name, int modifiers, String initialValue) throws IOException
IOExceptionpublic JavaWriter beginMethod(String returnType, String name, int modifiers, String... parameters) throws IOException
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.IOExceptionpublic JavaWriter emitJavadoc(String javadoc, Object... params) throws IOException
\n.IOExceptionpublic JavaWriter emitEndOfLineComment(String comment, Object... args) throws IOException
IOExceptionpublic JavaWriter emitEmptyLine() throws IOException
IOExceptionpublic JavaWriter emitEnumValue(String name) throws IOException
IOExceptionpublic JavaWriter emitAnnotation(String annotation) throws IOException
annotation(annotation, emptyMap()).IOExceptionpublic JavaWriter emitAnnotation(Class<? extends Annotation> annotationType) throws IOException
annotation(annotationType.getName(), emptyMap()).IOExceptionpublic JavaWriter emitAnnotation(Class<? extends Annotation> annotationType, Object value) throws IOException
annotationType and a value.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.IOExceptionpublic JavaWriter emitAnnotation(String annotation, Object value) throws IOException
annotation and a value.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.IOExceptionpublic JavaWriter emitAnnotation(Class<? extends Annotation> annotationType, Map<String,?> attributes) throws IOException
annotation(annotationType.getName(), attributes).IOExceptionpublic JavaWriter emitAnnotation(String annotation, Map<String,?> attributes) throws IOException
annotation and attributes.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.IOExceptionpublic JavaWriter emitStatement(String pattern, Object... args) throws IOException
pattern - a code pattern like "int i = %s". Newlines will be further indented. Should not
contain trailing semicolon.IOExceptionpublic JavaWriter beginControlFlow(String controlFlow) throws IOException
controlFlow - the control flow construct and its code, such as "if (foo == 5)". Shouldn't
contain braces or newline characters.IOExceptionpublic JavaWriter nextControlFlow(String controlFlow) throws IOException
controlFlow - the control flow construct and its code, such as "else if (foo == 10)".
Shouldn't contain braces or newline characters.IOExceptionpublic JavaWriter endControlFlow() throws IOException
IOExceptionpublic JavaWriter endControlFlow(String controlFlow) throws IOException
controlFlow - the optional control flow construct and its code, such as
"while(foo == 20)". Only used for "do/while" control flows.IOExceptionpublic JavaWriter endMethod() throws IOException
IOExceptionpublic static String stringLiteral(String data)
data, including wrapping quotes.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2013 Square, Inc.. All Rights Reserved.