public final class JBlock extends Object implements JGenerable, JStatement
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 and Description |
---|
JBlock() |
JBlock(boolean bracesRequired,
boolean indentRequired) |
Modifier and Type | Method and Description |
---|---|
void |
_break()
Create a break statement and add it to this block
|
void |
_break(JLabel label) |
void |
_continue() |
void |
_continue(JLabel label)
Create a continue statement and add it to this block
|
JDoLoop |
_do(JExpression test)
Create a Do statement and add it to this block
|
JForLoop |
_for()
Create a For statement and add it to this block
|
JConditional |
_if(JExpression expr)
Create an If statement and add it to this block
|
void |
_return()
Create a return statement and add it to this block
|
void |
_return(JExpression exp)
Create a return statement and add it to this block
|
JSwitch |
_switch(JExpression test)
Create a switch/case statement and add it to this block
|
void |
_throw(JExpression exp)
Create a throw statement and add it to this block
|
JTryBlock |
_try()
Create a Try statement and add it to this block
|
JWhileLoop |
_while(JExpression test)
Create a While statement and add it to this block
|
JBlock |
add(JStatement s)
Adds a statement to this block
|
JBlock |
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 block
|
JVar |
decl(int mods,
JType type,
String name,
JExpression init)
Adds a local variable declaration to this block
|
JVar |
decl(JType type,
String name)
Adds a local variable declaration to this block
|
JVar |
decl(JType type,
String name,
JExpression init)
Adds a local variable declaration to this block
|
JStatement |
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 block
|
void |
generate(JFormatter f) |
List<Object> |
getContents()
Returns a read-only view of
JStatement s and JDeclaration
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 from
continue and break 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.
|
public JBlock()
public JBlock(boolean bracesRequired, boolean indentRequired)
public List<Object> getContents()
JStatement
s and JDeclaration
in this block.public int pos()
pos(int)
public int pos(int newPos)
IllegalArgumentException
- if the new position value is illegal.pos()
public boolean isEmpty()
public JVar decl(JType type, String name)
type
- JType of the variablename
- Name of the variablepublic JVar decl(JType type, String name, JExpression init)
type
- JType of the variablename
- Name of the variableinit
- Initialization expression for this variable. May be null.public JVar decl(int mods, JType type, String name, JExpression init)
mods
- Modifiers for the variabletype
- JType of the variablename
- Name of the variableinit
- Initialization expression for this variable. May be null.public JBlock assign(JAssignmentTarget lhs, JExpression exp)
lhs
- Assignable variable or field for left hand side of expressionexp
- Right hand side expressionpublic JBlock assignPlus(JAssignmentTarget lhs, JExpression exp)
public JInvocation invoke(JExpression expr, String method)
expr
- JExpression evaluating to the class or object upon which
the named method will be invokedmethod
- Name of method to invokepublic JInvocation invoke(JExpression expr, JMethod method)
expr
- JExpression evaluating to the class or object upon which
the method will be invokedmethod
- JMethod to invokepublic JInvocation staticInvoke(JClass type, String method)
public JInvocation invoke(String method)
method
- Name of method to invokepublic JInvocation invoke(JMethod method)
method
- JMethod to invokepublic JBlock add(JStatement s)
s
- JStatement to be addedpublic JConditional _if(JExpression expr)
expr
- JExpression to be tested to determine branchingpublic JForLoop _for()
public JWhileLoop _while(JExpression test)
public JSwitch _switch(JExpression test)
public JDoLoop _do(JExpression test)
public JTryBlock _try()
public void _return()
public void _return(JExpression exp)
public void _throw(JExpression exp)
public void _break()
public void _break(JLabel label)
public JLabel label(String name)
continue
and break
statements.public void _continue(JLabel label)
public void _continue()
public JBlock block()
public JStatement directStatement(String source)
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;")
.
public void generate(JFormatter f)
generate
in interface JGenerable
public JForEach forEach(JType varType, String name, JExpression collection)
public void state(JFormatter f)
state
in interface JStatement
Copyright © 2019 JBoss by Red Hat. All rights reserved.