Package org.codehaus.groovy.transform.tailrec

# Class VariableExpressionReplacer

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

All Implemented Interfaces:
  `GroovyCodeVisitor`

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Closure<VariableExpression>`
  `getReplaceWith()`
  Returns the closure that creates replacement variable expressions.
  `Closure<Boolean>`
  `getWhen()`
  Returns the predicate that decides whether a variable expression should be replaced.
  `void`
  `replaceIn(ASTNode root)`
  Replaces matching variable expressions within the supplied AST subtree.
  `void`
  `setReplaceWith(Closure<VariableExpression> replaceWith)`
  Sets the closure that creates replacement variable expressions.
  `void`
  `setWhen(Closure<Boolean> when)`
  Sets the predicate that decides whether a variable expression should be replaced.
  `void`
  `visitAssertStatement(AssertStatement statement)`
  Visits an `AssertStatement`, traversing the assertion condition and optional message expression.
  `void`
  `visitBinaryExpression(BinaryExpression expression)`
  It's the only Expression type in which replacing is considered.
  `void`
  `visitCaseStatement(CaseStatement statement)`
  Visits a `CaseStatement`, traversing the case expression and code block.
  `void`
  `visitDoWhileLoop(DoWhileStatement loop)`
  Visits a `DoWhileStatement`, traversing the loop body before the boolean condition.
  `void`
  `visitExpressionStatement(ExpressionStatement statement)`
  Visits an `ExpressionStatement`, traversing its contained expression.
  `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`
  `visitReturnStatement(ReturnStatement statement)`
  Visits a `ReturnStatement`, traversing its return expression.
  `void`
  `visitSwitch(SwitchStatement statement)`
  Visits a `SwitchStatement`, traversing the switch expression, case statements, and default statement.
  `void`
  `visitSynchronizedStatement(SynchronizedStatement statement)`
  Visits a `SynchronizedStatement`, traversing the synchronization expression and code block.
  `void`
  `visitThrowStatement(ThrowStatement statement)`
  Visits a `ThrowStatement`, traversing the exception expression.
  `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, visitAttributeExpression, visitBitwiseNegationExpression, visitBlockStatement, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitEmptyStatement, visitFieldExpression, visitGStringExpression, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitTernaryExpression, 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

  - ### VariableExpressionReplacer

    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 Details

  - ### visitReturnStatement

    public void visitReturnStatement(ReturnStatement statement)
    Visits a `ReturnStatement`, traversing its return expression.
    Specified by:
      `visitReturnStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitReturnStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the return statement

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

  - ### visitBinaryExpression

    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.
    Specified by:
      `visitBinaryExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBinaryExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the binary expression

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

  - ### visitSwitch

    public void visitSwitch(SwitchStatement statement)
    Visits a `SwitchStatement`, traversing the switch expression, case statements, and default statement.
    Specified by:
      `visitSwitch` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSwitch` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the switch statement

  - ### visitCaseStatement

    public void visitCaseStatement(CaseStatement statement)
    Visits a `CaseStatement`, traversing the case expression and code block.
    Specified by:
      `visitCaseStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCaseStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the case statement

  - ### visitExpressionStatement

    public void visitExpressionStatement(ExpressionStatement statement)
    Visits an `ExpressionStatement`, traversing its contained expression.
    Specified by:
      `visitExpressionStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitExpressionStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the expression statement

  - ### visitThrowStatement

    public void visitThrowStatement(ThrowStatement statement)
    Visits a `ThrowStatement`, traversing the exception expression.
    Specified by:
      `visitThrowStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitThrowStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the throw statement

  - ### visitAssertStatement

    public void visitAssertStatement(AssertStatement statement)
    Visits an `AssertStatement`, traversing the assertion condition and optional message expression.
    Specified by:
      `visitAssertStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitAssertStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the assert statement

  - ### visitSynchronizedStatement

    public void visitSynchronizedStatement(SynchronizedStatement statement)
    Visits a `SynchronizedStatement`, traversing the synchronization expression and code block.
    Specified by:
      `visitSynchronizedStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSynchronizedStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the synchronized statement

  - ### replaceIn

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

  - ### getWhen

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

  - ### setWhen

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

  - ### getReplaceWith

    public Closure<VariableExpression> getReplaceWith()
    Returns the closure that creates replacement variable expressions.
    Returns:
      the replacement closure

  - ### setReplaceWith

    public void setReplaceWith(Closure<VariableExpression> replaceWith)
    Sets the closure that creates replacement variable expressions.
    Parameters:
      `replaceWith` - the replacement closure