public class SwitchStatement
extends Statement
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 and description |
|---|
SwitchStatement(Expression expression)Constructs a switch statement with the given control expression. |
SwitchStatement(Expression expression, Statement defaultStatement)Constructs a switch statement with the given control expression and default statement. |
SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)Constructs a switch statement with the given control expression, case statements, and default statement. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
addCase(CaseStatement caseStatement)Adds a case statement to this switch. |
|
public CaseStatement |
getCaseStatement(int idx)Returns the case statement at the given index. |
|
public List<CaseStatement> |
getCaseStatements()Returns the list of case statements in this switch. |
|
public Statement |
getDefaultStatement()Returns the statement executed when no case matches the control expression. |
|
public Expression |
getExpression()Returns the control expression that is evaluated against case values. |
|
public void |
setDefaultStatement(Statement defaultStatement)Sets the statement executed when no case matches the control expression. |
|
public void |
setExpression(Expression e)Sets the control expression that is evaluated against case values. |
|
public void |
visit(GroovyCodeVisitor visitor) |
| Methods inherited from class | Name |
|---|---|
class Statement |
addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabel |
class ASTNode |
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit |
Constructs a switch statement with the given control expression. The default statement is initialized to EmptyStatement.INSTANCE.
expression - the expression to evaluate against case valuesConstructs a switch statement with the given control expression and default statement.
expression - the expression to evaluate against case valuesdefaultStatement - the statement executed when no case matches; may be EmptyStatement.INSTANCEConstructs a switch statement with the given control expression, case statements, and default statement.
expression - the expression to evaluate against case valuescaseStatements - the list of CaseStatement objects representing case branchesdefaultStatement - the statement executed when no case matchesAdds a case statement to this switch.
caseStatement - the CaseStatement to addReturns the case statement at the given index.
idx - the index of the case statement to retrieveReturns 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.
Sets the statement executed when no case matches the control expression.
defaultStatement - the default StatementSets the control expression that is evaluated against case values.
e - the control ExpressionCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.