Class SwitchStatement

All Implemented Interfaces:
NodeMetaDataHandler

public class SwitchStatement extends Statement
Represents a switch (object) { case value: ... case [1, 2, 3]: ... default: ... } statement in Groovy. A switch statement evaluates the control expression against a sequence of case values and executes the code associated with the matching case, or the default statement if no case matches.
  • Constructor Details

    • SwitchStatement

      public SwitchStatement(Expression expression)
      Constructs a switch statement with the given control expression. The default statement is initialized to EmptyStatement.INSTANCE.
      Parameters:
      expression - the expression to evaluate against case values
      See Also:
    • SwitchStatement

      public SwitchStatement(Expression expression, Statement defaultStatement)
      Constructs a switch statement with the given control expression and default statement.
      Parameters:
      expression - the expression to evaluate against case values
      defaultStatement - the statement executed when no case matches; may be EmptyStatement.INSTANCE
      See Also:
    • SwitchStatement

      public SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)
      Constructs a switch statement with the given control expression, case statements, and default statement.
      Parameters:
      expression - the expression to evaluate against case values
      caseStatements - the list of CaseStatement objects representing case branches
      defaultStatement - the statement executed when no case matches
  • 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
    • getCaseStatements

      public List<CaseStatement> getCaseStatements()
      Returns the list of case statements in this switch.
      Returns:
      a list of CaseStatement objects; never null
    • getExpression

      public Expression getExpression()
      Returns the control expression that is evaluated against case values.
      Returns:
      the control Expression
    • setExpression

      public void setExpression(Expression e)
      Sets the control expression that is evaluated against case values.
      Parameters:
      e - the control Expression
    • getDefaultStatement

      public Statement getDefaultStatement()
      Returns the statement executed when no case matches the control expression.
      Returns:
      the default Statement, or EmptyStatement.INSTANCE if not set
    • setDefaultStatement

      public void setDefaultStatement(Statement defaultStatement)
      Sets the statement executed when no case matches the control expression.
      Parameters:
      defaultStatement - the default Statement
    • addCase

      public void addCase(CaseStatement caseStatement)
      Adds a case statement to this switch.
      Parameters:
      caseStatement - the CaseStatement to add
    • getCaseStatement

      public CaseStatement getCaseStatement(int idx)
      Returns the case statement at the given index.
      Parameters:
      idx - the index of the case statement to retrieve
      Returns:
      the CaseStatement at the given index, or null if the index is out of bounds