Package org.codehaus.groovy.ast.expr

# Class SwitchExpression

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.SwitchExpression

All Implemented Interfaces:
  `GroovydocHolder<AnnotatedNode>`, `NodeMetaDataHandler`

public class SwitchExpression extends Expression
Represents a `switch` used as an expression, as specified by JEP 361 (Switch Expressions). The selector is evaluated once and matched against the `CaseStatement` list using Groovy's `isCase` semantics (or a tableswitch / lookupswitch when the compiler can prove that is equivalent). Each completing arm yields a value via `YieldStatement`; the expression's result is that value.

Arms stay as `CaseStatement`s, the same way a `ClosureExpression` holds a statement body: the case label is an expression, the arm is a statement. `transformExpression(org.codehaus.groovy.ast.expr.ExpressionTransformer)` rewrites the selector and case labels only and shares the arm statements.

Since:
  6.0.0
See Also:
  - `SwitchStatement`
  - `YieldStatement`
  - `CaseStatement`

- ## Field Summary

  ### Fields inherited from class org.codehaus.groovy.ast.expr.Expression

  `EMPTY_ARRAY`

  ### Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder

  `DOC_COMMENT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SwitchExpression(Expression expression)`
  Constructs a switch expression with the given selector.
  `SwitchExpression(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)`
  Constructs a switch expression with the given selector, case arms, and default arm.
  `SwitchExpression(Expression expression, Statement defaultStatement)`
  Constructs a switch expression with the given selector and default arm.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addCase(CaseStatement caseStatement)`
  Adds a case arm to this switch expression.
  `List<CaseStatement>`
  `getCaseStatements()`
  Returns the case arms of this switch expression.
  `Statement`
  `getDefaultStatement()`
  Returns the arm executed when no case matches.
  `Expression`
  `getExpression()`
  Returns the selector expression matched against case values.
  `String`
  `getText()`
  Returns a human-readable text representation of this AST node.
  `void`
  `setDefaultStatement(Statement defaultStatement)`
  Sets the arm executed when no case matches.
  `void`
  `setExpression(Expression expression)`
  Sets the selector expression matched against case values.
  `String`
  `toString()`
  `Expression`
  `transformExpression(ExpressionTransformer transformer)`
  Returns a copy whose selector and case labels have been rewritten.
  `void`
  `visit(GroovyCodeVisitor visitor)`
  Accepts a code visitor for AST traversal and transformation.

  ### Methods inherited from class org.codehaus.groovy.ast.expr.Expression

  `getType, setType, transformExpressions, transformExpressions`

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

  `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`

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

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

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

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

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

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

- ## Constructor Details

  - ### SwitchExpression

    public SwitchExpression(Expression expression)
    Constructs a switch expression with the given selector. The default statement is initialized to `EmptyStatement.INSTANCE`.
    Parameters:
      `expression` - the selector expression

  - ### SwitchExpression

    public SwitchExpression(Expression expression, Statement defaultStatement)
    Constructs a switch expression with the given selector and default arm.
    Parameters:
      `expression` - the selector expression
      `defaultStatement` - the arm executed when no case matches; may be `EmptyStatement.INSTANCE`

  - ### SwitchExpression

    public SwitchExpression(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)
    Constructs a switch expression with the given selector, case arms, and default arm.
    Parameters:
      `expression` - the selector expression
      `caseStatements` - the case arms
      `defaultStatement` - the arm executed when no case matches

- ## Method Details

  - ### getExpression

    public Expression getExpression()
    Returns the selector expression matched against case values.
    Returns:
      the selector `Expression`

  - ### setExpression

    public void setExpression(Expression expression)
    Sets the selector expression matched against case values.
    Parameters:
      `expression` - the selector `Expression`

  - ### getCaseStatements

    public List<CaseStatement> getCaseStatements()
    Returns the case arms of this switch expression.
    Returns:
      a list of `CaseStatement` objects; never null

  - ### getDefaultStatement

    public Statement getDefaultStatement()
    Returns the arm executed when no case matches.
    Returns:
      the default `Statement`, or `EmptyStatement.INSTANCE` if not set

  - ### setDefaultStatement

    public void setDefaultStatement(Statement defaultStatement)
    Sets the arm executed when no case matches.
    Parameters:
      `defaultStatement` - the default `Statement`

  - ### addCase

    public void addCase(CaseStatement caseStatement)
    Adds a case arm to this switch expression.
    Parameters:
      `caseStatement` - the `CaseStatement` to add

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

  - ### transformExpression

    public Expression transformExpression(ExpressionTransformer transformer)
    Returns a copy whose selector and case labels have been rewritten. Arm statements are shared: an `ExpressionTransformer` does not walk statements.
    Specified by:
      `transformExpression` in class `Expression`
    Parameters:
      `transformer` - the `ExpressionTransformer` to apply
    Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)

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