Package com.squareup.javawriter
Class JavaWriter
java.lang.Object
com.squareup.javawriter.JavaWriter
- All Implemented Interfaces:
Closeable
,AutoCloseable
A utility class which aids in generating Java source files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbeginConstructor
(Set<Modifier> modifiers, String... parameters) beginControlFlow
(String controlFlow) beginControlFlow
(String controlFlow, Object... args) beginInitializer
(boolean isStatic) Emits an initializer declaration.Emit a method declaration.beginMethod
(String returnType, String name, Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) Emit a method declaration.Emits a type declaration.Emits a type declaration.beginType
(String type, String kind, Set<Modifier> modifiers, String extendsType, String... implementsTypes) Emits a type declaration.void
close()
compressType
(String type) Try to compress a fully-qualified class name to only the class name.emitAnnotation
(Class<? extends Annotation> annotationType) Equivalent toannotation(annotationType.getName(), emptyMap())
.emitAnnotation
(Class<? extends Annotation> annotationType, Object value) Annotates the next element withannotationType
and avalue
.emitAnnotation
(Class<? extends Annotation> annotationType, Map<String, ?> attributes) Equivalent toannotation(annotationType.getName(), attributes)
.emitAnnotation
(String annotation) Equivalent toannotation(annotation, emptyMap())
.emitAnnotation
(String annotation, Object value) Annotates the next element withannotation
and avalue
.emitAnnotation
(String annotation, Map<String, ?> attributes) Annotates the next element withannotation
andattributes
.emitEnumValue
(String name) 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 (;
).emitEnumValues
(Iterable<String> names) Emit a list of enum values followed by a semi-colon (;
).Emits a field declaration.Emits a field declaration.Emits a field declaration.emitImports
(Class<?>... types) Emit an import for eachtype
provided.emitImports
(String... types) Emit an import for eachtype
provided.emitImports
(Collection<String> types) Emit an import for eachtype
in the providedCollection
.emitJavadoc
(String javadoc, Object... params) Emits some Javadoc comments with line separated by\n
.emitPackage
(String packageName) Emit a package declaration and empty line.emitSingleLineComment
(String comment, Object... args) Emits a single line comment.emitStatement
(String pattern, Object... args) emitStaticImports
(String... types) Emit a static import for eachtype
provided.emitStaticImports
(Collection<String> types) Emit a static import for eachtype
in the providedCollection
.Completes the current constructor declaration.endControlFlow
(String controlFlow) endControlFlow
(String controlFlow, Object... args) Ends the current initializer declaration.Completes the current method declaration.endType()
Completes the current type declaration.boolean
nextControlFlow
(String controlFlow) nextControlFlow
(String controlFlow, Object... args) static String
Build a string representation of the raw type for a (optionally generic) type.void
setCompressingTypes
(boolean isCompressingTypes) void
static String
stringLiteral
(String data) Deprecated.static String
Build a string representation of a type and optionally its generic type arguments.
-
Constructor Details
-
JavaWriter
- Parameters:
out
- the stream to which Java source will be written. This should be a buffered stream.
-
-
Method Details
-
setCompressingTypes
public void setCompressingTypes(boolean isCompressingTypes) -
isCompressingTypes
public boolean isCompressingTypes() -
setIndent
-
getIndent
-
emitPackage
Emit a package declaration and empty line.- Throws:
IOException
-
emitImports
Emit an import for eachtype
provided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
IOException
-
emitImports
Emit an import for eachtype
provided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
IOException
-
emitImports
Emit an import for eachtype
in the providedCollection
. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
IOException
-
emitStaticImports
Emit a static import for eachtype
provided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
IOException
-
emitStaticImports
Emit a static import for eachtype
in the providedCollection
. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
IOException
-
compressType
Try to compress a fully-qualified class name to only the class name. -
beginInitializer
Emits an initializer declaration.- Parameters:
isStatic
- true if it should be an static initializer, false for an instance initializer.- Throws:
IOException
-
endInitializer
Ends the current initializer declaration.- Throws:
IOException
-
beginType
Emits a type declaration.- Parameters:
kind
- such as "class", "interface" or "enum".- Throws:
IOException
-
beginType
Emits a type declaration.- Parameters:
kind
- such as "class", "interface" or "enum".- Throws:
IOException
-
beginType
public JavaWriter beginType(String type, String kind, Set<Modifier> 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
Completes the current type declaration.- Throws:
IOException
-
emitField
Emits a field declaration.- Throws:
IOException
-
emitField
Emits a field declaration.- Throws:
IOException
-
emitField
public JavaWriter emitField(String type, String name, Set<Modifier> modifiers, String initialValue) throws IOException Emits a field declaration.- Throws:
IOException
-
beginMethod
public JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, String... parameters) throws IOException Emit a method declaration.A
null
return type may be used to indicate a constructor, butbeginConstructor(Set, String...)
should be preferred. This behavior may be removed in a future release.- Parameters:
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.- Throws:
IOException
-
beginMethod
public JavaWriter beginMethod(String returnType, String name, Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException Emit a method declaration.A
null
return type may be used to indicate a constructor, butbeginConstructor(Set, List, List)
should be preferred. This behavior may be removed in a future release.- Parameters:
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.- Throws:
IOException
-
beginConstructor
public JavaWriter beginConstructor(Set<Modifier> modifiers, String... parameters) throws IOException - Throws:
IOException
-
beginConstructor
public JavaWriter beginConstructor(Set<Modifier> modifiers, List<String> parameters, List<String> throwsTypes) throws IOException - Throws:
IOException
-
emitJavadoc
Emits some Javadoc comments with line separated by\n
.- Throws:
IOException
-
emitSingleLineComment
Emits a single line comment.- Throws:
IOException
-
emitEmptyLine
- Throws:
IOException
-
emitEnumValue
- Throws:
IOException
-
emitEnumValue
A simple switch to emit the proper enum depending if its last causing it to be terminated by a semi-colon (;
).- Throws:
IOException
-
emitEnumValues
Emit a list of enum values followed by a semi-colon (;
).- Throws:
IOException
-
emitAnnotation
Equivalent toannotation(annotation, emptyMap())
.- Throws:
IOException
-
emitAnnotation
Equivalent toannotation(annotationType.getName(), emptyMap())
.- Throws:
IOException
-
emitAnnotation
public JavaWriter emitAnnotation(Class<? extends Annotation> annotationType, Object value) throws IOException Annotates the next element withannotationType
and avalue
.- Parameters:
value
- an object used as the default (value) parameter of the annotation. The value will be encoded using Object.toString(); usestringLiteral(java.lang.String)
for String values. Object arrays are written one element per line.- Throws:
IOException
-
emitAnnotation
Annotates the next element withannotation
and avalue
.- Parameters:
value
- an object used as the default (value) parameter of the annotation. The value will be encoded using Object.toString(); usestringLiteral(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 IOExceptionEquivalent toannotation(annotationType.getName(), attributes)
.- Throws:
IOException
-
emitAnnotation
Annotates the next element withannotation
andattributes
.- Parameters:
attributes
- a map from annotation attribute names to their values. Values are encoded using Object.toString(); usestringLiteral(java.lang.String)
for String values. Object arrays are written one element per line.- Throws:
IOException
-
emitStatement
- Parameters:
pattern
- a code pattern like "int i = %s". Newlines will be further indented. Should not contain trailing semicolon.- Throws:
IOException
-
beginControlFlow
- Parameters:
controlFlow
- the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.- Throws:
IOException
-
beginControlFlow
- Parameters:
controlFlow
- the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.- Throws:
IOException
-
nextControlFlow
- Parameters:
controlFlow
- the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.- Throws:
IOException
-
nextControlFlow
- 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
- Throws:
IOException
-
endControlFlow
- Parameters:
controlFlow
- the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.- Throws:
IOException
-
endControlFlow
- 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
Completes the current method declaration.- Throws:
IOException
-
endConstructor
Completes the current constructor declaration.- Throws:
IOException
-
stringLiteral
Deprecated.useStringLiteral
and itsStringLiteral.literal()
method instead.Returns the string literal representingdata
, including wrapping quotes. -
type
Build a string representation of a type and optionally its generic type arguments. -
rawType
Build a string representation of the raw type for a (optionally generic) type. -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
StringLiteral
and itsStringLiteral.literal()
method instead.