Package com.squareup.javawriter
Class JavaWriter
- java.lang.Object
-
- com.squareup.javawriter.JavaWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class JavaWriter extends java.lang.Object implements java.io.CloseableA utility class which aids in generating Java source files.
-
-
Constructor Summary
Constructors Constructor Description JavaWriter(java.io.Writer out)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description JavaWriterbeginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String... parameters)JavaWriterbeginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.util.List<java.lang.String> parameters, java.util.List<java.lang.String> throwsTypes)JavaWriterbeginControlFlow(java.lang.String controlFlow)JavaWriterbeginControlFlow(java.lang.String controlFlow, java.lang.Object... args)JavaWriterbeginInitializer(boolean isStatic)Emits an initializer declaration.JavaWriterbeginMethod(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.JavaWriterbeginMethod(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.JavaWriterbeginType(java.lang.String type, java.lang.String kind)Emits a type declaration.JavaWriterbeginType(java.lang.String type, java.lang.String kind, java.util.Set<javax.lang.model.element.Modifier> modifiers)Emits a type declaration.JavaWriterbeginType(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.voidclose()java.lang.StringcompressType(java.lang.String type)Try to compress a fully-qualified class name to only the class name.JavaWriteremitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Equivalent toannotation(annotationType.getName(), emptyMap()).JavaWriteremitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object value)Annotates the next element withannotationTypeand avalue.JavaWriteremitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.util.Map<java.lang.String,?> attributes)Equivalent toannotation(annotationType.getName(), attributes).JavaWriteremitAnnotation(java.lang.String annotation)Equivalent toannotation(annotation, emptyMap()).JavaWriteremitAnnotation(java.lang.String annotation, java.lang.Object value)Annotates the next element withannotationand avalue.JavaWriteremitAnnotation(java.lang.String annotation, java.util.Map<java.lang.String,?> attributes)Annotates the next element withannotationandattributes.JavaWriteremitEmptyLine()JavaWriteremitEnumValue(java.lang.String name)JavaWriteremitEnumValue(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 (;).JavaWriteremitEnumValues(java.lang.Iterable<java.lang.String> names)Emit a list of enum values followed by a semi-colon (;).JavaWriteremitField(java.lang.String type, java.lang.String name)Emits a field declaration.JavaWriteremitField(java.lang.String type, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers)Emits a field declaration.JavaWriteremitField(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.JavaWriteremitImports(java.lang.Class<?>... types)Emit an import for eachtypeprovided.JavaWriteremitImports(java.lang.String... types)Emit an import for eachtypeprovided.JavaWriteremitImports(java.util.Collection<java.lang.String> types)Emit an import for eachtypein the providedCollection.JavaWriteremitJavadoc(java.lang.String javadoc, java.lang.Object... params)Emits some Javadoc comments with line separated by\n.JavaWriteremitPackage(java.lang.String packageName)Emit a package declaration and empty line.JavaWriteremitSingleLineComment(java.lang.String comment, java.lang.Object... args)Emits a single line comment.JavaWriteremitStatement(java.lang.String pattern, java.lang.Object... args)JavaWriteremitStaticImports(java.lang.String... types)Emit a static import for eachtypeprovided.JavaWriteremitStaticImports(java.util.Collection<java.lang.String> types)Emit a static import for eachtypein the providedCollection.JavaWriterendConstructor()Completes the current constructor declaration.JavaWriterendControlFlow()JavaWriterendControlFlow(java.lang.String controlFlow)JavaWriterendControlFlow(java.lang.String controlFlow, java.lang.Object... args)JavaWriterendInitializer()Ends the current initializer declaration.JavaWriterendMethod()Completes the current method declaration.JavaWriterendType()Completes the current type declaration.java.lang.StringgetIndent()booleanisCompressingTypes()JavaWriternextControlFlow(java.lang.String controlFlow)JavaWriternextControlFlow(java.lang.String controlFlow, java.lang.Object... args)static java.lang.StringrawType(java.lang.String type)Build a string representation of the raw type for a (optionally generic) type.voidsetCompressingTypes(boolean isCompressingTypes)voidsetIndent(java.lang.String indent)static java.lang.StringstringLiteral(java.lang.String data)Deprecated.useStringLiteraland itsStringLiteral.literal()method instead.static java.lang.Stringtype(java.lang.Class<?> raw, java.lang.String... parameters)Build a string representation of a type and optionally its generic type arguments.
-
-
-
Method Detail
-
setCompressingTypes
public void setCompressingTypes(boolean isCompressingTypes)
-
isCompressingTypes
public boolean isCompressingTypes()
-
setIndent
public void setIndent(java.lang.String indent)
-
getIndent
public java.lang.String getIndent()
-
emitPackage
public JavaWriter emitPackage(java.lang.String packageName) throws java.io.IOException
Emit a package declaration and empty line.- Throws:
java.io.IOException
-
emitImports
public JavaWriter emitImports(java.lang.String... types) throws java.io.IOException
Emit an import for eachtypeprovided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
java.io.IOException
-
emitImports
public JavaWriter emitImports(java.lang.Class<?>... types) throws java.io.IOException
Emit an import for eachtypeprovided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
java.io.IOException
-
emitImports
public JavaWriter emitImports(java.util.Collection<java.lang.String> types) throws java.io.IOException
Emit an import for eachtypein the providedCollection. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
java.io.IOException
-
emitStaticImports
public JavaWriter emitStaticImports(java.lang.String... types) throws java.io.IOException
Emit a static import for eachtypeprovided. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
java.io.IOException
-
emitStaticImports
public JavaWriter emitStaticImports(java.util.Collection<java.lang.String> types) throws java.io.IOException
Emit a static import for eachtypein the providedCollection. For the duration of the file, all references to these classes will be automatically shortened.- Throws:
java.io.IOException
-
compressType
public java.lang.String compressType(java.lang.String type)
Try to compress a fully-qualified class name to only the class name.
-
beginInitializer
public JavaWriter beginInitializer(boolean isStatic) throws java.io.IOException
Emits an initializer declaration.- Parameters:
isStatic- true if it should be an static initializer, false for an instance initializer.- Throws:
java.io.IOException
-
endInitializer
public JavaWriter endInitializer() throws java.io.IOException
Ends the current initializer declaration.- Throws:
java.io.IOException
-
beginType
public JavaWriter beginType(java.lang.String type, java.lang.String kind) throws java.io.IOException
Emits a type declaration.- Parameters:
kind- such as "class", "interface" or "enum".- Throws:
java.io.IOException
-
beginType
public JavaWriter beginType(java.lang.String type, java.lang.String kind, java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
Emits a type declaration.- Parameters:
kind- such as "class", "interface" or "enum".- Throws:
java.io.IOException
-
beginType
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
Emits a type declaration.- Parameters:
kind- such as "class", "interface" or "enum".extendsType- the class to extend, or null for no extends clause.- Throws:
java.io.IOException
-
endType
public JavaWriter endType() throws java.io.IOException
Completes the current type declaration.- Throws:
java.io.IOException
-
emitField
public JavaWriter emitField(java.lang.String type, java.lang.String name) throws java.io.IOException
Emits a field declaration.- Throws:
java.io.IOException
-
emitField
public JavaWriter emitField(java.lang.String type, java.lang.String name, java.util.Set<javax.lang.model.element.Modifier> modifiers) throws java.io.IOException
Emits a field declaration.- Throws:
java.io.IOException
-
emitField
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
Emits a field declaration.- Throws:
java.io.IOException
-
beginMethod
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
Emit a method declaration.A
nullreturn 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:
java.io.IOException
-
beginMethod
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
Emit a method declaration.A
nullreturn 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:
java.io.IOException
-
beginConstructor
public JavaWriter beginConstructor(java.util.Set<javax.lang.model.element.Modifier> modifiers, java.lang.String... parameters) throws java.io.IOException
- Throws:
java.io.IOException
-
beginConstructor
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
- Throws:
java.io.IOException
-
emitJavadoc
public JavaWriter emitJavadoc(java.lang.String javadoc, java.lang.Object... params) throws java.io.IOException
Emits some Javadoc comments with line separated by\n.- Throws:
java.io.IOException
-
emitSingleLineComment
public JavaWriter emitSingleLineComment(java.lang.String comment, java.lang.Object... args) throws java.io.IOException
Emits a single line comment.- Throws:
java.io.IOException
-
emitEmptyLine
public JavaWriter emitEmptyLine() throws java.io.IOException
- Throws:
java.io.IOException
-
emitEnumValue
public JavaWriter emitEnumValue(java.lang.String name) throws java.io.IOException
- Throws:
java.io.IOException
-
emitEnumValue
public JavaWriter emitEnumValue(java.lang.String name, boolean isLast) throws java.io.IOException
A simple switch to emit the proper enum depending if its last causing it to be terminated by a semi-colon (;).- Throws:
java.io.IOException
-
emitEnumValues
public JavaWriter emitEnumValues(java.lang.Iterable<java.lang.String> names) throws java.io.IOException
Emit a list of enum values followed by a semi-colon (;).- Throws:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.String annotation) throws java.io.IOException
Equivalent toannotation(annotation, emptyMap()).- Throws:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) throws java.io.IOException
Equivalent toannotation(annotationType.getName(), emptyMap()).- Throws:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Object value) throws java.io.IOException
Annotates the next element withannotationTypeand 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:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.String annotation, java.lang.Object value) throws java.io.IOException
Annotates the next element withannotationand 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:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.util.Map<java.lang.String,?> attributes) throws java.io.IOException
Equivalent toannotation(annotationType.getName(), attributes).- Throws:
java.io.IOException
-
emitAnnotation
public JavaWriter emitAnnotation(java.lang.String annotation, java.util.Map<java.lang.String,?> attributes) throws java.io.IOException
Annotates the next element withannotationandattributes.- 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:
java.io.IOException
-
emitStatement
public JavaWriter emitStatement(java.lang.String pattern, java.lang.Object... args) throws java.io.IOException
- Parameters:
pattern- a code pattern like "int i = %s". Newlines will be further indented. Should not contain trailing semicolon.- Throws:
java.io.IOException
-
beginControlFlow
public JavaWriter beginControlFlow(java.lang.String controlFlow) throws java.io.IOException
- Parameters:
controlFlow- the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.- Throws:
java.io.IOException
-
beginControlFlow
public JavaWriter beginControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
- Parameters:
controlFlow- the control flow construct and its code, such as "if (foo == 5)". Shouldn't contain braces or newline characters.- Throws:
java.io.IOException
-
nextControlFlow
public JavaWriter nextControlFlow(java.lang.String controlFlow) throws java.io.IOException
- Parameters:
controlFlow- the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.- Throws:
java.io.IOException
-
nextControlFlow
public JavaWriter nextControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
- Parameters:
controlFlow- the control flow construct and its code, such as "else if (foo == 10)". Shouldn't contain braces or newline characters.- Throws:
java.io.IOException
-
endControlFlow
public JavaWriter endControlFlow() throws java.io.IOException
- Throws:
java.io.IOException
-
endControlFlow
public JavaWriter endControlFlow(java.lang.String controlFlow) throws java.io.IOException
- Parameters:
controlFlow- the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.- Throws:
java.io.IOException
-
endControlFlow
public JavaWriter endControlFlow(java.lang.String controlFlow, java.lang.Object... args) throws java.io.IOException
- Parameters:
controlFlow- the optional control flow construct and its code, such as "while(foo == 20)". Only used for "do/while" control flows.- Throws:
java.io.IOException
-
endMethod
public JavaWriter endMethod() throws java.io.IOException
Completes the current method declaration.- Throws:
java.io.IOException
-
endConstructor
public JavaWriter endConstructor() throws java.io.IOException
Completes the current constructor declaration.- Throws:
java.io.IOException
-
stringLiteral
@Deprecated public static java.lang.String stringLiteral(java.lang.String data)
Deprecated.useStringLiteraland itsStringLiteral.literal()method instead.Returns the string literal representingdata, including wrapping quotes.
-
type
public 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.
-
rawType
public static java.lang.String rawType(java.lang.String type)
Build a string representation of the raw type for a (optionally generic) type.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
-