Class VariableExpressionReplacer
java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.transform.tailrec.VariableExpressionReplacer
- All Implemented Interfaces:
GroovyCodeVisitor
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
ConstructorsConstructorDescriptionVariableExpressionReplacer(Closure<Boolean> when, Closure<VariableExpression> replaceWith) Creates a replacer with the supplied predicate and replacement strategy. -
Method Summary
Modifier and TypeMethodDescriptionReturns the closure that creates replacement variable expressions.getWhen()Returns the predicate that decides whether a variable expression should be replaced.voidReplaces matching variable expressions within the supplied AST subtree.voidsetReplaceWith(Closure<VariableExpression> replaceWith) Sets the closure that creates replacement variable expressions.voidSets the predicate that decides whether a variable expression should be replaced.voidvisitAssertStatement(AssertStatement statement) Visits anAssertStatement, traversing the assertion condition and optional message expression.voidvisitBinaryExpression(BinaryExpression expression) It's the only Expression type in which replacing is considered.voidvisitCaseStatement(CaseStatement statement) Visits aCaseStatement, traversing the case expression and code block.voidVisits aDoWhileStatement, traversing the loop body before the boolean condition.voidvisitExpressionStatement(ExpressionStatement statement) Visits anExpressionStatement, traversing its contained expression.voidvisitForLoop(ForStatement forLoop) Visits aForStatement, traversing the collection expression and loop body.voidvisitIfElse(IfStatement ifElse) Visits anIfStatement, traversing the condition, if-block, and optional else-block.voidvisitReturnStatement(ReturnStatement statement) Visits aReturnStatement, traversing its return expression.voidvisitSwitch(SwitchStatement statement) Visits aSwitchStatement, traversing the switch expression, case statements, and default statement.voidvisitSynchronizedStatement(SynchronizedStatement statement) Visits aSynchronizedStatement, traversing the synchronization expression and code block.voidvisitThrowStatement(ThrowStatement statement) Visits aThrowStatement, traversing the exception expression.voidvisitWhileLoop(WhileStatement loop) Visits aWhileStatement, 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, visitVariableExpressionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor
visit, visit, visitEmptyExpression, visitListOfExpressions
-
Constructor Details
-
VariableExpressionReplacer
Creates a replacer with the supplied predicate and replacement strategy.- Parameters:
when- decides whether a variable expression should be replacedreplaceWith- creates the replacement variable expression
-
-
Method Details
-
visitReturnStatement
Visits aReturnStatement, traversing its return expression.- Specified by:
visitReturnStatementin interfaceGroovyCodeVisitor- Overrides:
visitReturnStatementin classCodeVisitorSupport- Parameters:
statement- the return statement
-
visitIfElse
Visits anIfStatement, traversing the condition, if-block, and optional else-block.- Specified by:
visitIfElsein interfaceGroovyCodeVisitor- Overrides:
visitIfElsein classCodeVisitorSupport- Parameters:
ifElse- the if-else statement
-
visitForLoop
Visits aForStatement, traversing the collection expression and loop body.- Specified by:
visitForLoopin interfaceGroovyCodeVisitor- Overrides:
visitForLoopin classCodeVisitorSupport- Parameters:
forLoop- the for loop statement
-
visitBinaryExpression
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:
visitBinaryExpressionin interfaceGroovyCodeVisitor- Overrides:
visitBinaryExpressionin classCodeVisitorSupport- Parameters:
expression- the binary expression
-
visitWhileLoop
Visits aWhileStatement, traversing the boolean condition and loop body.- Specified by:
visitWhileLoopin interfaceGroovyCodeVisitor- Overrides:
visitWhileLoopin classCodeVisitorSupport- Parameters:
loop- the while loop statement
-
visitDoWhileLoop
Visits aDoWhileStatement, traversing the loop body before the boolean condition.- Specified by:
visitDoWhileLoopin interfaceGroovyCodeVisitor- Overrides:
visitDoWhileLoopin classCodeVisitorSupport- Parameters:
loop- the do-while loop statement
-
visitSwitch
Visits aSwitchStatement, traversing the switch expression, case statements, and default statement.- Specified by:
visitSwitchin interfaceGroovyCodeVisitor- Overrides:
visitSwitchin classCodeVisitorSupport- Parameters:
statement- the switch statement
-
visitCaseStatement
Visits aCaseStatement, traversing the case expression and code block.- Specified by:
visitCaseStatementin interfaceGroovyCodeVisitor- Overrides:
visitCaseStatementin classCodeVisitorSupport- Parameters:
statement- the case statement
-
visitExpressionStatement
Visits anExpressionStatement, traversing its contained expression.- Specified by:
visitExpressionStatementin interfaceGroovyCodeVisitor- Overrides:
visitExpressionStatementin classCodeVisitorSupport- Parameters:
statement- the expression statement
-
visitThrowStatement
Visits aThrowStatement, traversing the exception expression.- Specified by:
visitThrowStatementin interfaceGroovyCodeVisitor- Overrides:
visitThrowStatementin classCodeVisitorSupport- Parameters:
statement- the throw statement
-
visitAssertStatement
Visits anAssertStatement, traversing the assertion condition and optional message expression.- Specified by:
visitAssertStatementin interfaceGroovyCodeVisitor- Overrides:
visitAssertStatementin classCodeVisitorSupport- Parameters:
statement- the assert statement
-
visitSynchronizedStatement
Visits aSynchronizedStatement, traversing the synchronization expression and code block.- Specified by:
visitSynchronizedStatementin interfaceGroovyCodeVisitor- Overrides:
visitSynchronizedStatementin classCodeVisitorSupport- Parameters:
statement- the synchronized statement
-
replaceIn
Replaces matching variable expressions within the supplied AST subtree.- Parameters:
root- the AST subtree to mutate
-
getWhen
Returns the predicate that decides whether a variable expression should be replaced.- Returns:
- the replacement predicate
-
setWhen
Sets the predicate that decides whether a variable expression should be replaced.- Parameters:
when- the replacement predicate
-
getReplaceWith
Returns the closure that creates replacement variable expressions.- Returns:
- the replacement closure
-
setReplaceWith
Sets the closure that creates replacement variable expressions.- Parameters:
replaceWith- the replacement closure
-