Class BlockStatement

All Implemented Interfaces:
NodeMetaDataHandler

public class BlockStatement extends Statement
Represents a compound statement consisting of an ordered sequence of Statements within a specific VariableScope. A block statement is the fundamental grouping mechanism in the Groovy AST and is used to represent the body of methods, classes, loops, conditionals, try-catch blocks, and any other scoped code region.
See Also:
  • Constructor Details

    • BlockStatement

      public BlockStatement()
      Constructs an empty BlockStatement with a new default scope.
    • BlockStatement

      public BlockStatement(Statement[] statements, VariableScope scope)
      Constructs a BlockStatement with an array of statements and a variable scope. The provided array is copied internally; subsequent modifications to the array will not affect this BlockStatement.
      Parameters:
      statements - an array of Statements to include in this block; must not be null or a NullPointerException will be raised
      scope - the VariableScope for this block, typically containing local variable declarations and type information
    • BlockStatement

      public BlockStatement(List<Statement> statements, VariableScope scope)
      Constructs a BlockStatement with a list of statements and a variable scope. A reference to the provided list is maintained; modifications to the list after construction will be reflected in this BlockStatement.
      Parameters:
      statements - a List of Statements to include in this block; pass an empty list, not null, to avoid NullPointerException later
      scope - the VariableScope for this block, containing variable binding and type metadata
  • Method Details

    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
    • getStatements

      public List<Statement> getStatements()
      Returns the list of Statements in this block.
      Returns:
      an unmodifiable view or direct reference to the statements list
    • addStatement

      public void addStatement(Statement statement)
      Appends a Statement to this block.
      Parameters:
      statement - the Statement to add; must not be null
    • addStatements

      public void addStatements(List<Statement> listOfStatements)
      Appends all Statements from the provided list to this block.
      Parameters:
      listOfStatements - a List of Statements to append; must not be null
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isEmpty

      public boolean isEmpty()
      Checks whether this block contains no statements.
      Overrides:
      isEmpty in class Statement
      Returns:
      true if the statement list is empty, false otherwise
    • getVariableScope

      public VariableScope getVariableScope()
      Returns the VariableScope associated with this block, which contains variable declarations, type information, and scope metadata.
      Returns:
      the VariableScope for this block
    • setVariableScope

      public void setVariableScope(VariableScope scope)
      Sets the VariableScope for this block.
      Parameters:
      scope - the VariableScope to associate with this block