com.sun.codemodel
Class JBlock

java.lang.Object
  extended by 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.


Constructor Summary
JBlock()
           
JBlock(boolean bracesRequired, boolean indentRequired)
           
 
Method Summary
 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 JStatements 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JBlock

public JBlock()

JBlock

public JBlock(boolean bracesRequired,
              boolean indentRequired)
Method Detail

getContents

public List<Object> getContents()
Returns a read-only view of JStatements and JDeclaration 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 variable
name - 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 variable
name - Name of the variable
init - 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 variable
type - JType of the variable
name - Name of the variable
init - 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 expression
exp - 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 invoked
method - 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 invoked
method - 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 from continue and break 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 interface JGenerable

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 interface JStatement


Copyright © 2015 Oracle Corporation. All Rights Reserved.