Package org.codehaus.groovy.transform.tailrec

# Class StatementReplacer

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.transform.tailrec.StatementReplacer

All Implemented Interfaces:
  `GroovyCodeVisitor`

public class StatementReplacer extends CodeVisitorSupport
Tool for replacing Statement objects in an AST by other Statement instances.

Within @TailRecursive it is used to swap ReturnStatements with looping back to RECUR label

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `StatementReplacer(Closure<Boolean> when, Closure<Statement> replaceWith)`
  Creates a replacer with the supplied predicate and replacement strategy.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `getClosureLevel()`
  Returns the current closure nesting depth.
  `Closure<Statement>`
  `getReplaceWith()`
  Returns the closure that builds replacement statements.
  `Closure<Boolean>`
  `getWhen()`
  Returns the predicate that decides whether a statement should be replaced.
  `void`
  `replaceIn(ASTNode root)`
  Replaces matching statements within the supplied AST subtree.
  `void`
  `setClosureLevel(int closureLevel)`
  Sets the current closure nesting depth.
  `void`
  `setReplaceWith(Closure<Statement> replaceWith)`
  Sets the closure that builds replacement statements.
  `void`
  `setWhen(Closure<Boolean> when)`
  Sets the predicate that decides whether a statement should be replaced.
  `void`
  `visitBlockStatement(BlockStatement block)`
  Visits a `BlockStatement`, traversing each contained statement in order.
  `void`
  `visitClosureExpression(ClosureExpression expression)`
  Visits a `ClosureExpression`, traversing parameter initializers and the closure code block.
  `void`
  `visitDoWhileLoop(DoWhileStatement loop)`
  Visits a `DoWhileStatement`, traversing the loop body before the boolean condition.
  `void`
  `visitForLoop(ForStatement forLoop)`
  Visits a `ForStatement`, traversing the collection expression and loop body.
  `void`
  `visitIfElse(IfStatement ifElse)`
  Visits an `IfStatement`, traversing the condition, if-block, and optional else-block.
  `void`
  `visitWhileLoop(WhileStatement loop)`
  Visits a `WhileStatement`, traversing the boolean condition and loop body.

  ### Methods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport

  `afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAssertStatement, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCaseStatement, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitEmptyStatement, visitExpressionStatement, visitFieldExpression, visitGStringExpression, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor

  `visit, visit, visitEmptyExpression, visitListOfExpressions`

- ## Constructor Details

  - ### StatementReplacer

    public StatementReplacer(Closure<Boolean> when, Closure<Statement> replaceWith)
    Creates a replacer with the supplied predicate and replacement strategy.
    Parameters:
      `when` - decides whether a statement should be replaced
      `replaceWith` - creates the replacement statement

- ## Method Details

  - ### replaceIn

    public void replaceIn(ASTNode root)
    Replaces matching statements within the supplied AST subtree.
    Parameters:
      `root` - the AST subtree to mutate

  - ### visitClosureExpression

    public void visitClosureExpression(ClosureExpression expression)
    Visits a `ClosureExpression`, traversing parameter initializers and the closure code block.
    Specified by:
      `visitClosureExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitClosureExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the closure expression

  - ### visitBlockStatement

    public void visitBlockStatement(BlockStatement block)
    Visits a `BlockStatement`, traversing each contained statement in order.
    Specified by:
      `visitBlockStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBlockStatement` in class `CodeVisitorSupport`
    Parameters:
      `block` - the block statement to visit, may contain multiple statements
    See Also:
      - `CodeVisitorSupport.visitForLoop(ForStatement)`
      - `CodeVisitorSupport.visitWhileLoop(WhileStatement)`

  - ### visitIfElse

    public void visitIfElse(IfStatement ifElse)
    Visits an `IfStatement`, traversing the condition, if-block, and optional else-block.
    Specified by:
      `visitIfElse` in interface `GroovyCodeVisitor`
    Overrides:
      `visitIfElse` in class `CodeVisitorSupport`
    Parameters:
      `ifElse` - the if-else statement

  - ### visitForLoop

    public void visitForLoop(ForStatement forLoop)
    Visits a `ForStatement`, traversing the collection expression and loop body.
    Specified by:
      `visitForLoop` in interface `GroovyCodeVisitor`
    Overrides:
      `visitForLoop` in class `CodeVisitorSupport`
    Parameters:
      `forLoop` - the for loop statement

  - ### visitWhileLoop

    public void visitWhileLoop(WhileStatement loop)
    Visits a `WhileStatement`, traversing the boolean condition and loop body.
    Specified by:
      `visitWhileLoop` in interface `GroovyCodeVisitor`
    Overrides:
      `visitWhileLoop` in class `CodeVisitorSupport`
    Parameters:
      `loop` - the while loop statement

  - ### visitDoWhileLoop

    public void visitDoWhileLoop(DoWhileStatement loop)
    Visits a `DoWhileStatement`, traversing the loop body before the boolean condition.
    Specified by:
      `visitDoWhileLoop` in interface `GroovyCodeVisitor`
    Overrides:
      `visitDoWhileLoop` in class `CodeVisitorSupport`
    Parameters:
      `loop` - the do-while loop statement

  - ### getWhen

    public Closure<Boolean> getWhen()
    Returns the predicate that decides whether a statement should be replaced.
    Returns:
      the replacement predicate

  - ### setWhen

    public void setWhen(Closure<Boolean> when)
    Sets the predicate that decides whether a statement should be replaced.
    Parameters:
      `when` - the replacement predicate

  - ### getReplaceWith

    public Closure<Statement> getReplaceWith()
    Returns the closure that builds replacement statements.
    Returns:
      the replacement closure

  - ### setReplaceWith

    public void setReplaceWith(Closure<Statement> replaceWith)
    Sets the closure that builds replacement statements.
    Parameters:
      `replaceWith` - the replacement closure

  - ### getClosureLevel

    public int getClosureLevel()
    Returns the current closure nesting depth.
    Returns:
      the number of enclosing closures being visited

  - ### setClosureLevel

    public void setClosureLevel(int closureLevel)
    Sets the current closure nesting depth.
    Parameters:
      `closureLevel` - the closure nesting depth