public class JBlock extends Object implements IJGenerable, IJStatement
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.
| Modifier and Type | Field and Description |
|---|---|
static boolean |
DEFAULT_BRACES_REQUIRED |
static boolean |
DEFAULT_INDENT_REQUIRED |
static boolean |
DEFAULT_VIRTUAL_BLOCK |
protected List<Object> |
m_aContentList
Declarations and statements contained in this block.
|
| Modifier | Constructor and Description |
|---|---|
|
JBlock() |
protected |
JBlock(boolean bBracesRequired,
boolean bIndentRequired)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
JBreak |
_break()
Create a break statement without a label and add it to this block
|
JBreak |
_break(JLabel aLabel)
Create a break statement with an optional label and add it to this block
|
JContinue |
_continue()
Create a continue statement without a label and add it to this block
|
JContinue |
_continue(JLabel aLabel)
Create a continue statement with an optional label and add it to this block
|
JDoLoop |
_do(IJExpression 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(IJExpression aTestExpr)
Create an If statement and add it to this block
|
JConditional |
_if(IJExpression aTestExpr,
IJStatement aThen)
Create an If statement with the respective then statement and add it to
this block
|
JConditional |
_if(IJExpression aTestExpr,
IJStatement aThen,
IJStatement aElse)
Create an If statement with the respective then and else statements and add
it to this block
|
protected <T> T |
_insert(T aStatementOrDeclaration) |
JInvocation |
_new(AbstractJClass c) |
JInvocation |
_new(AbstractJType t) |
JReturn |
_return()
Create a return statement and add it to this block
|
JReturn |
_return(IJExpression aExpr)
Create a return statement and add it to this block
|
JSwitch |
_switch(IJExpression test)
Create a switch/case statement and add it to this block
|
JThrow |
_throw(IJExpression aExpr)
Create a throw statement and add it to this block
|
JTryBlock |
_try()
Create a Try statement and add it to this block
|
JWhileLoop |
_while(IJExpression test)
Create a While statement and add it to this block
|
JBlock |
add(IJStatement s)
Adds a statement to this block
|
JBlock |
assign(IJAssignmentTarget lhs,
IJExpression exp)
Creates an assignment statement and adds it to this block.
|
JBlock |
assignDivide(IJAssignmentTarget lhs,
IJExpression exp) |
JBlock |
assignMinus(IJAssignmentTarget lhs,
IJExpression exp) |
JBlock |
assignPlus(IJAssignmentTarget lhs,
IJExpression exp) |
JBlock |
assignTimes(IJAssignmentTarget lhs,
IJExpression exp) |
JBlock |
block()
Create a sub-block and add it to this block.
|
JBlock |
block(boolean bBracesRequired,
boolean bIndentRequired)
Create a sub-block and add it to this block
|
JBlock |
blockSimple()
Create a sub-block and add it to this block.
|
JBlock |
blockVirtual()
Create a sub-block and add it to this block.
|
boolean |
bracesRequired() |
JBlock |
bracesRequired(boolean bBracesRequired) |
JVar |
decl(AbstractJType type,
String name)
Adds a local variable declaration to this block
|
JVar |
decl(AbstractJType type,
String name,
IJExpression init)
Adds a local variable declaration to this block
|
JVar |
decl(int mods,
AbstractJType type,
String name)
Adds a local variable declaration to this block
|
JVar |
decl(int mods,
AbstractJType type,
String name,
IJExpression init)
Adds a local variable declaration to this block
|
IJStatement |
directStatement(String sSource)
Creates a "literal" statement directly.
|
JForEach |
forEach(AbstractJType aVarType,
String sName,
IJExpression aCollection)
Creates an enhanced For statement based on j2se 1.5 JLS and add it to this
block
|
void |
generate(JFormatter f) |
List<Object> |
getContents() |
boolean |
indentRequired() |
JBlock |
indentRequired(boolean bIndentRequired) |
JBlock |
insertBefore(JVar var,
Object before) |
JInvocation |
invoke(IJExpression expr,
JMethod method)
Creates an invocation statement and adds it to this block.
|
JInvocation |
invoke(IJExpression expr,
String method)
Creates an invocation statement and adds it to this block.
|
JInvocation |
invoke(JMethod aMethod)
Creates an invocation statement and adds it to this block.
|
JInvocation |
invoke(String sMethod)
Creates an invocation statement and adds it to this block.
|
JInvocation |
invokeThis(JMethod method)
Creates an invocation statement and adds it to this block.
|
JInvocation |
invokeThis(String method)
Creates an invocation statement and adds it to this block.
|
boolean |
isEmpty() |
JLabel |
label(String name)
Create a label, which can be referenced from
continue and
break statements. |
int |
pos() |
int |
pos(int nNewPos)
Sets the current position.
|
void |
remove(int index) |
void |
remove(Object o) |
void |
removeAll()
Remove all elements.
|
int |
size() |
void |
state(JFormatter f) |
JInvocation |
staticInvoke(AbstractJClass aType,
String sMethod)
Creates a static invocation statement.
|
JSynchronizedBlock |
synchronizedBlock(IJExpression aExpr)
Create a synchronized block statement and add it to this block
|
boolean |
virtual() |
JBlock |
virtual(boolean bVirtualBlock)
Mark this block virtual or not.
|
public static final boolean DEFAULT_VIRTUAL_BLOCK
public static final boolean DEFAULT_BRACES_REQUIRED
public static final boolean DEFAULT_INDENT_REQUIRED
protected final List<Object> m_aContentList
IJStatement or IJDeclaration.public JBlock()
@Deprecated protected JBlock(boolean bBracesRequired, boolean bIndentRequired)
public boolean virtual()
true if this is a virtual block never emitting braces
or indent. The default is DEFAULT_VIRTUAL_BLOCK@Nonnull public JBlock virtual(boolean bVirtualBlock)
false.bVirtualBlock - true to make this block a virtual block.public boolean bracesRequired()
public boolean indentRequired()
@Nonnull public List<Object> getContents()
IJStatements and IJDeclaration
in this block.public void remove(Object o)
public void remove(@Nonnegative int index)
public void removeAll()
@Nonnegative public int pos()
pos(int)@Nonnegative public int pos(@Nonnegative int nNewPos)
nNewPos - The new position to setIllegalArgumentException - if the new position value is illegal.pos()public boolean isEmpty()
true if this block is empty and does not contain any
statement.@Nonnegative public int size()
@Nonnull public JVar decl(@Nonnull AbstractJType type, @Nonnull String name)
type - JType of the variablename - Name of the variableJVar@Nonnull public JVar decl(int mods, @Nonnull AbstractJType type, @Nonnull String name)
mods - Modifiers for the variabletype - JType of the variablename - Name of the variableJVar@Nonnull public JVar decl(@Nonnull AbstractJType type, @Nonnull String name, @Nullable IJExpression init)
type - JType of the variablename - Name of the variableinit - Initialization expression for this variable. May be null.JVar@Nonnull public JVar decl(int mods, @Nonnull AbstractJType type, @Nonnull String name, @Nullable IJExpression init)
mods - Modifiers for the variabletype - JType of the variablename - Name of the variableinit - Initialization expression for this variable. May be null.JVar@Nonnull public JBlock assign(@Nonnull IJAssignmentTarget lhs, @Nonnull IJExpression exp)
lhs - Assignable variable or field for left hand side of expressionexp - Right hand side expression@Nonnull public JBlock assignPlus(@Nonnull IJAssignmentTarget lhs, @Nonnull IJExpression exp)
@Nonnull public JBlock assignMinus(@Nonnull IJAssignmentTarget lhs, @Nonnull IJExpression exp)
@Nonnull public JBlock assignTimes(@Nonnull IJAssignmentTarget lhs, @Nonnull IJExpression exp)
@Nonnull public JBlock assignDivide(@Nonnull IJAssignmentTarget lhs, @Nonnull IJExpression exp)
@Nonnull public JInvocation invoke(@Nonnull IJExpression expr, @Nonnull String method)
expr - IJExpression evaluating to the class or object upon which
the named method will be invokedmethod - Name of method to invokeJInvocation@Nonnull public JInvocation invokeThis(@Nonnull String method)
method - Name of method to invoke on thisJInvocation@Nonnull public JInvocation invoke(@Nonnull IJExpression expr, @Nonnull JMethod method)
expr - IJExpression evaluating to the class or object upon which
the method will be invokedmethod - JMethod to invokeJInvocation@Nonnull public JInvocation invokeThis(@Nonnull JMethod method)
method - JMethod to invoke on thisJInvocation@Nonnull public JInvocation staticInvoke(@Nonnull AbstractJClass aType, @Nonnull String sMethod)
aType - Type upon which the method should be invokedsMethod - Name of method to invokeJInvocation@Nonnull public JInvocation invoke(@Nonnull String sMethod)
sMethod - Name of method to invokeJInvocation@Nonnull public JInvocation invoke(@Nonnull JMethod aMethod)
aMethod - JMethod to invokeJInvocation@Nonnull public JInvocation _new(@Nonnull AbstractJClass c)
@Nonnull public JInvocation _new(@Nonnull AbstractJType t)
@Nonnull public JBlock add(@Nonnull IJStatement s)
s - JStatement to be added@Nonnull public JConditional _if(@Nonnull IJExpression aTestExpr)
aTestExpr - JExpression to be tested to determine branching@Nonnull public JConditional _if(@Nonnull IJExpression aTestExpr, @Nonnull IJStatement aThen)
aTestExpr - JExpression to be tested to determine branchingaThen - The then-block. May not be null.@Nonnull public JConditional _if(@Nonnull IJExpression aTestExpr, @Nonnull IJStatement aThen, @Nonnull IJStatement aElse)
aTestExpr - JExpression to be tested to determine branchingaThen - The then-block. May not be null.aElse - The else-block. May not be null.@Nonnull public JForLoop _for()
null.@Nonnull public JWhileLoop _while(@Nonnull IJExpression test)
test - Test expression for the while statementJWhileLoop statement@Nonnull public JSwitch _switch(@Nonnull IJExpression test)
test - Test expression for the switch statementJSwitch@Nonnull public JDoLoop _do(@Nonnull IJExpression test)
test - Test expression for the while statementJDoLoop statement@Nonnull public JTryBlock _try()
JTryBlock statement@Nonnull public JReturn _return()
JReturn statement@Nonnull public JReturn _return(@Nullable IJExpression aExpr)
aExpr - Expression to be returned. May be null.JReturn statement@Nonnull public JThrow _throw(@Nonnull IJExpression aExpr)
aExpr - Expression to be thrownJThrow@Nonnull public JBreak _break()
JBreak@Nonnull public JBreak _break(@Nullable JLabel aLabel)
aLabel - Optional label for the break statementJBreak@Nonnull public JLabel label(@Nonnull String name)
continue and
break statements.name - Label nameJLabel@Nonnull public JContinue _continue()
JContinue@Nonnull public JContinue _continue(@Nullable JLabel aLabel)
aLabel - Optional label statement.JContinue@Nonnull public JBlock block()
JBlockblock(boolean, boolean),
blockSimple()@Nonnull public JBlock blockSimple()
JBlockblock(),
block(boolean, boolean)@Nonnull public JBlock blockVirtual()
JBlockblock(),
block(boolean, boolean)@Nonnull public JBlock block(boolean bBracesRequired, boolean bIndentRequired)
bBracesRequired - true if braces should be requiredbIndentRequired - true if indentation is requiredJBlockblock(),
blockSimple()@Nonnull public JForEach forEach(@Nonnull AbstractJType aVarType, @Nonnull String sName, @Nonnull IJExpression aCollection)
aVarType - Variable typesName - Variable nameaCollection - Collection to be iterated@Nonnull public JSynchronizedBlock synchronizedBlock(@Nonnull IJExpression aExpr)
aExpr - The expression to synchronize on. May not be null.null.@Nonnull public IJStatement directStatement(@Nonnull String sSource)
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;").
sSource - The source code to state. May not be null.public void generate(@Nonnull JFormatter f)
generate in interface IJGenerablepublic void state(@Nonnull JFormatter f)
state in interface IJStatementCopyright © 2013–2015 Philip Helger. All rights reserved.