Class 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 Detail

      • JBlock

        public JBlock()
      • JBlock

        public JBlock​(boolean bracesRequired,
                      boolean indentRequired)
    • Method Detail

      • 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
      • 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;").

      • 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