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 classUsed during the optimization of class imports. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean(package private) static final charSpecial 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 intCurrent number of indentation strings to printprivate final StringString to be used for each indentation.private JPackageprivate charprivate JFormatter.ModeThe current running mode.private final PrintWriterStream 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 itselfvoidclose()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) ProducesJGenerables separated by ','i()Increment the indentation level.Print an identifierbooleanReturns true if we are in the printing mode, where we actually produce text.private booleanneedSpace(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 voidspaceIfNeeded(char c) private booleansupressImport(JClass clazz, JClass c) determine if an import statement should be supressedPrint a type name.(package private) voidGenerates 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
-
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
-
p
-
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
ProducesJGenerables 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
-