Class ForEachStmt

All Implemented Interfaces:
NodeWithBody<ForEachStmt>, NodeWithRange<Node>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable

public class ForEachStmt extends Statement implements NodeWithBody<ForEachStmt>
A for-each statement.
for(Object o: objects) { ... } It was introduced in Java 5.
Author:
Julio Vilmar Gesser
  • Constructor Details

  • Method Details

    • accept

      public <R,​ A> R accept(GenericVisitor<R,​A> v, A arg)
      Description copied from interface: Visitable
      Accept method for visitor support.
      Specified by:
      accept in interface Visitable
      Type Parameters:
      R - the type of the return value of the visitor
      A - the type the user argument passed to the visitor
      Parameters:
      v - the visitor implementation
      arg - the argument passed to the visitor (of type A)
      Returns:
      the result of the visit (of type R)
    • accept

      public <A> void accept(VoidVisitor<A> v, A arg)
      Description copied from interface: Visitable
      Accept method for visitor support.
      Specified by:
      accept in interface Visitable
      Type Parameters:
      A - the type the argument passed for the visitor
      Parameters:
      v - the visitor implementation
      arg - any value relevant for the visitor (of type A)
    • getBody

      public Statement getBody()
      Specified by:
      getBody in interface NodeWithBody<ForEachStmt>
    • getIterable

      public Expression getIterable()
    • getVariable

      public VariableDeclarationExpr getVariable()
    • setBody

      public ForEachStmt setBody(Statement body)
      Specified by:
      setBody in interface NodeWithBody<ForEachStmt>
    • setIterable

      public ForEachStmt setIterable(Expression iterable)
    • setVariable

      public ForEachStmt setVariable(VariableDeclarationExpr variable)
    • getVariableDeclarator

      public VariableDeclarator getVariableDeclarator()
      Convenience method that directly returns this foreach statement's single variable declarator. Note that any foreach statement's variable declaration expression (as returned by getVariable()) always has exactly one variable declarator.

      Calling this method on a foreach statement forEachStmt is equivalent to calling forEachStmt.getVariable().getVariable(0).

      Returns:
      this foreach statement's single variable declarator.
    • hasFinalVariable

      public boolean hasFinalVariable()
      Convenience method that decides whether this foreach statement's variable is final. Note that any foreach statement's variable declaration expression (as returned by getVariable()) always has either no modifiers, or a single final modifier.

      Calling this method on a foreach statement forEachStmt is equivalent to calling forEachStmt.getVariable().getModifiers().isNonEmpty() && forEachStmt.getVariable().getModifiers().get(0).getKeyword() == Modifier.Keyword.FINAL.

      Returns:
      true if this foreach statement's variable is final, and false otherwise.
    • clone

      public ForEachStmt clone()
      Overrides:
      clone in class Statement
    • replace

      public boolean replace(Node node, Node replacementNode)
      Overrides:
      replace in class Node
    • isForEachStmt

      public boolean isForEachStmt()
      Overrides:
      isForEachStmt in class Statement
    • asForEachStmt

      public ForEachStmt asForEachStmt()
      Overrides:
      asForEachStmt in class Statement
    • toForEachStmt

      public Optional<ForEachStmt> toForEachStmt()
      Overrides:
      toForEachStmt in class Statement
    • ifForEachStmt

      public void ifForEachStmt(Consumer<ForEachStmt> action)
      Overrides:
      ifForEachStmt in class Statement
    • getMetaModel

      public ForEachStmtMetaModel getMetaModel()
      Overrides:
      getMetaModel in class Statement
      Returns:
      get JavaParser specific node introspection information.