Package com.sun.codemodel
Class JFormatter
java.lang.Object
com.sun.codemodel.JFormatter
This is a utility class for managing indentation and other basic
formatting for PrintWriter.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enum
(package private) final class
Used during the optimization of class imports. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
(package private) static final char
Special character token we use to differenciate '>' as an operator and '>' as the end of the type arguments.private HashMap
<String, JFormatter.ReferenceList> map from short type name to ReferenceList (list of JClass and ids sharing that name)set of imported types (including package java types, eventhough we won't generate imports for them)private int
Current number of indentation strings to printprivate final String
String to be used for each indentation.private JPackage
private char
private JFormatter.Mode
The current running mode.private final PrintWriter
Stream associated with this JFormatter -
Constructor Summary
ConstructorsConstructorDescriptionCreates a formatter with default incremental indentations of four spaces.JFormatter
(PrintWriter s, String space) Creates a JFormatter.JFormatter
(Writer w) Creates a formatter with default incremental indentations of four spaces. -
Method Summary
Modifier and TypeMethodDescriptionCause the JVar to generate source for itselfvoid
close()
Closes this formatter.d
(JDeclaration d) Cause the JDeclaration to generate source for itselfg
(JGenerable g) Cause the JGenerable object to generate source for iteselfg
(Collection<? extends JGenerable> list) ProducesJGenerable
s separated by ','i()
Increment the indentation level.Print an identifierboolean
Returns true if we are in the printing mode, where we actually produce text.private boolean
needSpace
(char c1, char c2) nl()
Print a new line into the streamo()
Decrement the indentation level.p
(char c) Print a char into the streamPrint a String into the streams
(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 supressedPrint a type name.(package private) void
Generates the whole source code out of the specified class.
-
Field Details
-
collectedReferences
map from short type name to ReferenceList (list of JClass and ids sharing that name) -
importedClasses
set of imported types (including package java types, eventhough we won't generate imports for them) -
mode
The current running mode. Set to PRINTING so that a casual client can use a formatter just like before. -
indentLevel
private int indentLevelCurrent number of indentation strings to print -
indentSpace
String to be used for each indentation. Defaults to four spaces. -
pw
Stream associated with this JFormatter -
lastChar
private char lastChar -
atBeginningOfLine
private boolean atBeginningOfLine -
javaLang
-
CLOSE_TYPE_ARGS
static final char CLOSE_TYPE_ARGSSpecial 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:
-
-
Constructor Details
-
JFormatter
Creates a JFormatter.- Parameters:
s
- PrintWriter to JFormatter to use.space
- Incremental indentation string, similar to tab value.
-
JFormatter
Creates a formatter with default incremental indentations of four spaces. -
JFormatter
Creates a formatter with default incremental indentations of four spaces.
-
-
Method Details
-
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
Decrement the indentation level. -
i
Increment the indentation level. -
needSpace
private boolean needSpace(char c1, char c2) -
spaceIfNeeded
private void spaceIfNeeded(char c) -
p
Print a char into the stream- Parameters:
c
- the char
-
p
Print a String into the stream- Parameters:
s
- the String
-
t
-
t
Print a type name.In the collecting mode we use this information to decide what types to import and what not to.
-
id
Print an identifier -
nl
Print a new line into the stream -
g
Cause the JGenerable object to generate source for iteself- Parameters:
g
- the JGenerable object
-
g
ProducesJGenerable
s separated by ',' -
d
Cause the JDeclaration to generate source for itself- Parameters:
d
- the JDeclaration object
-
s
Cause the JStatement to generate source for itself- Parameters:
s
- the JStatement object
-
b
Cause the JVar to generate source for itself- Parameters:
v
- the JVar object
-
write
Generates the whole source code out of the specified class. -
supressImport
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
-