Class ForStatement

All Implemented Interfaces:
NodeMetaDataHandler, LoopingStatement

public class ForStatement extends Statement implements LoopingStatement
Represents a for loop in Groovy.
  • Field Details

  • Constructor Details

    • ForStatement

      public ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock)
      A constructor of for-in loops (possibly with an optional index variable). An example with an index variable:
       for (int idx, int i in 10..12) {
         println "$idx $i"
       }
       
      Since:
      5.0.0
    • ForStatement

      public ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock)
      A constructor of for-in loops without an index variable. Variants using Java-style ":" or the Groovy reserved word "in" are equivalent:
       for (int i : 0..2) {
         println i
       }
       for (int j in 5..7) {
         println j
       }
       
      Since:
      5.0.0
    • ForStatement

      public ForStatement(ClosureListExpression closureListExpression, Statement loopBlock)
      A constructor of classic (C-style) for loops.
       for (int i = 20; i < 23; i++) {
         println i
       }
       
      Also handles multi-assignment for loops.
       for (def (String i, int j) = ['a', 30]; j < 33; i++, j++) {
         println "$i $j"
       }
       
      Since:
      6.0.0
  • Method Details

    • setCollectionExpression

      public void setCollectionExpression(Expression collectionExpression)
    • setLoopBlock

      public void setLoopBlock(Statement loopBlock)
      Description copied from interface: LoopingStatement
      Sets the loop block.
      Specified by:
      setLoopBlock in interface LoopingStatement
    • getIndexVariable

      public Parameter getIndexVariable()
      Retrieves the index variable of for-in loops with an index variable or null in other cases.
      Since:
      5.0.0
    • getValueVariable

      public Parameter getValueVariable()
      Retrieves the value variable of for-in loops or null for a classic for loop.
      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()
      Retrieves the collection expression of a for loop. Will be an instance of ClosureListExpression for a classic for loop.
    • getLoopBlock

      public Statement getLoopBlock()
      Description copied from interface: LoopingStatement
      Gets the loop block.
      Specified by:
      getLoopBlock in interface LoopingStatement
    • getVariableScope

      public VariableScope getVariableScope()
    • setVariableScope

      public void setVariableScope(VariableScope scope)
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Overrides:
      visit in class ASTNode