Top Description Inners Fields Constructors Methods
com.sun.codemodel

public final Class JFormatter

extends Object
Class Inheritance
Imports
java.io.PrintWriter, .Writer, java.util.ArrayList, .Arrays, .Collection, .Collections, .HashMap, .HashSet, .Iterator, .List, .Map, .Map.Entry

This is a utility class for managing indentation and other basic formatting for PrintWriter.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
pack-priv class
JFormatter.ReferenceList

Used during the optimization of class imports.

Field Summary

Modifier and TypeField and Description
private boolean
private final Map<String, String>
pack-priv static final char
CLOSE_TYPE_ARGS

Special character token we use to differentiate '>' as an operator and '>' as the end of the type arguments.

private HashMap<String, JFormatter.ReferenceList>
private HashSet<JClass>
importedClasses

set of imported types (including package java types, eventhough we won't generate imports for them)

private int
indentLevel

Current number of indentation strings to print

private final String
indentSpace

String to be used for each indentation.

private JPackage
private char
private JFormatter.Mode
mode

The current running mode.

private final PrintWriter
pw

Stream associated with this JFormatter

Constructor Summary

AccessConstructor and Description
public
JFormatter(PrintWriter
PrintWriter to JFormatter to use.
s
,
String
Incremental indentation string, similar to tab value.
space
)

Creates a JFormatter.

public
JFormatter(PrintWriter
PrintWriter to JFormatter to use.
s
,
String
Incremental indentation string, similar to tab value.
space
,
Map<String, String>
Class names to replace.
classNameReplacer
)

Creates a JFormatter.

public
JFormatter(PrintWriter s)

Creates a formatter with default incremental indentations of four spaces.

public
JFormatter(PrintWriter s, Map<String, String> classNameReplacer)

Creates a formatter with default incremental indentations of four spaces and replaces the class names in classNameReplacer.

public
JFormatter(Writer w)

Creates a formatter with default incremental indentations of four spaces.

Method Summary

Modifier and TypeMethod and Description
public JFormatter
b(JVar
the JVar object
v
)

Cause the JVar to generate source for itself

public void
close()

Closes this formatter.

public JFormatter
d(JDeclaration
the JDeclaration object
d
)

Cause the JDeclaration to generate source for itself

public JFormatter
g(JGenerable
the JGenerable object
g
)

Cause the JGenerable object to generate source for iteself

public JFormatter
g(Collection<? extends JGenerable> list)

Produces JGenerables separated by ','

public JFormatter
i()

Increment the indentation level.

public JFormatter
id(String id)

Print an identifier

public boolean
isPrinting()

Returns true if we are in the printing mode, where we actually produce text.

private boolean
needSpace(char c1, char c2)

public JFormatter
nl()

Print a new line into the stream

public JFormatter
o()

Decrement the indentation level.

public JFormatter
p(char
the char
c
)

Print a char into the stream

public JFormatter
p(String
the String
s
)

Print a String into the stream

private String
public JFormatter
s(JStatement
the JStatement object
s
)

Cause the JStatement to generate source for itself

private void
spaceIfNeeded(char c)

private boolean

Returns:

true if an import statement should be suppressed, false otherwise
supressImport
(JClass
JType that may or may not have an import
clazz
,
JClass
JType that is the current class being processed
c
)

determine if an import statement should be supressed

public JFormatter
t(JType type)

public JFormatter
t(JClass type)

Print a type name.

pack-priv void
write(JDefinedClass c)

Generates the whole source code out of the specified class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

atBeginningOfLineback to summary
private boolean atBeginningOfLine
classNameReplacerback to summary
private final Map<String, String> classNameReplacer
CLOSE_TYPE_ARGSback to summary
pack-priv static final char CLOSE_TYPE_ARGS

Special character token we use to differentiate '>' as an operator and '>' as the end of the type arguments. The former uses '>' and it requires a preceding whitespace. The latter uses this, and it does not have a preceding whitespace.

collectedReferencesback to summary
private HashMap<String, JFormatter.ReferenceList> collectedReferences
importedClassesback to summary
private HashSet<JClass> importedClasses

set of imported types (including package java types, eventhough we won't generate imports for them)

indentLevelback to summary
private int indentLevel

Current number of indentation strings to print

indentSpaceback to summary
private final String indentSpace

String to be used for each indentation. Defaults to four spaces.

javaLangback to summary
private JPackage javaLang
lastCharback to summary
private char lastChar
modeback to summary
private JFormatter.Mode mode

The current running mode. Set to PRINTING so that a casual client can use a formatter just like before.

pwback to summary
private final PrintWriter pw

Stream associated with this JFormatter

Constructor Detail

JFormatterback to summary
public JFormatter(PrintWriter s, String space)

Creates a JFormatter.

Parameters
s:PrintWriter

PrintWriter to JFormatter to use.

space:String

Incremental indentation string, similar to tab value.

JFormatterback to summary
public JFormatter(PrintWriter s, String space, Map<String, String> classNameReplacer)

Creates a JFormatter.

Parameters
s:PrintWriter

PrintWriter to JFormatter to use.

space:String

Incremental indentation string, similar to tab value.

classNameReplacer:Map<String, String>

Class names to replace.

JFormatterback to summary
public JFormatter(PrintWriter s)

Creates a formatter with default incremental indentations of four spaces.

JFormatterback to summary
public JFormatter(PrintWriter s, Map<String, String> classNameReplacer)

Creates a formatter with default incremental indentations of four spaces and replaces the class names in classNameReplacer.

JFormatterback to summary
public JFormatter(Writer w)

Creates a formatter with default incremental indentations of four spaces.

Method Detail

bback to summary
public JFormatter b(JVar v)

Cause the JVar to generate source for itself

Parameters
v:JVar

the JVar object

closeback to summary
public void close()

Closes this formatter.

dback to summary
public JFormatter d(JDeclaration d)

Cause the JDeclaration to generate source for itself

Parameters
d:JDeclaration

the JDeclaration object

gback to summary
public JFormatter g(JGenerable g)

Cause the JGenerable object to generate source for iteself

Parameters
g:JGenerable

the JGenerable object

gback to summary
public JFormatter g(Collection<? extends JGenerable> list)

Produces JGenerables separated by ','

iback to summary
public JFormatter i()

Increment the indentation level.

idback to summary
public JFormatter id(String id)

Print an identifier

isPrintingback to summary
public boolean isPrinting()

Returns true if we are in the printing mode, where we actually produce text. The other mode is the "collecting mode'

needSpaceback to summary
private boolean needSpace(char c1, char c2)
nlback to summary
public JFormatter nl()

Print a new line into the stream

oback to summary
public JFormatter o()

Decrement the indentation level.

pback to summary
public JFormatter p(char c)

Print a char into the stream

Parameters
c:char

the char

pback to summary
public JFormatter p(String s)

Print a String into the stream

Parameters
s:String

the String

renameClassNameback to summary
private String renameClassName(String fullName)
sback to summary
public JFormatter s(JStatement s)

Cause the JStatement to generate source for itself

Parameters
s:JStatement

the JStatement object

spaceIfNeededback to summary
private void spaceIfNeeded(char c)
supressImportback to summary
private boolean supressImport(JClass clazz, JClass c)

determine if an import statement should be supressed

Parameters
clazz:JClass

JType that may or may not have an import

c:JClass

JType that is the current class being processed

Returns:boolean

true if an import statement should be suppressed, false otherwise

tback to summary
public JFormatter t(JType type)
tback to summary
public JFormatter t(JClass type)

Print a type name.

In the collecting mode we use this information to decide what types to import and what not to.

writeback to summary
pack-priv void write(JDefinedClass c)

Generates the whole source code out of the specified class.

com.sun.codemodel back to summary

private final Enum JFormatter.Mode

extends Enum<JFormatter.Mode>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final JFormatter.Mode
COLLECTING

Collect all the type names and identifiers.

public static final JFormatter.Mode
PRINTING

Print the actual source code.

Constructor Summary

AccessConstructor and Description
private
Mode()

Method Summary

Modifier and TypeMethod and Description
public static JFormatter.Mode
public static JFormatter.Mode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

COLLECTINGback to summary
public static final JFormatter.Mode COLLECTING

Collect all the type names and identifiers. In this mode we don't actually generate anything.

PRINTINGback to summary
public static final JFormatter.Mode PRINTING

Print the actual source code.

Constructor Detail

Modeback to summary
private Mode()

Method Detail

valueOfback to summary
public static JFormatter.Mode valueOf(String name)
valuesback to summary
public static JFormatter.Mode[] values()
com.sun.codemodel back to summary

pack-priv final Class JFormatter.ReferenceList

extends Object
Class Inheritance

Used during the optimization of class imports. List of JClasses whose short name is the same.
Author
[email protected]

Field Summary

Modifier and TypeField and Description
private final ArrayList<JClass>
private boolean
id

true if this name is used as an identifier (like a variable name.)

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
add(JClass clazz)

public boolean
collisions(JDefinedClass enclosingClass)

Returns true if the symbol represented by the short name is "importable".

public List<JClass>
public boolean
isId()

Return true iff this is strictly an id, meaning that there are no collisions with type names.

public void
setId(boolean value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classesback to summary
private final ArrayList<JClass> classes
idback to summary
private boolean id

true if this name is used as an identifier (like a variable name.)

Constructor Detail

ReferenceListback to summary
pack-priv ReferenceList()

Method Detail

addback to summary
public void add(JClass clazz)
collisionsback to summary
public boolean collisions(JDefinedClass enclosingClass)

Returns true if the symbol represented by the short name is "importable".

getClassesback to summary
public List<JClass> getClasses()
isIdback to summary
public boolean isId()

Return true iff this is strictly an id, meaning that there are no collisions with type names.

setIdback to summary
public void setId(boolean value)