Class 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 CaseStatements, 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:
  • 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