Top Description Fields Constructors Methods
com.sun.codemodel

public final Class JBlock

extends Object
implements JGenerable, JStatement
Class Inheritance
All Implemented Interfaces
com.sun.codemodel.JStatement, com.sun.codemodel.JGenerable
Imports
java.util.ArrayList, .List, .Collections

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.

Field Summary

Modifier and TypeField and Description
private boolean
bracesRequired

Whether or not this block must be braced and indented

private final List<Object>
content

Declarations and statements contained in this block.

private boolean
private int
pos

Current position.

Constructor Summary

AccessConstructor and Description
public
public
JBlock(boolean bracesRequired, boolean indentRequired)

Method Summary

Modifier and TypeMethod and Description
public void
_break()

Create a break statement and add it to this block

public void
_break(JLabel label)

public void
_continue(JLabel label)

Create a continue statement and add it to this block

public void
public JDoLoop

Returns:

Newly generated Do statement
_do
(JExpression test)

Create a Do statement and add it to this block

public JForLoop

Returns:

Newly generated For statement
_for
()

Create a For statement and add it to this block

public JConditional

Returns:

Newly generated conditional statement
_if
(JExpression
JExpression to be tested to determine branching
expr
)

Create an If statement and add it to this block

public void
_return()

Create a return statement and add it to this block

public void
_return(JExpression exp)

Create a return statement and add it to this block

public JSwitch
_switch(JExpression test)

Create a switch/case statement and add it to this block

public void
_throw(JExpression exp)

Create a throw statement and add it to this block

public JTryBlock

Returns:

Newly generated Try statement
_try
()

Create a Try statement and add it to this block

public JWhileLoop

Returns:

Newly generated While statement
_while
(JExpression test)

Create a While statement and add it to this block

public JBlock

Returns:

This block
add
(JStatement
JStatement to be added
s
)

Adds a statement to this block

public JBlock
assign(JAssignmentTarget
Assignable variable or field for left hand side of expression
lhs
,
JExpression
Right hand side expression
exp
)

Creates an assignment statement and adds it to this block.

public JBlock
public JBlock
block()

Create a sub-block and add it to this block

public JVar

Returns:

Newly generated JVar
decl
(JType
JType of the variable
type
,
String
Name of the variable
name
)

Adds a local variable declaration to this block

public JVar

Returns:

Newly generated JVar
decl
(JType
JType of the variable
type
,
String
Name of the variable
name
,
JExpression
Initialization expression for this variable. May be null.
init
)

Adds a local variable declaration to this block

public JVar

Returns:

Newly generated JVar
decl
(int
Modifiers for the variable
mods
,
JType
JType of the variable
type
,
String
Name of the variable
name
,
JExpression
Initialization expression for this variable. May be null.
init
)

Adds a local variable declaration to this block

public JStatement
directStatement(final String source)

Creates a "literal" statement directly.

public JForEach

Returns:

Newly generated enhanced For statement per j2se 1.5 specification
forEach
(JType varType, String name, JExpression collection)

Creates an enhanced For statement based on j2se 1.5 JLS and add it to this block

public void
pack-priv void
public List<Object>
getContents()

Returns a read-only view of JStatements and JDeclaration in this block.

private <T> T
insert(T statementOrDeclaration)

public JInvocation

Returns:

Newly generated JInvocation
invoke
(JExpression
JExpression evaluating to the class or object upon which the named method will be invoked
expr
,
String
Name of method to invoke
method
)

Creates an invocation statement and adds it to this block.

public JInvocation

Returns:

Newly generated JInvocation
invoke
(JExpression
JExpression evaluating to the class or object upon which the method will be invoked
expr
,
JMethod
JMethod to invoke
method
)

Creates an invocation statement and adds it to this block.

public JInvocation

Returns:

Newly generated JInvocation
invoke
(String
Name of method to invoke
method
)

Creates an invocation statement and adds it to this block.

public JInvocation

Returns:

Newly generated JInvocation
invoke
(JMethod
JMethod to invoke
method
)

Creates an invocation statement and adds it to this block.

public boolean
isEmpty()

Returns true if this block is empty and does not contain any statement.

public JLabel
label(String name)

Create a label, which can be referenced from continue and break statements.

public int
pos()

Gets the current position to which new statements will be inserted.

public int

Returns:

the old value of the current position.
pos
(int newPos)

Sets the current position.

public void
public JInvocation
staticInvoke(JClass type, String method)

Creates a static invocation statement.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bracesRequiredback to summary
private boolean bracesRequired

Whether or not this block must be braced and indented

contentback to summary
private final List<Object> content

Declarations and statements contained in this block. Either JStatement or JDeclaration.

indentRequiredback to summary
private boolean indentRequired
posback to summary
private int pos

Current position.

Constructor Detail

JBlockback to summary
public JBlock()
JBlockback to summary
public JBlock(boolean bracesRequired, boolean indentRequired)

Method Detail

_breakback to summary
public void _break()

Create a break statement and add it to this block

_breakback to summary
public void _break(JLabel label)
_continueback to summary
public void _continue(JLabel label)

Create a continue statement and add it to this block

_continueback to summary
public void _continue()
_doback to summary
public JDoLoop _do(JExpression test)

Create a Do statement and add it to this block

Returns:JDoLoop

Newly generated Do statement

_forback to summary
public JForLoop _for()

Create a For statement and add it to this block

Returns:JForLoop

Newly generated For statement

_ifback to summary
public JConditional _if(JExpression expr)

Create an If statement and add it to this block

Parameters
expr:JExpression

JExpression to be tested to determine branching

Returns:JConditional

Newly generated conditional statement

_returnback to summary
public void _return()

Create a return statement and add it to this block

_returnback to summary
public void _return(JExpression exp)

Create a return statement and add it to this block

_switchback to summary
public JSwitch _switch(JExpression test)

Create a switch/case statement and add it to this block

_throwback to summary
public void _throw(JExpression exp)

Create a throw statement and add it to this block

_tryback to summary
public JTryBlock _try()

Create a Try statement and add it to this block

Returns:JTryBlock

Newly generated Try statement

_whileback to summary
public JWhileLoop _while(JExpression test)

Create a While statement and add it to this block

Returns:JWhileLoop

Newly generated While statement

addback to summary
public JBlock add(JStatement s)

Adds a statement to this block

Parameters
s:JStatement

JStatement to be added

Returns:JBlock

This block

assignback to summary
public JBlock assign(JAssignmentTarget lhs, JExpression exp)

Creates an assignment statement and adds it to this block.

Parameters
lhs:JAssignmentTarget

Assignable variable or field for left hand side of expression

exp:JExpression

Right hand side expression

assignPlusback to summary
public JBlock assignPlus(JAssignmentTarget lhs, JExpression exp)
blockback to summary
public JBlock block()

Create a sub-block and add it to this block

declback to summary
public JVar decl(JType type, String name)

Adds a local variable declaration to this block

Parameters
type:JType

JType of the variable

name:String

Name of the variable

Returns:JVar

Newly generated JVar

declback to summary
public JVar decl(JType type, String name, JExpression init)

Adds a local variable declaration to this block

Parameters
type:JType

JType of the variable

name:String

Name of the variable

init:JExpression

Initialization expression for this variable. May be null.

Returns:JVar

Newly generated JVar

declback to summary
public JVar decl(int mods, JType type, String name, JExpression init)

Adds a local variable declaration to this block

Parameters
mods:int

Modifiers for the variable

type:JType

JType of the variable

name:String

Name of the variable

init:JExpression

Initialization expression for this variable. May be null.

Returns:JVar

Newly generated JVar

directStatementback to summary
public JStatement directStatement(final 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;").

forEachback to summary
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:JForEach

Newly generated enhanced For statement per j2se 1.5 specification

generateback to summary
public void generate(JFormatter f)

Implements com.sun.codemodel.JGenerable.generate.

Annotations
@Override
generateBodyback to summary
pack-priv void generateBody(JFormatter f)
getContentsback to summary
public List<Object> getContents()

Returns a read-only view of JStatements and JDeclaration in this block.

insertback to summary
private <T> T insert(T statementOrDeclaration)
invokeback to summary
public JInvocation invoke(JExpression expr, String method)

Creates an invocation statement and adds it to this block.

Parameters
expr:JExpression

JExpression evaluating to the class or object upon which the named method will be invoked

method:String

Name of method to invoke

Returns:JInvocation

Newly generated JInvocation

invokeback to summary
public JInvocation invoke(JExpression expr, JMethod method)

Creates an invocation statement and adds it to this block.

Parameters
expr:JExpression

JExpression evaluating to the class or object upon which the method will be invoked

method:JMethod

JMethod to invoke

Returns:JInvocation

Newly generated JInvocation

invokeback to summary
public JInvocation invoke(String method)

Creates an invocation statement and adds it to this block.

Parameters
method:String

Name of method to invoke

Returns:JInvocation

Newly generated JInvocation

invokeback to summary
public JInvocation invoke(JMethod method)

Creates an invocation statement and adds it to this block.

Parameters
method:JMethod

JMethod to invoke

Returns:JInvocation

Newly generated JInvocation

isEmptyback to summary
public boolean isEmpty()

Returns true if this block is empty and does not contain any statement.

labelback to summary
public JLabel label(String name)

Create a label, which can be referenced from continue and break statements.

posback to summary
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)
posback to summary
public int pos(int newPos)

Sets the current position.

Returns:int

the old value of the current position.

Exceptions
IllegalArgumentException:
if the new position value is illegal.
See Also
pos()
stateback to summary
public void state(JFormatter f)

Implements com.sun.codemodel.JStatement.state.

Annotations
@Override
staticInvokeback to summary
public JInvocation staticInvoke(JClass type, String method)

Creates a static invocation statement.