public class JavaWriter extends Object implements Closeable
| Constructor and Description |
|---|
JavaWriter(Writer out) |
| Modifier and Type | Method and Description |
|---|---|
JavaWriter |
beginConstructor(Set<Modifier> modifiers,
List<String> parameters,
List<String> throwsTypes) |
JavaWriter |
beginConstructor(Set<Modifier> modifiers,
String... parameters) |
JavaWriter |
beginControlFlow(String controlFlow) |
JavaWriter |
beginControlFlow(String controlFlow,
Object... args) |
JavaWriter |
beginInitializer(boolean isStatic)
Emits an initializer declaration.
|
JavaWriter |
beginMethod(String returnType,
String name,
Set<Modifier> modifiers,
List<String> parameters,
List<String> throwsTypes)
Emit a method declaration.
|
JavaWriter |
beginMethod(String returnType,
String name,
Set<Modifier> modifiers,
String... parameters)
Emit a method declaration.
|
JavaWriter |
beginType(String type,
String kind)
Emits a type declaration.
|
JavaWriter |
beginType(String type,
String kind,
Set<Modifier> modifiers)
Emits a type declaration.
|
JavaWriter |
beginType(String type,
String kind,
Set<Modifier> 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 |
emitEnumValue(String name,
boolean isLast)
A simple switch to emit the proper enum depending if its last causing it to be terminated
by a semi-colon (
;). |
JavaWriter |
emitEnumValues(Iterable<String> names)
Emit a list of enum values followed by a semi-colon (
;). |
JavaWriter |
emitField(String type,
String name)
Emits a field declaration.
|
JavaWriter |
emitField(String type,
String name,
Set<Modifier> modifiers)
Emits a field declaration.
|
JavaWriter |
emitField(String type,
String name,
Set<Modifier> modifiers,
String initialValue)
Emits a field declaration.
|
JavaWriter |
emitImports(Class<?>... types)
Emit an import for each
type provided. |
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 |
endConstructor()
Completes the current constructor declaration.
|
JavaWriter |
endControlFlow() |
JavaWriter |
endControlFlow(String controlFlow) |
JavaWriter |
endControlFlow(String controlFlow,
Object... args) |
JavaWriter |
endInitializer()
Ends the current initializer declaration.
|
JavaWriter |
endMethod()
Completes the current method declaration.
|
JavaWriter |
endType()
Completes the current type declaration.
|
String |
getIndent() |
boolean |
isCompressingTypes() |
JavaWriter |
nextControlFlow(String controlFlow) |
JavaWriter |
nextControlFlow(String controlFlow,
Object... args) |
static String |
rawType(String type)
Build a string representation of the raw type for a (optionally generic) type.
|
void |
setCompressingTypes(boolean isCompressingTypes) |
void |
setIndent(String indent) |
static String |
stringLiteral(String data)
Deprecated.
use
StringLiteral and its StringLiteral.literal() method instead. |
static String |
type(Class<?> raw,
String... parameters)
Build a string representation of a type and optionally its generic type arguments.
|
public JavaWriter(Writer out)
out - the stream to which Java source will be written. This should be a buffered stream.public void setCompressingTypes(boolean isCompressingTypes)
public boolean isCompressingTypes()
public void setIndent(String indent)
public String getIndent()
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(Class<?>... 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 emitStaticImports(String... types) throws IOException
type provided. For the duration of the file,
all references to these classes will be automatically shortened.IOExceptionpublic JavaWriter emitStaticImports(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 String compressType(String type)
public JavaWriter beginInitializer(boolean isStatic) throws IOException
isStatic - true if it should be an static initializer, false for an instance initializer.IOExceptionpublic JavaWriter endInitializer() throws IOException
IOExceptionpublic JavaWriter beginType(String type, String kind) throws IOException
kind - such as "class", "interface" or "enum".IOExceptionpublic JavaWriter beginType(String type, String kind, Set<Modifier> modifiers) throws IOException
kind - such as "class", "interface" or "enum".IOExceptionpublic JavaWriter beginType(String type, String kind, Set<Modifier> 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) throws IOException
IOExceptionpublic JavaWriter emitField(String type, String name, Set<Modifier> modifiers) throws IOException
IOExceptionpublic JavaWriter emitField(String type, String name, Set<Modifier> modifiers, String initialValue) throws IOException
IOExceptionpublic JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, String... parameters) throws IOException
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.
returnType - the method's return type, or null for constructorsname - the method name, or the fully qualified class name for constructors.modifiers - the set of modifiers to be applied to the methodparameters - alternating parameter types and names.IOExceptionpublic JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
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.
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 methodparameters - alternating parameter types and names.throwsTypes - the classes to throw, or null for no throws clause.IOExceptionpublic JavaWriter beginConstructor(Set<Modifier> modifiers, String... parameters) throws IOException
IOExceptionpublic JavaWriter beginConstructor(Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
IOExceptionpublic JavaWriter emitJavadoc(String javadoc, Object... params) throws IOException
\n.IOExceptionpublic JavaWriter emitSingleLineComment(String comment, Object... args) throws IOException
IOExceptionpublic JavaWriter emitEmptyLine() throws IOException
IOExceptionpublic JavaWriter emitEnumValue(String name) throws IOException
IOExceptionpublic JavaWriter emitEnumValue(String name, boolean isLast) throws IOException
;).IOExceptionpublic JavaWriter emitEnumValues(Iterable<String> names) 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 beginControlFlow(String controlFlow, Object... args) 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 nextControlFlow(String controlFlow, Object... args) 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 endControlFlow(String controlFlow, Object... args) 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 JavaWriter endConstructor() throws IOException
IOException@Deprecated public static String stringLiteral(String data)
StringLiteral and its StringLiteral.literal() method instead.data, including wrapping quotes.public static String type(Class<?> raw, String... parameters)
public static String rawType(String type)
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2014 Square, Inc.. All Rights Reserved.