Package org.codehaus.groovy.ast.stmt
Class SwitchStatement
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.stmt.Statement
org.codehaus.groovy.ast.stmt.SwitchStatement
- All Implemented Interfaces:
NodeMetaDataHandler
Represents a switch (object) { case value: ... case [1, 2, 3]: ... default: ... } statement in Groovy.
A switch statement evaluates the control
expression against a sequence of
case values and executes the code associated with the matching case, or the
default statement if no case matches.-
Constructor Summary
ConstructorsConstructorDescriptionSwitchStatement(Expression expression) Constructs a switch statement with the given control expression.SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement) Constructs a switch statement with the given control expression, case statements, and default statement.SwitchStatement(Expression expression, Statement defaultStatement) Constructs a switch statement with the given control expression and default statement. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCase(CaseStatement caseStatement) Adds a case statement to this switch.getCaseStatement(int idx) Returns the case statement at the given index.Returns the list of case statements in this switch.Returns the statement executed when no case matches the control expression.Returns the control expression that is evaluated against case values.voidsetDefaultStatement(Statement defaultStatement) Sets the statement executed when no case matches the control expression.voidSets the control expression that is evaluated against case values.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.stmt.Statement
addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabelMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Constructor Details
-
SwitchStatement
Constructs a switch statement with the given control expression. The default statement is initialized toEmptyStatement.INSTANCE.- Parameters:
expression- the expression to evaluate against case values- See Also:
-
SwitchStatement
Constructs a switch statement with the given control expression and default statement.- Parameters:
expression- the expression to evaluate against case valuesdefaultStatement- the statement executed when no case matches; may beEmptyStatement.INSTANCE- See Also:
-
SwitchStatement
public SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement) Constructs a switch statement with the given control expression, case statements, and default statement.- Parameters:
expression- the expression to evaluate against case valuescaseStatements- the list ofCaseStatementobjects representing case branchesdefaultStatement- the statement executed when no case matches
-
-
Method Details
-
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
-
getCaseStatements
Returns the list of case statements in this switch.- Returns:
- a list of
CaseStatementobjects; never null
-
getExpression
Returns the control expression that is evaluated against case values.- Returns:
- the control
Expression
-
setExpression
Sets the control expression that is evaluated against case values.- Parameters:
e- the controlExpression
-
getDefaultStatement
Returns the statement executed when no case matches the control expression.- Returns:
- the default
Statement, orEmptyStatement.INSTANCEif not set
-
setDefaultStatement
Sets the statement executed when no case matches the control expression.- Parameters:
defaultStatement- the defaultStatement
-
addCase
Adds a case statement to this switch.- Parameters:
caseStatement- theCaseStatementto add
-
getCaseStatement
Returns the case statement at the given index.- Parameters:
idx- the index of the case statement to retrieve- Returns:
- the
CaseStatementat the given index, or null if the index is out of bounds
-