Class ASTTransformationVisitor
- All Implemented Interfaces:
GroovyClassVisitor,GroovyCodeVisitor,ErrorCollecting
ASTTransformationCollectorCodeVisitor will add a list
of annotations that this visitor should be concerned about. All other
annotations are ignored, whether they are GroovyASTTransformation
annotated or not.
A Two-pass method is used. First all candidate annotations are added to a list then the transformations are called on those collected annotations. This is done to avoid concurrent modification exceptions during the AST tree walk and allows the transformations to alter any portion of the AST tree. Hence annotations that are added in this phase will not be processed as transformations. They will only be handled in later phases (and then only if the type was in the AST prior to any AST transformations being run against it).
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidLoads global AST transformations fromMETA-INF/services/org.codehaus.groovy.transform.ASTTransformation, records every accepted name oncontext, and registers their phase operations.static voidLoads global AST transformations discovered after a@Grab, and registers phase operations only for names that were not already recorded byaddGlobalTransforms(ASTTransformationsContext).static voidaddNewPhaseOperation(CompilationUnit compilationUnit, SourceUnit sourceUnit, ClassNode classNode) Enables transforms for class that was added during current phase.static voidaddPhaseOperations(CompilationUnit compilationUnit) Registers the phase operations that collect and run AST transformations.protected SourceUnitProvides access to theSourceUnitfor error reporting during visitation.static voidinvokeTransformsForClass(CompilePhase phase, ASTTransformationsContext context, SourceUnit source, ClassNode classNode) Invokes any AST transformations registered forphaseon the given class using the standardASTTransformationVisitorwalk.voidAdds the annotation to the internal target list if a match is found.voidvisitClass(ClassNode classNode) Main loop entry.voidvisitProperty(PropertyNode node) Visits aPropertyNode, processing its annotations, initial value expression, and getter/setter blocks if present.protected voidvisitStatementAnnotations(Statement statement) Adds annotated loop statements to the target list so that the registered AST transformation is invoked withnodes[1]being the loop statement.Methods inherited from class org.codehaus.groovy.ast.ClassCodeVisitorSupport
addError, visitAnnotation, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCaseStatement, visitCatchStatement, visitClassCodeContainer, visitClosureExpression, visitConstructor, visitConstructorOrMethod, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitExpressionStatement, visitField, visitForLoop, visitIfElse, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitReturnStatement, visitStatement, visitSwitch, visitSynchronizedStatement, visitThrowStatement, visitTryCatchFinally, visitWhileLoop, visitYieldStatementMethods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport
afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBooleanExpression, visitBytecodeExpression, visitCastExpression, visitClassExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitEmptyStatement, visitFieldExpression, visitGStringExpression, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitchExpression, visitTernaryExpression, 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
-
Method Details
-
invokeTransformsForClass
public static void invokeTransformsForClass(CompilePhase phase, ASTTransformationsContext context, SourceUnit source, ClassNode classNode) Invokes any AST transformations registered forphaseon the given class using the standardASTTransformationVisitorwalk. Intended for callers that need to drive transform invocation outside the standard phase wiring (for example,JavaAwareCompilationUnitruns CONVERSION-phase transforms before the stub generator emits stubs — see GEP-21).- Since:
- 6.0.0
-
getSourceUnit
Description copied from class:ClassCodeVisitorSupportProvides access to theSourceUnitfor error reporting during visitation. Implementations must override this method.- Specified by:
getSourceUnitin classClassCodeVisitorSupport- Returns:
- the source unit for this visitor
-
visitClass
Main loop entry.First, it delegates to the super visitClass so we can collect the relevant annotations in an AST tree walk.
Second, it calls the visit method on the transformation for each relevant annotation found.
- Specified by:
visitClassin interfaceGroovyClassVisitor- Overrides:
visitClassin classClassCodeVisitorSupport- Parameters:
classNode- the class to visit
-
visitAnnotations
Adds the annotation to the internal target list if a match is found.- Overrides:
visitAnnotationsin classClassCodeVisitorSupport- Parameters:
node- the node to be processed
-
visitStatementAnnotations
Adds annotated loop statements to the target list so that the registered AST transformation is invoked withnodes[1]being the loop statement.- Overrides:
visitStatementAnnotationsin classClassCodeVisitorSupport- Parameters:
statement- the loop statement that may have statement-level annotations
-
visitProperty
Description copied from class:ClassCodeVisitorSupportVisits aPropertyNode, processing its annotations, initial value expression, and getter/setter blocks if present.- Specified by:
visitPropertyin interfaceGroovyClassVisitor- Overrides:
visitPropertyin classClassCodeVisitorSupport- Parameters:
node- the property node to visit
-
addPhaseOperations
Registers the phase operations that collect and run AST transformations. Global transforms are loaded first. A semantic-analysis operation then collects per-class transformation annotations, and every later compile phase gets an operation that runs the transformations for that phase. Initialization, parsing and conversion are left unchanged.- Parameters:
compilationUnit- the unit that receives the operations
-
addNewPhaseOperation
public static void addNewPhaseOperation(CompilationUnit compilationUnit, SourceUnit sourceUnit, ClassNode classNode) Enables transforms for class that was added during current phase. -
addGlobalTransformsAfterGrab
Loads global AST transformations discovered after a@Grab, and registers phase operations only for names that were not already recorded byaddGlobalTransforms(ASTTransformationsContext).- Parameters:
context- the compilation unit's transformation context
-
addGlobalTransforms
Loads global AST transformations fromMETA-INF/services/org.codehaus.groovy.transform.ASTTransformation, records every accepted name oncontext, and registers their phase operations.- Parameters:
context- the compilation unit's transformation context
-