|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectoutput.java.CodeBuilder
public final class CodeBuilder
An object for building source code, analogous to StringBuilder for
building strings or the various builders in Builders for
bulding collections.
| Field Summary | |
|---|---|
State |
initialState
The initial state with which this code builder was constructed. |
| Constructor Summary | |
|---|---|
CodeBuilder()
Default constructor. |
|
CodeBuilder(State initialState)
Construct a code builder to be embedded in another. |
|
| Method Summary | |
|---|---|
CodeBuilder |
addImport(String target)
Add an import statement to the top level. |
CodeBuilder |
addImport(String fmt,
Object... args)
Call addImport(String.format(fmt, args)). |
CodeBuilder |
addPackageStatement(String pkgName)
Add a package declaration. |
CodeBuilder |
addPackageStatement(String fmt,
Object... args)
Call addPackageStatement(String.format(fmt, args)). |
CodeBuilder |
addStaticImport(String target)
Add a static import statement to the top level. |
CodeBuilder |
addStaticImport(String fmt,
Object... args)
Call addStaticImport(String.format(fmt, args)). |
CodeBuilder |
append(CodeBuilder builder)
Absorb the contents of another code builder. |
CodeBuilder |
append(String code)
Add to the code being built. |
CodeBuilder |
append(String fmt,
Object... args)
Call append(String.format(fmt, args)). |
CodeBuilder |
appendComment(String comment)
Add a one-line-style comment to the code being built. |
CodeBuilder |
appendComment(String fmt,
Object... args)
Call appendComment(String.format(fmt, args)). |
CodeBuilder |
beginBlock(String header)
Begin building some kind of code block within a method. |
CodeBuilder |
beginBlock(String fmt,
Object... args)
Call beginBlock(String.format(fmt, args)). |
CodeBuilder |
beginClass(String decl)
Begin building a class (or interface or enum). |
CodeBuilder |
beginClass(String fmt,
Object... args)
Call beginClass(String.format(fmt, args)). |
CodeBuilder |
beginComment()
Begin building a multi-line comment. |
CodeBuilder |
beginJavaDoc()
Begin building a JavaDoc comment. |
CodeBuilder |
beginMethod(String decl)
Begin building a method (or constructor). |
CodeBuilder |
beginMethod(String fmt,
Object... args)
Call beginMethod(String.format(fmt, args)). |
CodeBuilder |
beginStaticBlock()
Begin building a static block for a class. |
CodeBuilder |
chainBlock(String betweenBraces)
Finish building a code block, but begin another immediately. |
CodeBuilder |
chainBlock(String fmt,
Object... args)
Call chainBlock(String.format(fmt, args)). |
String |
finish()
Return the code generated by the builder. |
CodeBuilder |
finishBlock()
Finish building a code block. |
CodeBuilder |
finishBlock(String afterBrace)
Finish building a code block requiring further code after the closing brace. |
CodeBuilder |
finishBlock(String fmt,
Object... args)
Call finishBlock(String.format(fmt, args)). |
CodeBuilder |
finishClass()
Finish building a class (or interface or enum). |
CodeBuilder |
finishComment()
Finish building a multi-line comment. |
CodeBuilder |
finishMethod()
Finish building a method (or constructor). |
CodeBuilder |
finishStaticBlock()
Finish building a static block for a class. |
CodeBuilder |
format(String fmt,
Object... args)
Add to the code being built, using String.format(String, Object[]). |
State |
getState()
Get the current state of the code builder. |
String |
toString()
Return the code generated by the builder so far. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public final State initialState
State.TOPLEVEL.
State| Constructor Detail |
|---|
public CodeBuilder()
public CodeBuilder(State initialState)
State.CLASS.
initialState - The initial state for this code builder.State| Method Detail |
|---|
public State getState()
append(String) will go.
public CodeBuilder addImport(String target)
initialState
is not State.TOPLEVEL), so long as it is passed directly to
the append(CodeBuilder) method of a top-level code builder (or
a builder that is so passed, and so forth).
target - The name of the package to import. The same as would be
found in the statement "import stuff.package;", but without the
"import" or the semicolon. The name can end in an asterisk, just as
in the import statement.
this, for chaining.
public CodeBuilder addImport(String fmt,
Object... args)
addImport(String.format(fmt, args)).
addImport(String)public CodeBuilder addStaticImport(String target)
target - The identifier of the class member to import.
this, for chaining.addImport(String)
public CodeBuilder addStaticImport(String fmt,
Object... args)
addStaticImport(String.format(fmt, args)).
addStaticImport(String)public CodeBuilder addPackageStatement(String pkgName)
State.TOPLEVEL state, and only once.
pkgName - The name of the package to declare.
this, for chaining.
public CodeBuilder addPackageStatement(String fmt,
Object... args)
addPackageStatement(String.format(fmt, args)).
addPackageStatement(String)public CodeBuilder append(String code)
code - The code to add. Must include the semicolon if the
code is a statement; however, a newline will be added
automatically. (If the code spans more than one line, the line
breaks will be respected.) The newline will be omitted if code is
empty (for a blank line use append("\n")).
this, for chaining.
public CodeBuilder append(String fmt,
Object... args)
append(String.format(fmt, args)).
append(String)public CodeBuilder append(CodeBuilder builder)
builder - The builder with the code to append.
this, for chaining.
CodeBuilderStateError - If the current state does not match the
initial state of the other builder or it is not complete.
public CodeBuilder format(String fmt,
Object... args)
String.format(String, Object[]).
builder.format(fmt, arg1, arg2) is shorthand for
builder.append(String.format(fmt, arg1, arg2)) and does the same
thing.
fmt - The format string.args - The arguments referred to in the format string.
this, for chaining.String.format(String, Object[])public CodeBuilder beginClass(String decl)
State.CLASS.
decl - The declaration for the class. This means anything that
would be in the class statement, up to but not including
the opening brace.
this, for chaining.
public CodeBuilder beginClass(String fmt,
Object... args)
beginClass(String.format(fmt, args)).
beginClass(String)public CodeBuilder finishClass()
this, for chaining.
CodeBuilderStateError - If the current state is not
State.CLASS.public CodeBuilder beginMethod(String decl)
State.METHOD.
decl - The declaration for the method. This means anything that
would be in the method declaration in the code, up to but
not including the opening brace.
this, for chaining.
CodeBuilderStateError - If the current state is not
State.CLASS.
public CodeBuilder beginMethod(String fmt,
Object... args)
beginMethod(String.format(fmt, args)).
beginMethod(String)public CodeBuilder finishMethod()
this, for chaining.
CodeBuilderStateError - If the current state is not
State.METHOD.public CodeBuilder beginStaticBlock()
State.METHOD.
this, for chaining.
CodeBuilderStateError - If the current state is not
State.CLASS.public CodeBuilder finishStaticBlock()
this, for chaining.
CodeBuilderStateError - If the current state is not
State.METHOD.public CodeBuilder beginBlock(String header)
if statement, a for loop,
a switch block, even a bare { ... } block used for
scoping. The one thing it shouldn't be is an inner class
declaration - for that, use beginClass(String).
The current state will become State.BLOCK.
header - The part of the statement before the opening brace -
for instance if (condition), switch(thing), do,
or the empty string for bare braces. May include a label declaration
(LABEL:) if you're into that sort of thing.
this, for chaining.
CodeBuilderStateError - If the current state is neither
State.METHOD nor State.BLOCK.
public CodeBuilder beginBlock(String fmt,
Object... args)
beginBlock(String.format(fmt, args)).
beginBlock(String)public CodeBuilder finishBlock()
this, for chaining.
CodeBuilderStateError - If the current state is not
State.BLOCK.public CodeBuilder finishBlock(String afterBrace)
while part of a do/while
loop.
afterBrace - The code to go after the brace, including any necessary
semicolon.
this, for chaining.
CodeBuilderStateError - If the current state is not
State.BLOCK.
public CodeBuilder finishBlock(String fmt,
Object... args)
finishBlock(String.format(fmt, args)).
finishBlock(String)public CodeBuilder chainBlock(String betweenBraces)
if (condition) {
// ...
} else if (otherCondition) {
// ...
} else if (anotherCondition) {
// ...
} else {
// ...
}
Each of the else-ifs and the else would be
generated by a call to this method. (The original if and the
final closing brace would be generated by
beginBlock("if (condition)") and finishBlock(),
respectively.)
betweenBraces - The code to be put between the closing brace of the
old block and the opening brace of the new one. (For example, the
"else if" or "else" above.)
this, for chaining.
CodeBuilderStateError - If the current state is not
State.BLOCK.
public CodeBuilder chainBlock(String fmt,
Object... args)
chainBlock(String.format(fmt, args)).
chainBlock(String)public CodeBuilder appendComment(String comment)
comment - The text of the comment.
this, for chaining.
public CodeBuilder appendComment(String fmt,
Object... args)
appendComment(String.format(fmt, args)).
appendComment(String)public CodeBuilder beginComment()
State.COMMENT.
this, for chaining.
CodeBuilderStateError - If the current state is already
State.COMMENT.public CodeBuilder beginJavaDoc()
State.COMMENT.
this, for chaining.
CodeBuilderStateError - If the current state is already
State.COMMENT.public CodeBuilder finishComment()
this, for chaining.
CodeBuilderStateError - If the current state is not
State.COMMENT.public String toString()
toString in class Objectpublic String finish()
finish in interface Builders.Builder<String>CodeBuilderStateError - If there remain unfinished scopes.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||