Package com.sun.codemodel
Class JFormatter
- java.lang.Object
-
- com.sun.codemodel.JFormatter
-
public final class JFormatter extends java.lang.Object
This is a utility class for managing indentation and other basic formatting for PrintWriter.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
JFormatter.Mode
(package private) class
JFormatter.ReferenceList
Used during the optimization of class imports.
-
Field Summary
Fields Modifier and Type Field Description private boolean
atBeginningOfLine
(package private) static char
CLOSE_TYPE_ARGS
Special character token we use to differenciate '>' as an operator and '>' as the end of the type arguments.private java.util.HashMap<java.lang.String,JFormatter.ReferenceList>
collectedReferences
map from short type name to ReferenceList (list of JClass and ids sharing that name)private java.util.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 printprivate java.lang.String
indentSpace
String to be used for each indentation.private JPackage
javaLang
private char
lastChar
private JFormatter.Mode
mode
The current running mode.private java.io.PrintWriter
pw
Stream associated with this JFormatter
-
Constructor Summary
Constructors Constructor Description JFormatter(java.io.PrintWriter s)
Creates a formatter with default incremental indentations of four spaces.JFormatter(java.io.PrintWriter s, java.lang.String space)
Creates a JFormatter.JFormatter(java.io.Writer w)
Creates a formatter with default incremental indentations of four spaces.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JFormatter
b(JVar v)
Cause the JVar to generate source for itselfvoid
close()
Closes this formatter.JFormatter
d(JDeclaration d)
Cause the JDeclaration to generate source for itselfJFormatter
g(JGenerable g)
Cause the JGenerable object to generate source for iteselfJFormatter
g(java.util.Collection<? extends JGenerable> list)
ProducesJGenerable
s separated by ','JFormatter
i()
Increment the indentation level.JFormatter
id(java.lang.String id)
Print an identifierboolean
isPrinting()
Returns true if we are in the printing mode, where we actually produce text.private boolean
needSpace(char c1, char c2)
JFormatter
nl()
Print a new line into the streamJFormatter
o()
Decrement the indentation level.JFormatter
p(char c)
Print a char into the streamJFormatter
p(java.lang.String s)
Print a String into the streamJFormatter
s(JStatement s)
Cause the JStatement to generate source for itselfprivate void
spaceIfNeeded(char c)
private boolean
supressImport(JClass clazz, JClass c)
determine if an import statement should be supressedJFormatter
t(JClass type)
Print a type name.JFormatter
t(JType type)
(package private) void
write(JDefinedClass c)
Generates the whole source code out of the specified class.
-
-
-
Field Detail
-
collectedReferences
private java.util.HashMap<java.lang.String,JFormatter.ReferenceList> collectedReferences
map from short type name to ReferenceList (list of JClass and ids sharing that name)
-
importedClasses
private java.util.HashSet<JClass> importedClasses
set of imported types (including package java types, eventhough we won't generate imports for them)
-
mode
private JFormatter.Mode mode
The current running mode. Set to PRINTING so that a casual client can use a formatter just like before.
-
indentLevel
private int indentLevel
Current number of indentation strings to print
-
indentSpace
private final java.lang.String indentSpace
String to be used for each indentation. Defaults to four spaces.
-
pw
private final java.io.PrintWriter pw
Stream associated with this JFormatter
-
lastChar
private char lastChar
-
atBeginningOfLine
private boolean atBeginningOfLine
-
javaLang
private JPackage javaLang
-
CLOSE_TYPE_ARGS
static final char CLOSE_TYPE_ARGS
Special character token we use to differenciate '>' 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.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JFormatter
public JFormatter(java.io.PrintWriter s, java.lang.String space)
Creates a JFormatter.- Parameters:
s
- PrintWriter to JFormatter to use.space
- Incremental indentation string, similar to tab value.
-
JFormatter
public JFormatter(java.io.PrintWriter s)
Creates a formatter with default incremental indentations of four spaces.
-
JFormatter
public JFormatter(java.io.Writer w)
Creates a formatter with default incremental indentations of four spaces.
-
-
Method Detail
-
close
public void close()
Closes this formatter.
-
isPrinting
public boolean isPrinting()
Returns true if we are in the printing mode, where we actually produce text. The other mode is the "collecting mode'
-
o
public JFormatter o()
Decrement the indentation level.
-
i
public JFormatter i()
Increment the indentation level.
-
needSpace
private boolean needSpace(char c1, char c2)
-
spaceIfNeeded
private void spaceIfNeeded(char c)
-
p
public JFormatter p(char c)
Print a char into the stream- Parameters:
c
- the char
-
p
public JFormatter p(java.lang.String s)
Print a String into the stream- Parameters:
s
- the String
-
t
public JFormatter t(JType type)
-
t
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.
-
id
public JFormatter id(java.lang.String id)
Print an identifier
-
nl
public JFormatter nl()
Print a new line into the stream
-
g
public JFormatter g(JGenerable g)
Cause the JGenerable object to generate source for iteself- Parameters:
g
- the JGenerable object
-
g
public JFormatter g(java.util.Collection<? extends JGenerable> list)
ProducesJGenerable
s separated by ','
-
d
public JFormatter d(JDeclaration d)
Cause the JDeclaration to generate source for itself- Parameters:
d
- the JDeclaration object
-
s
public JFormatter s(JStatement s)
Cause the JStatement to generate source for itself- Parameters:
s
- the JStatement object
-
b
public JFormatter b(JVar v)
Cause the JVar to generate source for itself- Parameters:
v
- the JVar object
-
write
void write(JDefinedClass c)
Generates the whole source code out of the specified class.
-
supressImport
private boolean supressImport(JClass clazz, JClass c)
determine if an import statement should be supressed- Parameters:
clazz
- JType that may or may not have an importc
- JType that is the current class being processed- Returns:
- true if an import statement should be suppressed, false otherwise
-
-