Package: org.codehaus.groovy.transform.tailrec

## [Java] Class StatementReplacer

- org.codehaus.groovy.transform.tailrec.StatementReplacer

-     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 and description
    `StatementReplacer(Closure<Boolean> when, Closure<Statement> replaceWith)` Creates a replacer with the supplied predicate and replacement strategy.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public int` | `getClosureLevel()` Returns the current closure nesting depth.
    `` | `public Closure<Statement>` | `getReplaceWith()` Returns the closure that builds replacement statements.
    `` | `public Closure<Boolean>` | `getWhen()` Returns the predicate that decides whether a statement should be replaced.
    `` | `public void` | `replaceIn(ASTNode root)` Replaces matching statements within the supplied AST subtree.
    `` | `public void` | `setClosureLevel(int closureLevel)` Sets the current closure nesting depth.
    `` | `public void` | `setReplaceWith(Closure<Statement> replaceWith)` Sets the closure that builds replacement statements.
    `` | `public void` | `setWhen(Closure<Boolean> when)` Sets the predicate that decides whether a statement should be replaced.
    `` | `public void` | `visitBlockStatement(BlockStatement block)` * Visits a BlockStatement, traversing each contained statement in order. * * Parameters: `block` - the block statement to visit, may contain multiple statements * See Also: * visitWhileLoop(WhileStatement)
    `` | `public void` | `visitClosureExpression(ClosureExpression expression)` * Visits a ClosureExpression, traversing parameter initializers and the closure code block. * * Parameters: `expression` - the closure expression
    `` | `public void` | `visitDoWhileLoop(DoWhileStatement loop)` * Visits a DoWhileStatement, traversing the loop body before the boolean condition. * * Parameters: `statement` - the do-while loop statement
    `` | `public void` | `visitForLoop(ForStatement forLoop)` * Visits a ForStatement, traversing the collection expression and loop body. * * Parameters: `statement` - the for loop statement
    `` | `public void` | `visitIfElse(IfStatement ifElse)` * Visits an IfStatement, traversing the condition, if-block, and optional else-block. * * Parameters: `statement` - the if-else statement
    `` | `public void` | `visitWhileLoop(WhileStatement loop)` * Visits a WhileStatement, traversing the boolean condition and loop body. * * Parameters: `statement` - the while loop statement

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class CodeVisitorSupport` | `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAssertStatement, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBlockStatement, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCaseStatement, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitEmptyStatement, visitExpressionStatement, visitFieldExpression, visitForLoop, visitGStringExpression, visitIfElse, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSwitchExpression, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, visitYieldStatement`

- - ### Constructor Detail

    - #### 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 Detail

    - #### public int getClosureLevel()

      Returns the current closure nesting depth.

      Returns:
        the number of enclosing closures being visited

    - #### public Closure<Statement> getReplaceWith()

      Returns the closure that builds replacement statements.

      Returns:
        the replacement closure

    - #### public Closure<Boolean> getWhen()

      Returns the predicate that decides whether a statement should be replaced.

      Returns:
        the replacement predicate

    - #### public void replaceIn(ASTNode root)

      Replaces matching statements within the supplied AST subtree.

      Parameters:
        `root` - the AST subtree to mutate

    - #### public void setClosureLevel(int closureLevel)

      Sets the current closure nesting depth.

      Parameters:
        `closureLevel` - the closure nesting depth

    - #### public void setReplaceWith(Closure<Statement> replaceWith)

      Sets the closure that builds replacement statements.

      Parameters:
        `replaceWith` - the replacement closure

    - #### public void setWhen(Closure<Boolean> when)

      Sets the predicate that decides whether a statement should be replaced.

      Parameters:
        `when` - the replacement predicate

    - #### public void visitBlockStatement(BlockStatement block)

      * Visits a BlockStatement, traversing each contained statement in order. * *

      Parameters:
        `block` - the block statement to visit, may contain multiple statements *
      See Also:
        *
        visitWhileLoop(WhileStatement)

    - #### public void visitClosureExpression(ClosureExpression expression)

      * Visits a ClosureExpression, traversing parameter initializers and the closure code block. * *

      Parameters:
        `expression` - the closure expression

    - #### public void visitDoWhileLoop(DoWhileStatement loop)

      * Visits a DoWhileStatement, traversing the loop body before the boolean condition. * *

      Parameters:
        `statement` - the do-while loop statement

    - #### public void visitForLoop(ForStatement forLoop)

      * Visits a ForStatement, traversing the collection expression and loop body. * *

      Parameters:
        `statement` - the for loop statement

    - #### public void visitIfElse(IfStatement ifElse)

      * Visits an IfStatement, traversing the condition, if-block, and optional else-block. * *

      Parameters:
        `statement` - the if-else statement

    - #### public void visitWhileLoop(WhileStatement loop)

      * Visits a WhileStatement, traversing the boolean condition and loop body. * *

      Parameters:
        `statement` - the while loop statement