Package org.codehaus.groovy.ast.stmt
Class ForStatement
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.stmt.Statement
org.codehaus.groovy.ast.stmt.ForStatement
- All Implemented Interfaces:
NodeMetaDataHandler,LoopingStatement
Represents a for loop in Groovy, supporting both for-in loops (with values and optional indices)
and classic (C-style) for loops with initialization, condition, and update expressions.
For-in loops iterate over a collection or iterable expression, optionally capturing the index.
Classic for loops use a
ClosureListExpression to represent initialization, condition,
and update expressions and may include multi-assignment declarations.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionForStatement(ClosureListExpression closureListExpression, Statement loopBlock) Constructs a classic (C-style) for loop with optional multi-assignment support.ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock) Constructs a for-in loop with a value variable but no index.ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock) Constructs a for-in loop with an optional index variable and a value variable. -
Method Summary
Modifier and TypeMethodDescriptionReturns theExpressionthat produces the iterable to loop over.Returns the optional indexParameterfor for-in loops with an index variable, or null if this is a for-in loop without an index or a classic for loop.Returns the loop bodyStatement.Returns the valueParameterfor for-in loops, or null if this is a classic for loop (represented internally with a dummy parameter).Deprecated.Returns theVariableScopeassociated with this for loop, containing loop variable bindings and type information.Deprecated.voidsetCollectionExpression(Expression collectionExpression) Sets theExpressionthat produces the collection or iterable to loop over.voidsetLoopBlock(Statement loopBlock) Sets theStatementto execute for each loop iteration.voidsetVariableScope(VariableScope scope) Sets theVariableScopefor this loop.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
-
Field Details
-
FOR_LOOP_DUMMY
Deprecated.
-
-
Constructor Details
-
ForStatement
public ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock) Constructs a for-in loop with an optional index variable and a value variable. This constructor supports both indexed and non-indexed iteration patterns:for (int i in 10..12) { ... } // no index for (int idx, int i in 10..12) { ... } // with index- Parameters:
indexVariable- the optional loop indexParameter, or null if no index is neededvalueVariable- theParameterrepresenting the loop value; must not be nullcollectionExpression- theExpressionthat produces the iterable or collection to loop over; must not be nullloopBlock- theStatementto execute for each iteration; must not be null- Throws:
NullPointerException- if valueVariable, collectionExpression, or loopBlock is null- Since:
- 5.0.0
-
ForStatement
Constructs a for-in loop with a value variable but no index. This is the most common for-in loop pattern, equivalent to Java's enhanced for loop:for (int i : 0..2) { ... } // Java-style colon syntax for (int j in 5..7) { ... } // Groovy-style 'in' keyword- Parameters:
valueVariable- theParameterrepresenting the loop value; must not be nullcollectionExpression- theExpressionthat produces the iterable or collection to loop over; must not be nullloopBlock- theStatementto execute for each iteration; must not be null- Throws:
NullPointerException- if valueVariable, collectionExpression, or loopBlock is null- Since:
- 5.0.0
-
ForStatement
Constructs a classic (C-style) for loop with optional multi-assignment support. TheClosureListExpressionencapsulates initialization, condition, and update expressions:for (int i = 20; i < 23; i++) { ... } for (def (String i, int j) = ['a', 30]; j < 33; i++, j++) { ... }- Parameters:
closureListExpression- theClosureListExpressioncontaining loop initialization, condition, and update; must not be nullloopBlock- theStatementto execute for each iteration; must not be null- Throws:
NullPointerException- if closureListExpression or loopBlock is null- Since:
- 6.0.0
-
-
Method Details
-
setCollectionExpression
Sets theExpressionthat produces the collection or iterable to loop over.- Parameters:
collectionExpression- theExpressionthat evaluates to an iterable; must not be null- Throws:
NullPointerException- if collectionExpression is null
-
setLoopBlock
Sets theStatementto execute for each loop iteration.- Specified by:
setLoopBlockin interfaceLoopingStatement- Parameters:
loopBlock- the loop bodyStatement; must not be null- Throws:
NullPointerException- if loopBlock is null
-
getIndexVariable
Returns the optional indexParameterfor for-in loops with an index variable, or null if this is a for-in loop without an index or a classic for loop.- Returns:
- the index
Parameter, or null - Since:
- 5.0.0
-
getValueVariable
Returns the valueParameterfor for-in loops, or null if this is a classic for loop (represented internally with a dummy parameter).- Returns:
- the value
Parameter, or null for classic for loops - Since:
- 5.0.0
-
getVariable
Deprecated. -
getVariableType
Deprecated. -
getCollectionExpression
Returns theExpressionthat produces the iterable to loop over. For classic for loops, this is aClosureListExpressioncontaining initialization, condition, and update expressions.- Returns:
- the collection or control
Expression
-
getLoopBlock
Returns the loop bodyStatement.- Specified by:
getLoopBlockin interfaceLoopingStatement- Returns:
- the loop block
Statement
-
getVariableScope
Returns theVariableScopeassociated with this for loop, containing loop variable bindings and type information.- Returns:
- the
VariableScope}
-
setVariableScope
Sets theVariableScopefor this loop.- Parameters:
scope- theVariableScopeto associate with this loop
-
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
-