Package com.sun.codemodel
Class JBlock
- java.lang.Object
-
- com.sun.codemodel.JBlock
-
- All Implemented Interfaces:
JGenerable
,JStatement
public final class JBlock extends Object implements 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.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
_break()
Create a break statement and add it to this blockvoid
_break(JLabel label)
void
_continue()
void
_continue(JLabel label)
Create a continue statement and add it to this blockJDoLoop
_do(JExpression test)
Create a Do statement and add it to this blockJForLoop
_for()
Create a For statement and add it to this blockJConditional
_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 blockJSwitch
_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 blockJTryBlock
_try()
Create a Try statement and add it to this blockJWhileLoop
_while(JExpression test)
Create a While statement and add it to this blockJBlock
add(JStatement s)
Adds a statement to this blockJBlock
assign(JAssignmentTarget lhs, JExpression exp)
Creates an assignment statement and adds it to this block.JBlock
assignPlus(JAssignmentTarget lhs, JExpression exp)
JBlock
block()
Create a sub-block and add it to this blockJVar
decl(int mods, JType type, String name, JExpression init)
Adds a local variable declaration to this blockJVar
decl(JType type, String name)
Adds a local variable declaration to this blockJVar
decl(JType type, String name, JExpression init)
Adds a local variable declaration to this blockJStatement
directStatement(String source)
Creates a "literal" statement directly.JForEach
forEach(JType varType, String name, JExpression collection)
Creates an enhanced For statement based on j2se 1.5 JLS and add it to this blockvoid
generate(JFormatter f)
List<Object>
getContents()
Returns a read-only view ofJStatement
s andJDeclaration
in this block.JInvocation
invoke(JExpression expr, JMethod method)
Creates an invocation statement and adds it to this block.JInvocation
invoke(JExpression expr, String method)
Creates an invocation statement and adds it to this block.JInvocation
invoke(JMethod method)
Creates an invocation statement and adds it to this block.JInvocation
invoke(String method)
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.JLabel
label(String name)
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)
JInvocation
staticInvoke(JClass type, String method)
Creates a static invocation statement.
-
-
-
Method Detail
-
getContents
public List<Object> 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(int)
-
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:
pos()
-
isEmpty
public boolean isEmpty()
Returns true if this block is empty and does not contain any statement.
-
decl
public JVar decl(JType type, String name)
Adds a local variable declaration to this block- Parameters:
type
- JType of the variablename
- Name of the variable- Returns:
- Newly generated JVar
-
decl
public JVar decl(JType type, String name, JExpression init)
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
public JVar decl(int mods, JType type, String name, JExpression init)
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
public JBlock assign(JAssignmentTarget lhs, JExpression exp)
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
public JBlock assignPlus(JAssignmentTarget lhs, JExpression exp)
-
invoke
public JInvocation invoke(JExpression expr, String method)
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
public JInvocation invoke(JExpression expr, JMethod method)
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
public JInvocation staticInvoke(JClass type, String method)
Creates a static invocation statement.
-
invoke
public JInvocation invoke(String method)
Creates an invocation statement and adds it to this block.- Parameters:
method
- Name of method to invoke- Returns:
- Newly generated JInvocation
-
invoke
public JInvocation invoke(JMethod method)
Creates an invocation statement and adds it to this block.- Parameters:
method
- JMethod to invoke- Returns:
- Newly generated JInvocation
-
add
public JBlock add(JStatement s)
Adds a statement to this block- Parameters:
s
- JStatement to be added- Returns:
- This block
-
_if
public JConditional _if(JExpression expr)
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
public JForLoop _for()
Create a For statement and add it to this block- Returns:
- Newly generated For statement
-
_while
public JWhileLoop _while(JExpression test)
Create a While statement and add it to this block- Returns:
- Newly generated While statement
-
_switch
public JSwitch _switch(JExpression test)
Create a switch/case statement and add it to this block
-
_do
public JDoLoop _do(JExpression test)
Create a Do statement and add it to this block- Returns:
- Newly generated Do statement
-
_try
public JTryBlock _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
public void _return(JExpression exp)
Create a return statement and add it to this block
-
_throw
public void _throw(JExpression exp)
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
public void _break(JLabel label)
-
label
public JLabel label(String name)
Create a label, which can be referenced fromcontinue
andbreak
statements.
-
_continue
public void _continue(JLabel label)
Create a continue statement and add it to this block
-
_continue
public void _continue()
-
block
public JBlock block()
Create a sub-block and add it to this block
-
directStatement
public JStatement directStatement(String source)
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
public void generate(JFormatter f)
- Specified by:
generate
in interfaceJGenerable
-
forEach
public JForEach forEach(JType varType, String name, JExpression collection)
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
public void state(JFormatter f)
- Specified by:
state
in interfaceJStatement
-
-