Package: org.codehaus.groovy.ast.expr

## [Java] Class SwitchExpression

- org.codehaus.groovy.ast.expr.SwitchExpression

-     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 CaseStatements, the same way a ClosureExpression holds a statement body: the case label is an expression, the arm is a statement. transformExpression rewrites the selector and case labels only and shares the arm statements.

  See Also:
    SwitchStatement
    YieldStatement
    CaseStatement
  Since:
    6.0.0

- - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class Expression` | `EMPTY_ARRAY`

  - ### Constructor Summary

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

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addCase(CaseStatement caseStatement)` Adds a case arm to this switch expression.
    `` | `public List<CaseStatement>` | `getCaseStatements()` Returns the case arms of this switch expression.
    `` | `public Statement` | `getDefaultStatement()` Returns the arm executed when no case matches.
    `` | `public Expression` | `getExpression()` Returns the selector expression matched against case values.
    `` | `public String` | `getText()`
    `` | `public void` | `setDefaultStatement(Statement defaultStatement)` Sets the arm executed when no case matches.
    `` | `public void` | `setExpression(Expression expression)` Sets the selector expression matched against case values.
    `` | `public String` | `toString()`
    `` | `public Expression` | `transformExpression(ExpressionTransformer transformer)` Returns a copy whose selector and case labels have been rewritten.
    `` | `public void` | `visit(GroovyCodeVisitor visitor)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Expression` | `getType, setType, transformExpression, transformExpressions, transformExpressions`
    `class AnnotatedNode` | `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Constructor Detail

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

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

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

    - #### public void addCase(CaseStatement caseStatement)

      Adds a case arm to this switch expression.

      Parameters:
        `caseStatement` - the CaseStatement to add

    - #### public List<CaseStatement> getCaseStatements()

      Returns the case arms of this switch expression.

      Returns:
        a list of CaseStatement objects; never null

    - #### public Statement getDefaultStatement()

      Returns the arm executed when no case matches.

      Returns:
        the default Statement, or EmptyStatement.INSTANCE if not set

    - #### public Expression getExpression()

      Returns the selector expression matched against case values.

      Returns:
        the selector Expression

    - #### public String getText()

    - #### public void setDefaultStatement(Statement defaultStatement)

      Sets the arm executed when no case matches.

      Parameters:
        `defaultStatement` - the default Statement

    - #### public void setExpression(Expression expression)

      Sets the selector expression matched against case values.

      Parameters:
        `expression` - the selector Expression

    - #### public String toString()

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

    - #### public void visit(GroovyCodeVisitor visitor)