Package org.codehaus.groovy.ast.stmt

# Class SwitchStatement

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.stmt.Statement
org.codehaus.groovy.ast.stmt.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 Summary

  Constructors
  Constructor
  Description
  `SwitchStatement(Expression expression)`
  Constructs a switch statement with the given control expression.
  `SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)`
  Constructs a switch statement with the given control expression, case statements, and default statement.
  `SwitchStatement(Expression expression, Statement defaultStatement)`
  Constructs a switch statement with the given control expression and default statement.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addCase(CaseStatement caseStatement)`
  Adds a case statement to this switch.
  `CaseStatement`
  `getCaseStatement(int idx)`
  Returns the case statement at the given index.
  `List<CaseStatement>`
  `getCaseStatements()`
  Returns the list of case statements in this switch.
  `Statement`
  `getDefaultStatement()`
  Returns the statement executed when no case matches the control expression.
  `Expression`
  `getExpression()`
  Returns the control expression that is evaluated against case values.
  `void`
  `setDefaultStatement(Statement defaultStatement)`
  Sets the statement executed when no case matches the control expression.
  `void`
  `setExpression(Expression e)`
  Sets the control expression that is evaluated against case values.
  `void`
  `visit(GroovyCodeVisitor visitor)`
  Accepts a code visitor for AST traversal and transformation.

  ### Methods inherited from class org.codehaus.groovy.ast.stmt.Statement

  `addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabel`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## 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(Expression, Statement)`

  - ### 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(Expression, List, Statement)`

  - ### 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