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
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:
-
Field Summary
Fields inherited from class org.codehaus.groovy.ast.expr.Expression
EMPTY_ARRAYFields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
ConstructorsConstructorDescriptionSwitchExpression(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 TypeMethodDescriptionvoidaddCase(CaseStatement caseStatement) Adds a case arm to this switch expression.Returns the case arms of this switch expression.Returns the arm executed when no case matches.Returns the selector expression matched against case values.getText()Returns a human-readable text representation of this AST node.voidsetDefaultStatement(Statement defaultStatement) Sets the arm executed when no case matches.voidsetExpression(Expression expression) Sets the selector expression matched against case values.toString()transformExpression(ExpressionTransformer transformer) Returns a copy whose selector and case labels have been rewritten.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.expr.Expression
getType, setType, transformExpressions, transformExpressionsMethods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Constructor Details
-
SwitchExpression
Constructs a switch expression with the given selector. The default statement is initialized toEmptyStatement.INSTANCE.- Parameters:
expression- the selector expression
-
SwitchExpression
Constructs a switch expression with the given selector and default arm.- Parameters:
expression- the selector expressiondefaultStatement- the arm executed when no case matches; may beEmptyStatement.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 expressioncaseStatements- the case armsdefaultStatement- the arm executed when no case matches
-
-
Method Details
-
getExpression
Returns the selector expression matched against case values.- Returns:
- the selector
Expression
-
setExpression
Sets the selector expression matched against case values.- Parameters:
expression- the selectorExpression
-
getCaseStatements
Returns the case arms of this switch expression.- Returns:
- a list of
CaseStatementobjects; never null
-
getDefaultStatement
Returns the arm executed when no case matches.- Returns:
- the default
Statement, orEmptyStatement.INSTANCEif not set
-
setDefaultStatement
Sets the arm executed when no case matches.- Parameters:
defaultStatement- the defaultStatement
-
addCase
Adds a case arm to this switch expression.- Parameters:
caseStatement- theCaseStatementto add
-
getText
Description copied from class:ASTNodeReturns 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. -
toString
-
transformExpression
Returns a copy whose selector and case labels have been rewritten. Arm statements are shared: anExpressionTransformerdoes not walk statements.- Specified by:
transformExpressionin classExpression- Parameters:
transformer- theExpressionTransformerto apply- Returns:
- a transformed copy of this expression (or this expression itself if no changes are needed)
-
visit
Description copied from class:ASTNodeAccepts 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:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-