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.JBlockcontains 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 blockJBlockadd(JStatement s)Adds a statement to this blockJBlockassign(JAssignmentTarget lhs, JExpression exp)Creates an assignment statement and adds it to this block.JBlockassignPlus(JAssignmentTarget lhs, JExpression exp)JBlockblock()Create a sub-block and add it to this blockJVardecl(int mods, JType type, String name, JExpression init)Adds a local variable declaration to this blockJVardecl(JType type, String name)Adds a local variable declaration to this blockJVardecl(JType type, String name, JExpression init)Adds a local variable declaration to this blockJStatementdirectStatement(String source)Creates a "literal" statement directly.JForEachforEach(JType varType, String name, JExpression collection)Creates an enhanced For statement based on j2se 1.5 JLS and add it to this blockvoidgenerate(JFormatter f)List<Object>getContents()Returns a read-only view ofJStatements andJDeclarationin this block.JInvocationinvoke(JExpression expr, JMethod method)Creates an invocation statement and adds it to this block.JInvocationinvoke(JExpression expr, String method)Creates an invocation statement and adds it to this block.JInvocationinvoke(JMethod method)Creates an invocation statement and adds it to this block.JInvocationinvoke(String method)Creates an invocation statement and adds it to this block.booleanisEmpty()Returns true if this block is empty and does not contain any statement.JLabellabel(String name)Create a label, which can be referenced fromcontinueandbreakstatements.intpos()Gets the current position to which new statements will be inserted.intpos(int newPos)Sets the current position.voidstate(JFormatter f)JInvocationstaticInvoke(JClass type, String method)Creates a static invocation statement.
-
-
-
Method Detail
-
getContents
public List<Object> getContents()
Returns a read-only view ofJStatements andJDeclarationin 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 fromcontinueandbreakstatements.
-
_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:
generatein 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:
statein interfaceJStatement
-
-