public class JavaWriter
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Class and Description |
---|---|
private static class |
JavaWriter.Scope |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.String> |
importedTypes
Map fully qualified type names to their short names.
|
private java.lang.String |
indent |
private static java.lang.String |
INDENT |
private boolean |
isCompressingTypes |
private static int |
MAX_SINGLE_LINE_ATTRIBUTES |
private static java.util.EnumSet<JavaWriter.Scope> |
METHOD_SCOPES |
private java.io.Writer |
out |
private java.lang.String |
packagePrefix |
private java.util.Deque<JavaWriter.Scope> |
scopes |
private static java.util.regex.Pattern |
TYPE_PATTERN |
private static java.util.regex.Pattern |
TYPE_TRAILER |
private java.util.Deque<java.lang.String> |
types |
Constructor and Description |
---|
JavaWriter(java.io.Writer out) |
Modifier and Type | Method and Description |
---|---|
JavaWriter |
beginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.util.List<java.lang.String> parameters,
java.util.List<java.lang.String> throwsTypes) |
JavaWriter |
beginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.lang.String... parameters) |
JavaWriter |
beginControlFlow(java.lang.String controlFlow) |
JavaWriter |
beginControlFlow(java.lang.String controlFlow,
java.lang.Object... args) |
JavaWriter |
beginInitializer(boolean isStatic)
Emits an initializer declaration.
|
JavaWriter |
beginMethod(java.lang.String returnType,
java.lang.String name,
java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.util.List<java.lang.String> parameters,
java.util.List<java.lang.String> throwsTypes)
Emit a method declaration.
|
JavaWriter |
beginMethod(java.lang.String returnType,
java.lang.String name,
java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.lang.String... parameters)
Emit a method declaration.
|
JavaWriter |
beginType(java.lang.String type,
java.lang.String kind)
Emits a type declaration.
|
JavaWriter |
beginType(java.lang.String type,
java.lang.String kind,
java.util.Set<javax.lang.model.element.Modifier> modifiers)
Emits a type declaration.
|
JavaWriter |
beginType(java.lang.String type,
java.lang.String kind,
java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.lang.String extendsType,
java.lang.String... implementsTypes)
Emits a type declaration.
|
private void |
checkInMethod() |
void |
close() |
java.lang.String |
compressType(java.lang.String type)
Try to compress a fully-qualified class name to only the class name.
|
private boolean |
containsArray(java.util.Collection<?> values) |
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 . |
private JavaWriter |
emitAnnotationValue(java.lang.Object value)
Writes a single annotation value.
|
private JavaWriter |
emitCompressedType(java.lang.String type)
Emits a name like
java.lang.String or java.util.List<java.lang.String> ,
compressing it with imports if possible. |
JavaWriter |
emitEmptyLine() |
JavaWriter |
emitEnumValue(java.lang.String name) |
JavaWriter |
emitEnumValue(java.lang.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(java.lang.Iterable<java.lang.String> names)
Emit a list of enum values followed by a semi-colon (
; ). |
JavaWriter |
emitField(java.lang.String type,
java.lang.String name)
Emits a field declaration.
|
JavaWriter |
emitField(java.lang.String type,
java.lang.String name,
java.util.Set<javax.lang.model.element.Modifier> modifiers)
Emits a field declaration.
|
JavaWriter |
emitField(java.lang.String type,
java.lang.String name,
java.util.Set<javax.lang.model.element.Modifier> modifiers,
java.lang.String initialValue)
Emits a field declaration.
|
JavaWriter |
emitImports(java.lang.Class<?>... types)
Emit an import for each
type provided. |
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 . |
private JavaWriter |
emitLastEnumValue(java.lang.String name) |
private void |
emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)
Emits the modifiers to the writer.
|
JavaWriter |
emitPackage(java.lang.String packageName)
Emit a package declaration and empty line.
|
JavaWriter |
emitSingleLineComment(java.lang.String comment,
java.lang.Object... args)
Emits a single line comment.
|
JavaWriter |
emitStatement(java.lang.String pattern,
java.lang.Object... args) |
JavaWriter |
emitStaticImports(java.util.Collection<java.lang.String> types)
Emit a static import for each
type in the provided Collection . |
JavaWriter |
emitStaticImports(java.lang.String... types)
Emit a static import for each
type provided. |
JavaWriter |
endConstructor()
Completes the current constructor declaration.
|
JavaWriter |
endControlFlow() |
JavaWriter |
endControlFlow(java.lang.String controlFlow) |
JavaWriter |
endControlFlow(java.lang.String controlFlow,
java.lang.Object... args) |
JavaWriter |
endInitializer()
Ends the current initializer declaration.
|
JavaWriter |
endMethod()
Completes the current method declaration.
|
JavaWriter |
endType()
Completes the current type declaration.
|
java.lang.String |
getIndent() |
private void |
hangingIndent() |
private void |
indent() |
private boolean |
isAmbiguous(java.lang.String compressed)
Returns true if the imports contain a class with same simple name as
compressed . |
private static boolean |
isClassInPackage(java.lang.String name,
java.lang.String packagePrefix) |
boolean |
isCompressingTypes() |
JavaWriter |
nextControlFlow(java.lang.String controlFlow) |
JavaWriter |
nextControlFlow(java.lang.String controlFlow,
java.lang.Object... args) |
private void |
popScope(JavaWriter.Scope... expected) |
static java.lang.String |
rawType(java.lang.String type)
Build a string representation of the raw type for a (optionally generic) type.
|
void |
setCompressingTypes(boolean isCompressingTypes) |
void |
setIndent(java.lang.String indent) |
static java.lang.String |
stringLiteral(java.lang.String data)
Deprecated.
use
StringLiteral and its StringLiteral.literal() method instead. |
static java.lang.String |
type(java.lang.Class<?> raw,
java.lang.String... parameters)
Build a string representation of a type and optionally its generic type arguments.
|
private static final java.util.regex.Pattern TYPE_TRAILER
private static final java.util.regex.Pattern TYPE_PATTERN
private static final int MAX_SINGLE_LINE_ATTRIBUTES
private static final java.lang.String INDENT
private final java.util.Map<java.lang.String,java.lang.String> importedTypes
private java.lang.String packagePrefix
private final java.util.Deque<JavaWriter.Scope> scopes
private final java.util.Deque<java.lang.String> types
private final java.io.Writer out
private boolean isCompressingTypes
private java.lang.String indent
private static final java.util.EnumSet<JavaWriter.Scope> METHOD_SCOPES
public JavaWriter(java.io.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(java.lang.String indent)
public java.lang.String getIndent()
public JavaWriter emitPackage(java.lang.String packageName) throws java.io.IOException
java.io.IOException
public JavaWriter emitImports(java.lang.String... types) throws java.io.IOException
type
provided. For the duration of the file, all references to
these classes will be automatically shortened.java.io.IOException
public JavaWriter emitImports(java.lang.Class<?>... types) throws java.io.IOException
type
provided. For the duration of the file, all references to
these classes will be automatically shortened.java.io.IOException
public JavaWriter emitImports(java.util.Collection<java.lang.String> types) throws java.io.IOException
type
in the provided Collection
. For the duration of
the file, all references to these classes will be automatically shortened.java.io.IOException
public JavaWriter emitStaticImports(java.lang.String... types) throws java.io.IOException
type
provided. For the duration of the file,
all references to these classes will be automatically shortened.java.io.IOException
public JavaWriter emitStaticImports(java.util.Collection<java.lang.String> types) throws java.io.IOException
type
in the provided Collection
. For the
duration of the file, all references to these classes will be automatically shortened.java.io.IOException
private JavaWriter emitCompressedType(java.lang.String type) throws java.io.IOException
java.lang.String
or java.util.List<java.lang.String>
,
compressing it with imports if possible. Type compression will only be enabled if
isCompressingTypes
is true.java.io.IOException
public java.lang.String compressType(java.lang.String type)
private static boolean isClassInPackage(java.lang.String name, java.lang.String packagePrefix)
private boolean isAmbiguous(java.lang.String compressed)
compressed
.compressed
- simple name of the typepublic JavaWriter beginInitializer(boolean isStatic) throws java.io.IOException
isStatic
- true if it should be an static initializer, false for an instance initializer.java.io.IOException
public JavaWriter endInitializer() throws java.io.IOException
java.io.IOException
public JavaWriter beginType(java.lang.String type, java.lang.String kind) throws java.io.IOException
kind
- such as "class", "interface" or "enum".java.io.IOException
public JavaWriter beginType(java.lang.String type, java.lang.String kind, java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
kind
- such as "class", "interface" or "enum".java.io.IOException
public JavaWriter beginType(java.lang.String type, java.lang.String kind, java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String extendsType, java.lang.String... implementsTypes) throws java.io.IOException
kind
- such as "class", "interface" or "enum".extendsType
- the class to extend, or null for no extends clause.java.io.IOException
public JavaWriter endType() throws java.io.IOException
java.io.IOException
public JavaWriter emitField(java.lang.String type, java.lang.String name) throws java.io.IOException
java.io.IOException
public JavaWriter emitField(java.lang.String type, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
java.io.IOException
public JavaWriter emitField(java.lang.String type, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String initialValue) throws java.io.IOException
java.io.IOException
public JavaWriter beginMethod(java.lang.String returnType, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String... parameters) throws java.io.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.java.io.IOException
public JavaWriter beginMethod(java.lang.String returnType, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.List<java.lang.String> parameters, java.util.List<java.lang.String> throwsTypes) throws java.io.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.java.io.IOException
public JavaWriter beginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String... parameters) throws java.io.IOException
java.io.IOException
public JavaWriter beginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.List<java.lang.String> parameters, java.util.List<java.lang.String> throwsTypes) throws java.io.IOException
java.io.IOException
public JavaWriter emitJavadoc(java.lang.String javadoc, java.lang.Object... params) throws java.io.IOException
\n
.java.io.IOException
public JavaWriter emitSingleLineComment(java.lang.String comment, java.lang.Object... args) throws java.io.IOException
java.io.IOException
public JavaWriter emitEmptyLine() throws java.io.IOException
java.io.IOException
public JavaWriter emitEnumValue(java.lang.String name) throws java.io.IOException
java.io.IOException
public JavaWriter emitEnumValue(java.lang.String name, boolean isLast) throws java.io.IOException
;
).java.io.IOException
private JavaWriter emitLastEnumValue(java.lang.String name) throws java.io.IOException
java.io.IOException
public JavaWriter emitEnumValues(java.lang.Iterable<java.lang.String> names) throws java.io.IOException
;
).java.io.IOException
public JavaWriter emitAnnotation(java.lang.String annotation) throws java.io.IOException
annotation(annotation, emptyMap())
.java.io.IOException
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) throws java.io.IOException
annotation(annotationType.getName(), emptyMap())
.java.io.IOException
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object value) throws java.io.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.java.io.IOException
public JavaWriter emitAnnotation(java.lang.String annotation, java.lang.Object value) throws java.io.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.java.io.IOException
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.util.Map<java.lang.String,?> attributes) throws java.io.IOException
annotation(annotationType.getName(), attributes)
.java.io.IOException
public JavaWriter emitAnnotation(java.lang.String annotation, java.util.Map<java.lang.String,?> attributes) throws java.io.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.java.io.IOException
private boolean containsArray(java.util.Collection<?> values)
private JavaWriter emitAnnotationValue(java.lang.Object value) throws java.io.IOException
java.io.IOException
public JavaWriter emitStatement(java.lang.String pattern, java.lang.Object... args) throws java.io.IOException
pattern
- a code pattern like "int i = %s". Newlines will be further indented. Should not
contain trailing semicolon.java.io.IOException
public JavaWriter beginControlFlow(java.lang.String controlFlow) throws java.io.IOException
controlFlow
- the control flow construct and its code, such as "if (foo == 5)". Shouldn't
contain braces or newline characters.java.io.IOException
public JavaWriter beginControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
controlFlow
- the control flow construct and its code, such as "if (foo == 5)". Shouldn't
contain braces or newline characters.java.io.IOException
public JavaWriter nextControlFlow(java.lang.String controlFlow) throws java.io.IOException
controlFlow
- the control flow construct and its code, such as "else if (foo == 10)".
Shouldn't contain braces or newline characters.java.io.IOException
public JavaWriter nextControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
controlFlow
- the control flow construct and its code, such as "else if (foo == 10)".
Shouldn't contain braces or newline characters.java.io.IOException
public JavaWriter endControlFlow() throws java.io.IOException
java.io.IOException
public JavaWriter endControlFlow(java.lang.String controlFlow) throws java.io.IOException
controlFlow
- the optional control flow construct and its code, such as
"while(foo == 20)". Only used for "do/while" control flows.java.io.IOException
public JavaWriter endControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
controlFlow
- the optional control flow construct and its code, such as
"while(foo == 20)". Only used for "do/while" control flows.java.io.IOException
public JavaWriter endMethod() throws java.io.IOException
java.io.IOException
public JavaWriter endConstructor() throws java.io.IOException
java.io.IOException
@Deprecated public static java.lang.String stringLiteral(java.lang.String data)
StringLiteral
and its StringLiteral.literal()
method instead.data
, including wrapping quotes.public static java.lang.String type(java.lang.Class<?> raw, java.lang.String... parameters)
public static java.lang.String rawType(java.lang.String type)
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
private void emitModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
java.io.IOException
private void indent() throws java.io.IOException
java.io.IOException
private void hangingIndent() throws java.io.IOException
java.io.IOException
private void checkInMethod()
private void popScope(JavaWriter.Scope... expected)