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 |
beginInitializer(boolean isStatic)
Emits an initializer declaration.
|
JavaWriter |
beginMethod(String returnType,
String name,
int modifiers,
List<String> parameters,
List<String> throwsTypes)
Deprecated.
|
JavaWriter |
beginMethod(String returnType,
String name,
int modifiers,
String... parameters)
Deprecated.
|
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,
int modifiers)
Deprecated.
|
JavaWriter |
beginType(String type,
String kind,
int modifiers,
String extendsType,
String... implementsTypes)
Deprecated.
|
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 |
emitField(String type,
String name)
Emits a field declaration.
|
JavaWriter |
emitField(String type,
String name,
int modifiers)
Deprecated.
|
JavaWriter |
emitField(String type,
String name,
int modifiers,
String initialValue)
Deprecated.
|
JavaWriter |
emitField(String type,
String name,
Set<Modifier> modifiers)
Emits a field declaration.
|
JavaWriter |
emitField(String type,
String name,
Set<Modifier> 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.
|
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 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".IOException@Deprecated public JavaWriter beginType(String type, String kind, int modifiers) throws IOException
beginType(String, String, Set)IOExceptionpublic JavaWriter beginType(String type, String kind, Set<Modifier> modifiers) throws IOException
kind - such as "class", "interface" or "enum".IOException@Deprecated public JavaWriter beginType(String type, String kind, int modifiers, String extendsType, String... implementsTypes) throws IOException
beginType(String, String, Set, String, String...)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
IOException@Deprecated public JavaWriter emitField(String type, String name, int modifiers) throws IOException
emitField(String, String, Set).IOExceptionpublic JavaWriter emitField(String type, String name, Set<Modifier> modifiers) throws IOException
IOException@Deprecated public JavaWriter emitField(String type, String name, int modifiers, String initialValue) throws IOException
emitField(String, String, Set, String).IOExceptionpublic JavaWriter emitField(String type, String name, Set<Modifier> modifiers, String initialValue) throws IOException
IOException@Deprecated public JavaWriter beginMethod(String returnType, String name, int modifiers, String... parameters) throws IOException
beginMethod(String, String, Set, String...).IOExceptionpublic JavaWriter beginMethod(String returnType, String name, Set<Modifier> 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.modifiers - the set of modifiers to be applied to the methodparameters - alternating parameter types and names.IOException@Deprecated public JavaWriter beginMethod(String returnType, String name, int modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
beginMethod(String, String, Set, List, List).IOExceptionpublic JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException
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 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 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 static String type(Class<?> raw, String... parameters)
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2013 Square, Inc.. All Rights Reserved.