Package: org.codehaus.groovy.transform.tailrec

## [Java] Class VariableExpressionReplacer

- org.codehaus.groovy.transform.tailrec.VariableExpressionReplacer

-     public class VariableExpressionReplacer
      extends CodeVisitorSupport

  Tool for replacing VariableExpression instances in an AST by other VariableExpression instances. Regardless of a real change taking place in nested expressions, all considered expression (trees) will be replaced. This could be optimized to accelerate compilation.

  Within

  TailRecursive:
    it is used - to swap the access of method args with the access to iteration variables - to swap the access of iteration variables with the access of temp vars

- - ### Constructor Summary

    Constructors
    Constructor and description
    `VariableExpressionReplacer(Closure<Boolean> when, Closure<VariableExpression> replaceWith)` Creates a replacer with the supplied predicate and replacement strategy.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public Closure<VariableExpression>` | `getReplaceWith()` Returns the closure that creates replacement variable expressions.
    `` | `public Closure<Boolean>` | `getWhen()` Returns the predicate that decides whether a variable expression should be replaced.
    `` | `public void` | `replaceIn(ASTNode root)` Replaces matching variable expressions within the supplied AST subtree.
    `` | `public void` | `setReplaceWith(Closure<VariableExpression> replaceWith)` Sets the closure that creates replacement variable expressions.
    `` | `public void` | `setWhen(Closure<Boolean> when)` Sets the predicate that decides whether a variable expression should be replaced.
    `` | `public void` | `visitAssertStatement(AssertStatement statement)` * Visits an AssertStatement, traversing the assertion condition and optional message expression. * * Parameters: `statement` - the assert statement
    `` | `public void` | `visitBinaryExpression(BinaryExpression expression)` It's the only Expression type in which replacing is considered.
    `` | `public void` | `visitCaseStatement(CaseStatement statement)` * Visits a CaseStatement, traversing the case expression and code block. * * Parameters: `statement` - the case statement
    `` | `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` | `visitExpressionStatement(ExpressionStatement statement)` * Visits an ExpressionStatement, traversing its contained expression. * * Parameters: `statement` - the expression 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` | `visitReturnStatement(ReturnStatement statement)` * Visits a ReturnStatement, traversing its return expression. * * Parameters: `statement` - the return statement
    `` | `public void` | `visitSwitch(SwitchStatement statement)` * Visits a SwitchStatement, traversing the switch expression, case statements, and default statement. * * Parameters: `statement` - the switch statement
    `` | `public void` | `visitSynchronizedStatement(SynchronizedStatement statement)` * Visits a SynchronizedStatement, traversing the synchronization expression and code block. * * Parameters: `statement` - the synchronized statement
    `` | `public void` | `visitThrowStatement(ThrowStatement statement)` * Visits a ThrowStatement, traversing the exception expression. * * Parameters: `statement` - the throw 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, 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, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop`

- - ### Constructor Detail

    - #### public VariableExpressionReplacer(Closure<Boolean> when, Closure<VariableExpression> replaceWith)

      Creates a replacer with the supplied predicate and replacement strategy.

      Parameters:
        `when` - decides whether a variable expression should be replaced
        `replaceWith` - creates the replacement variable expression

  - ### Method Detail

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

      Returns the closure that creates replacement variable expressions.

      Returns:
        the replacement closure

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

      Returns the predicate that decides whether a variable expression should be replaced.

      Returns:
        the replacement predicate

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

      Replaces matching variable expressions within the supplied AST subtree.

      Parameters:
        `root` - the AST subtree to mutate

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

      Sets the closure that creates replacement variable expressions.

      Parameters:
        `replaceWith` - the replacement closure

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

      Sets the predicate that decides whether a variable expression should be replaced.

      Parameters:
        `when` - the replacement predicate

    - #### public void visitAssertStatement(AssertStatement statement)

      * Visits an AssertStatement, traversing the assertion condition and optional message expression. * *

      Parameters:
        `statement` - the assert statement

    - #### public void visitBinaryExpression(BinaryExpression expression)

      It's the only Expression type in which replacing is considered. That's an abuse of the class, but I couldn't think of a better way.

    - #### public void visitCaseStatement(CaseStatement statement)

      * Visits a CaseStatement, traversing the case expression and code block. * *

      Parameters:
        `statement` - the case statement

    - #### 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 visitExpressionStatement(ExpressionStatement statement)

      * Visits an ExpressionStatement, traversing its contained expression. * *

      Parameters:
        `statement` - the expression 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 visitReturnStatement(ReturnStatement statement)

      * Visits a ReturnStatement, traversing its return expression. * *

      Parameters:
        `statement` - the return statement

    - #### public void visitSwitch(SwitchStatement statement)

      * Visits a SwitchStatement, traversing the switch expression, case statements, and default statement. * *

      Parameters:
        `statement` - the switch statement

    - #### public void visitSynchronizedStatement(SynchronizedStatement statement)

      * Visits a SynchronizedStatement, traversing the synchronization expression and code block. * *

      Parameters:
        `statement` - the synchronized statement

    - #### public void visitThrowStatement(ThrowStatement statement)

      * Visits a ThrowStatement, traversing the exception expression. * *

      Parameters:
        `statement` - the throw statement

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

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

      Parameters:
        `statement` - the while loop statement