Package com.sun.codemodel
Class JBlock
java.lang.Object
com.sun.codemodel.JBlock
- All Implemented Interfaces:
JGenerable
,JStatement
A block of Java code, which may contain statements and local declarations.
JBlock
contains a large number of factory methods that creates new
statements/declarations. Those newly created statements/declarations are
inserted into the "current position"
. The position advances
one every time you add a new instruction.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
_break()
Create a break statement and add it to this blockvoid
void
void
Create a continue statement and add it to this block_do
(JExpression test) Create a Do statement and add it to this block_for()
Create a For statement and add it to this block_if
(JExpression expr) Create an If statement and add it to this blockvoid
_return()
Create a return statement and add it to this blockvoid
_return
(JExpression exp) Create a return statement and add it to this block_switch
(JExpression test) Create a switch/case statement and add it to this blockvoid
_throw
(JExpression exp) Create a throw statement and add it to this block_try()
Create a Try statement and add it to this block_while
(JExpression test) Create a While statement and add it to this blockadd
(JStatement s) Adds a statement to this blockassign
(JAssignmentTarget lhs, JExpression exp) Creates an assignment statement and adds it to this block.assignPlus
(JAssignmentTarget lhs, JExpression exp) block()
Create a sub-block and add it to this blockdecl
(int mods, JType type, String name, JExpression init) Adds a local variable declaration to this blockAdds a local variable declaration to this blockdecl
(JType type, String name, JExpression init) Adds a local variable declaration to this blockdirectStatement
(String source) Creates a "literal" statement directly.forEach
(JType varType, String name, JExpression collection) Creates an enhanced For statement based on j2se 1.5 JLS and add it to this blockvoid
Returns a read-only view ofJStatement
s andJDeclaration
in this block.invoke
(JExpression expr, JMethod method) Creates an invocation statement and adds it to this block.invoke
(JExpression expr, String method) Creates an invocation statement and adds it to this block.Creates an invocation statement and adds it to this block.Creates an invocation statement and adds it to this block.boolean
isEmpty()
Returns true if this block is empty and does not contain any statement.Create a label, which can be referenced fromcontinue
andbreak
statements.int
pos()
Gets the current position to which new statements will be inserted.int
pos
(int newPos) Sets the current position.void
state
(JFormatter f) staticInvoke
(JClass type, String method) Creates a static invocation statement.
-
Constructor Details
-
JBlock
public JBlock() -
JBlock
public JBlock(boolean bracesRequired, boolean indentRequired)
-
-
Method Details
-
getContents
Returns a read-only view ofJStatement
s andJDeclaration
in this block. -
pos
public int pos()Gets the current position to which new statements will be inserted. For example if the value is 0, newly created instructions will be inserted at the very beginning of the block.- See Also:
-
pos
public int pos(int newPos) Sets the current position.- Returns:
- the old value of the current position.
- Throws:
IllegalArgumentException
- if the new position value is illegal.- See Also:
-
isEmpty
public boolean isEmpty()Returns true if this block is empty and does not contain any statement. -
decl
Adds a local variable declaration to this block- Parameters:
type
- JType of the variablename
- Name of the variable- Returns:
- Newly generated JVar
-
decl
Adds a local variable declaration to this block- Parameters:
type
- JType of the variablename
- Name of the variableinit
- Initialization expression for this variable. May be null.- Returns:
- Newly generated JVar
-
decl
Adds a local variable declaration to this block- Parameters:
mods
- Modifiers for the variabletype
- JType of the variablename
- Name of the variableinit
- Initialization expression for this variable. May be null.- Returns:
- Newly generated JVar
-
assign
Creates an assignment statement and adds it to this block.- Parameters:
lhs
- Assignable variable or field for left hand side of expressionexp
- Right hand side expression
-
assignPlus
-
invoke
Creates an invocation statement and adds it to this block.- Parameters:
expr
- JExpression evaluating to the class or object upon which the named method will be invokedmethod
- Name of method to invoke- Returns:
- Newly generated JInvocation
-
invoke
Creates an invocation statement and adds it to this block.- Parameters:
expr
- JExpression evaluating to the class or object upon which the method will be invokedmethod
- JMethod to invoke- Returns:
- Newly generated JInvocation
-
staticInvoke
Creates a static invocation statement. -
invoke
Creates an invocation statement and adds it to this block.- Parameters:
method
- Name of method to invoke- Returns:
- Newly generated JInvocation
-
invoke
Creates an invocation statement and adds it to this block.- Parameters:
method
- JMethod to invoke- Returns:
- Newly generated JInvocation
-
add
Adds a statement to this block- Parameters:
s
- JStatement to be added- Returns:
- This block
-
_if
Create an If statement and add it to this block- Parameters:
expr
- JExpression to be tested to determine branching- Returns:
- Newly generated conditional statement
-
_for
Create a For statement and add it to this block- Returns:
- Newly generated For statement
-
_while
Create a While statement and add it to this block- Returns:
- Newly generated While statement
-
_switch
Create a switch/case statement and add it to this block -
_do
Create a Do statement and add it to this block- Returns:
- Newly generated Do statement
-
_try
Create a Try statement and add it to this block- Returns:
- Newly generated Try statement
-
_return
public void _return()Create a return statement and add it to this block -
_return
Create a return statement and add it to this block -
_throw
Create a throw statement and add it to this block -
_break
public void _break()Create a break statement and add it to this block -
_break
-
label
Create a label, which can be referenced fromcontinue
andbreak
statements. -
_continue
Create a continue statement and add it to this block -
_continue
public void _continue() -
block
Create a sub-block and add it to this block -
directStatement
Creates a "literal" statement directly.Specified string is printed as-is. This is useful as a short-cut.
For example, you can invoke this method as:
directStatement("a=b+c;")
. -
generate
- Specified by:
generate
in interfaceJGenerable
-
forEach
Creates an enhanced For statement based on j2se 1.5 JLS and add it to this block- Returns:
- Newly generated enhanced For statement per j2se 1.5 specification
-
state
- Specified by:
state
in interfaceJStatement
-