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.
| Fields inherited from class | Fields |
|---|---|
class Expression |
EMPTY_ARRAY |
| 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. |
| 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) |
| 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 |
Constructs a switch expression with the given selector. The default statement is initialized to EmptyStatement.INSTANCE.
expression - the selector expressionConstructs a switch expression with the given selector and default arm.
expression - the selector expressiondefaultStatement - the arm executed when no case matches; may be EmptyStatement.INSTANCEConstructs a switch expression with the given selector, case arms, and default arm.
expression - the selector expressioncaseStatements - the case armsdefaultStatement - the arm executed when no case matchesAdds a case arm to this switch expression.
caseStatement - the CaseStatement to addReturns the case arms of this switch expression.
Returns the arm executed when no case matches.
Returns the selector expression matched against case values.
Sets the arm executed when no case matches.
defaultStatement - the default StatementSets the selector expression matched against case values.
expression - the selector ExpressionReturns a copy whose selector and case labels have been rewritten. Arm statements are shared: an ExpressionTransformer does not walk statements.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.