Class StatementReplacer
java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.transform.tailrec.StatementReplacer
- All Implemented Interfaces:
GroovyCodeVisitor
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
ConstructorsConstructorDescriptionStatementReplacer(Closure<Boolean> when, Closure<Statement> replaceWith) Creates a replacer with the supplied predicate and replacement strategy. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the current closure nesting depth.Returns the closure that builds replacement statements.getWhen()Returns the predicate that decides whether a statement should be replaced.voidReplaces matching statements within the supplied AST subtree.voidsetClosureLevel(int closureLevel) Sets the current closure nesting depth.voidsetReplaceWith(Closure<Statement> replaceWith) Sets the closure that builds replacement statements.voidSets the predicate that decides whether a statement should be replaced.voidVisits aBlockStatement, traversing each contained statement in order.voidvisitClosureExpression(ClosureExpression expression) Visits aClosureExpression, traversing parameter initializers and the closure code block.voidVisits aDoWhileStatement, traversing the loop body before the boolean condition.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.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, 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, 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
-
StatementReplacer
Creates a replacer with the supplied predicate and replacement strategy.- Parameters:
when- decides whether a statement should be replacedreplaceWith- creates the replacement statement
-
-
Method Details
-
replaceIn
Replaces matching statements within the supplied AST subtree.- Parameters:
root- the AST subtree to mutate
-
visitClosureExpression
Visits aClosureExpression, traversing parameter initializers and the closure code block.- Specified by:
visitClosureExpressionin interfaceGroovyCodeVisitor- Overrides:
visitClosureExpressionin classCodeVisitorSupport- Parameters:
expression- the closure expression
-
visitBlockStatement
Visits aBlockStatement, traversing each contained statement in order.- Specified by:
visitBlockStatementin interfaceGroovyCodeVisitor- Overrides:
visitBlockStatementin classCodeVisitorSupport- Parameters:
block- the block statement to visit, may contain multiple statements- See Also:
-
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
-
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
-
getWhen
Returns the predicate that decides whether a statement should be replaced.- Returns:
- the replacement predicate
-
setWhen
Sets the predicate that decides whether a statement should be replaced.- Parameters:
when- the replacement predicate
-
getReplaceWith
Returns the closure that builds replacement statements.- Returns:
- the replacement closure
-
setReplaceWith
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
-