Class ForStatement

All Implemented Interfaces:
NodeMetaDataHandler, LoopingStatement

public class ForStatement extends Statement implements 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.
See Also:
  • Field Details

  • 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 index Parameter, or null if no index is needed
      valueVariable - the Parameter representing the loop value; must not be null
      collectionExpression - the Expression that produces the iterable or collection to loop over; must not be null
      loopBlock - the Statement to execute for each iteration; must not be null
      Throws:
      NullPointerException - if valueVariable, collectionExpression, or loopBlock is null
      Since:
      5.0.0
    • ForStatement

      public ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock)
      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 - the Parameter representing the loop value; must not be null
      collectionExpression - the Expression that produces the iterable or collection to loop over; must not be null
      loopBlock - the Statement to execute for each iteration; must not be null
      Throws:
      NullPointerException - if valueVariable, collectionExpression, or loopBlock is null
      Since:
      5.0.0
    • ForStatement

      public ForStatement(ClosureListExpression closureListExpression, Statement loopBlock)
      Constructs a classic (C-style) for loop with optional multi-assignment support. The ClosureListExpression encapsulates 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 - the ClosureListExpression containing loop initialization, condition, and update; must not be null
      loopBlock - the Statement to execute for each iteration; must not be null
      Throws:
      NullPointerException - if closureListExpression or loopBlock is null
      Since:
      6.0.0
  • Method Details

    • setCollectionExpression

      public void setCollectionExpression(Expression collectionExpression)
      Sets the Expression that produces the collection or iterable to loop over.
      Parameters:
      collectionExpression - the Expression that evaluates to an iterable; must not be null
      Throws:
      NullPointerException - if collectionExpression is null
    • setLoopBlock

      public void setLoopBlock(Statement loopBlock)
      Sets the Statement to execute for each loop iteration.
      Specified by:
      setLoopBlock in interface LoopingStatement
      Parameters:
      loopBlock - the loop body Statement; must not be null
      Throws:
      NullPointerException - if loopBlock is null
    • getIndexVariable

      public Parameter getIndexVariable()
      Returns the optional index Parameter for 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

      public Parameter getValueVariable()
      Returns the value Parameter for 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(since="5.0.0") public Parameter getVariable()
      Deprecated.
    • getVariableType

      @Deprecated(since="5.0.0") public ClassNode getVariableType()
      Deprecated.
    • getCollectionExpression

      public Expression getCollectionExpression()
      Returns the Expression that produces the iterable to loop over. For classic for loops, this is a ClosureListExpression containing initialization, condition, and update expressions.
      Returns:
      the collection or control Expression
    • getLoopBlock

      public Statement getLoopBlock()
      Returns the loop body Statement.
      Specified by:
      getLoopBlock in interface LoopingStatement
      Returns:
      the loop block Statement
    • getVariableScope

      public VariableScope getVariableScope()
      Returns the VariableScope associated with this for loop, containing loop variable bindings and type information.
      Returns:
      the VariableScope}
    • setVariableScope

      public void setVariableScope(VariableScope scope)
      Sets the VariableScope for this loop.
      Parameters:
      scope - the VariableScope to associate with this loop
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts 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:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node