Class BlockBuilder


public class BlockBuilder extends StatementBuilderBase
Block builder. This wraps the AST block representation with convenience methods and added control information.
  • Field Details

    • m_block

      private final Block m_block
      Compilation unit.
  • Constructor Details

    • BlockBuilder

      public BlockBuilder(ClassBuilder source, Block block)
      Constructor.
      Parameters:
      source -
      block -
  • Method Details

    • getStatement

      Statement getStatement()
      Get the statement.
      Specified by:
      getStatement in class StatementBuilderBase
      Returns:
      statement
    • addAssignToName

      public void addAssignToName(Expression expr, String name)
      Append an assignment from an expression to a field or local variable.
      Parameters:
      expr -
      name -
    • addAssignVariableToField

      public void addAssignVariableToField(String vname, String fname)
      Append an assignment from a local variable to a field. This handles the case where the local variable name is the same as the field name.
      Parameters:
      vname -
      fname -
    • addLocalVariableDeclaration

      public void addLocalVariableDeclaration(String type, String vname)
      Append a local variable declaration.
      Parameters:
      type -
      vname -
    • addLocalVariableDeclaration

      public void addLocalVariableDeclaration(Type type, String vname, ExpressionBuilderBase expr)
      Append a local variable declaration with initializer expression. This variation takes the actual type as a parameter.
      Parameters:
      type -
      vname -
      expr - initializer expression
    • addIfStatement

      public void addIfStatement(ExpressionBuilderBase expr, BlockBuilder ifblock)
      Append a simple 'if' statement (no else).
      Parameters:
      expr - conditional expression
      ifblock - block executed when condition true
    • addIfElseStatement

      public void addIfElseStatement(ExpressionBuilderBase expr, BlockBuilder ifblock, BlockBuilder elseblock)
      Append an 'if-else' statement.
      Parameters:
      expr - conditional expression
      ifblock - block executed when condition true
      elseblock - block executed when condition false
    • addIfElseIfStatement

      public void addIfElseIfStatement(ExpressionBuilderBase ifexpr, ExpressionBuilderBase elsexpr, BlockBuilder ifblock, BlockBuilder elseblock)
      Append an 'if-else-if' statement.
      Parameters:
      ifexpr - if conditional expression
      elsexpr - if conditional expression
      ifblock - block executed when condition true
      elseblock - block executed when condition false
    • addForStatement

      private void addForStatement(String name, Type type, Expression init, Expression test, Expression post, BlockBuilder block)
      Append a three-part 'for' statement with an associated variable. This assumes the first part is a local variable declaration with an initializer expression, while the other two parts are just expressions.
      Parameters:
      name - iteration variable name
      type - variable type
      init - variable initialization expression
      test - loop test expression (second part of 'for')
      post - post-loop expression (optional third part of 'for', null if none)
      block - statement body block
    • addIteratedForStatement

      public void addIteratedForStatement(String name, Type type, ExpressionBuilderBase init, BlockBuilder block)
      Append a standard 'for' statement using an iterator.
      Parameters:
      name - iteration variable name
      type - variable type (must be an iterator subclass or generic type)
      init - variable initialization expression
      block - statement body block
    • addIndexedForStatement

      public void addIndexedForStatement(String name, String array, BlockBuilder block)
      Append a standard 'for' statement using an index variable over an array. The index is always initialized to '0', and incremented each time the loop is executed until the size of the array is reached.
      Parameters:
      name - index variable name
      array - array name
      block - statement body block
    • addSugaredForStatement

      public void addSugaredForStatement(String name, String type, ExpressionBuilderBase expr, BlockBuilder block)
      Append a Java 5 "enhanced" 'for' statement.
      Parameters:
      name - iteration variable name
      type - iteration variable type
      expr - iteration source expression
      block - statement body block
    • addReturnExpression

      public void addReturnExpression(ExpressionBuilderBase expr)
      Append a statement returning the value of an expression.
      Parameters:
      expr - expression
    • addReturnNamed

      public void addReturnNamed(String name)
      Append a statement returning the value of a field or local variable.
      Parameters:
      name - field name
    • addReturnNull

      public void addReturnNull()
      Append a statement returning null.
    • addThrowException

      public void addThrowException(String type, String text)
      Append a throw new exception statement.
      Parameters:
      type - exception type
      text -
    • addThrowException

      public void addThrowException(String type, ExpressionBuilderBase expr)
      Append a throw new exception statement.
      Parameters:
      type - exception type
      expr - initializer expression
    • addCall

      public void addCall(InvocationBuilder call)
      Append a method call statement.
      Parameters:
      call -
    • addBreak

      public void addBreak()
      Append a 'break' statement.
    • addSwitch

      public SwitchBuilder addSwitch(String name)
      Append a 'switch' statement using a local variable or field name as the switch value.
      Parameters:
      name -
      Returns:
      statement builder
    • addSwitch

      public SwitchBuilder addSwitch(ExpressionBuilderBase expr)
      Append a 'switch' statement using a constructed expression as the switch value.
      Parameters:
      expr -
      Returns:
      statement builder
    • addExpressionStatement

      public void addExpressionStatement(ExpressionBuilderBase expr)
      Append an expression statement.
      Parameters:
      expr -
    • addStatement

      public void addStatement(StatementBuilderBase stmt)
      Append a constructed statement.
      Parameters:
      stmt -