Package org.codehaus.groovy.transform.sc
Class StaticCompilationVisitor
java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor
org.codehaus.groovy.transform.sc.StaticCompilationVisitor
- All Implemented Interfaces:
GroovyClassVisitor,GroovyCodeVisitor,ErrorCollecting
This visitor is responsible for amending the AST with static compilation metadata or transform the AST so that
a class or a method can be statically compiled. It may also throw errors specific to static compilation which
are not considered as an error at the type check pass. For example, usage of spread operator is not allowed
in statically compiled portions of code, while it may be statically checked.
Static compilation relies on static type checking, which explains why this visitor extends the type checker
visitor.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor
StaticTypeCheckingVisitor.SignatureCodecFactory, StaticTypeCheckingVisitor.VariableExpressionTypeMemoizer -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MethodNodeArrayList.add(Object)method node.static final ClassNodeClass node forArrayList.static final MethodNodeNo-argArrayListconstructor node.static final ClassNodeClass node forCompileStatic.static final ClassNodeClass node forTypeChecked.Fields inherited from class org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor
CHAR_SEQUENCE_TYPE, CLOSURE_CALL_NO_ARG, CLOSURE_CALL_ONE_ARG, CLOSURE_CALL_VARGS, CLOSUREPARAMS_CLASSNODE, CURRENT_SIGNATURE_PROTOCOL, CURRENT_SIGNATURE_PROTOCOL_VERSION, currentField, currentProperty, DELEGATES_TO, DELEGATES_TO_TARGET, DGM_CLASSNODE, EMPTY_METHODNODE_LIST, ENUMERATION_TYPE, ERROR_COLLECTOR, extension, GENERATED_EMPTY_STATEMENT, GET_DELEGATE, GET_OWNER, GET_THISOBJECT, ITERABLE_TYPE, LINKEDHASHMAP_CLASSNODE, MAP_ENTRY_TYPE, NAMED_PARAM_CLASSNODE, NAMED_PARAMS_CLASSNODE, returnAdder, returnListener, TYPECHECKING_ANNOTATIONS, TYPECHECKING_INFO_NODE, typeCheckingContext -
Constructor Summary
ConstructorsConstructorDescriptionStaticCompilationVisitor(SourceUnit unit, ClassNode node) Creates a static compilation visitor for the supplied source unit and class. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanexistsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor) Resolves a property reference and records the owning type for later static compilation.protected MethodNodefindMethodOrFail(Expression expr, ClassNode receiver, String name, ClassNode... args) Stores binary-expression target metadata alongside the resolved method.protected ClassNode[]Returns the annotations that trigger static type checking in this visitor.static booleanIndicates whether the supplied node is marked for static compilation.voidvisitClass(ClassNode node) Visits a class and attaches the metadata required by the static compiler.voidVisits a constructor and records static-compilation metadata for it.voidResolves and stores the direct target for a statically compiled constructor call.voidvisitForLoop(ForStatement statement) Finalizes inferred loop-variable types for statically compiledforloops.voidvisitLambdaExpression(LambdaExpression expression) Removes post-type-checking lambda rewrites that are incompatible with static compilation.voidvisitMethod(MethodNode node) Visits a method and records static-compilation metadata for it.voidResolves and stores the direct target for a statically compiled method call.voidvisitPropertyExpression(PropertyExpression expression) Marks receivers for property expressions that still require dynamic resolution.Methods inherited from class org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor
addAmbiguousErrorMessage, addAssignmentError, addCategoryMethodCallError, addClosureReturnType, addError, addNoMatchingMethodError, addNoMatchingMethodError, addReceivers, addStaticTypeError, addTypeCheckingExtension, addTypeCheckingInfoAnnotation, addUnsupportedPreOrPostfixExpressionError, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, areCategoryMethodCalls, checkCast, checkClosureParameters, checkForbiddenSpreadArgument, checkGroovyConstructorMap, checkGroovyStyleConstructor, checkReturnType, existsProperty, extractPropertyNameFromMethodName, extractTemporaryTypeInfoKey, extractTypesFromParameters, findCurrentInstanceOfClass, findMethod, findMethodsWithGenerated, formatArgumentList, getArgumentTypes, getDelegationMetadata, getGroupOperationResultType, getInferredReturnType, getInferredReturnTypeFromWithClosureArgument, getOriginalDeclarationType, getResultType, getSourceUnit, getTemporaryTypesForExpression, getType, getTypeCheckingContext, hasRHSIncompleteGenericTypeInfo, inferClosureParameterTypes, inferComponentType, inferDiamondType, inferListExpressionType, inferLoopElementType, inferMapExpressionType, inferRangeSliceType, inferReturnTypeGenerics, inferReturnTypeGenerics, initialize, isClassInnerClassOrEqualTo, isNullConstant, isSecondPassNeededForControlStructure, isSkipMode, isSkippedInnerClass, isSuperExpression, isThisExpression, makeOwnerList, performSecondPass, popAssignmentTracking, prettyPrintMethodList, pushAssignmentTracking, pushInstanceOfTypeInfo, restoreVariableExpressionMetadata, saveVariableExpressionMetadata, setCompilationUnit, setMethodsToBeVisited, shouldSkipClassNode, shouldSkipMethodNode, silentlyVisitMethodNode, startMethodInference, storeInferredReturnType, storeTargetMethod, storeType, typeCheckAssignment, typeCheckClosureCall, typeCheckMapConstructor, typeCheckMethodsWithGenericsOrFail, visitArrayExpression, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitCaseStatement, visitCastExpression, visitClassExpression, visitClosureExpression, visitConstructorOrMethod, visitExpressionStatement, visitField, visitIfElse, visitMethodCallArguments, visitMethodPointerExpression, visitNotExpression, visitObjectInitializerStatements, visitPostfixExpression, visitPrefixExpression, visitProperty, visitRangeExpression, visitReturnStatement, visitStaticMethodCallExpression, visitSwitch, visitTernaryExpression, visitTryCatchFinally, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, wrapTypeIfNecessaryMethods inherited from class org.codehaus.groovy.ast.ClassCodeVisitorSupport
visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCatchStatement, visitClassCodeContainer, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitImports, visitPackage, visitStatement, visitStatementAnnotations, visitSynchronizedStatement, visitThrowStatementMethods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport
visitArgumentlistExpression, visitBooleanExpression, visitBytecodeExpression, visitClosureListExpression, visitConstantExpression, visitEmptyStatement, visitFieldExpression, visitGStringExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodReferenceExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitTupleExpressionMethods 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
-
Field Details
-
TYPECHECKED_CLASSNODE
Class node forTypeChecked. -
COMPILESTATIC_CLASSNODE
Class node forCompileStatic. -
ARRAYLIST_CLASSNODE
Class node forArrayList. -
ARRAYLIST_ADD_METHOD
ArrayList.add(Object)method node. -
ARRAYLIST_CONSTRUCTOR
No-argArrayListconstructor node.
-
-
Constructor Details
-
StaticCompilationVisitor
Creates a static compilation visitor for the supplied source unit and class.- Parameters:
unit- the source unit being processednode- the class node to visit
-
-
Method Details
-
getTypeCheckingAnnotations
Returns the annotations that trigger static type checking in this visitor.- Overrides:
getTypeCheckingAnnotationsin classStaticTypeCheckingVisitor- Returns:
- the supported type-checking annotations
-
visitClass
Visits a class and attaches the metadata required by the static compiler.- Specified by:
visitClassin interfaceGroovyClassVisitor- Overrides:
visitClassin classStaticTypeCheckingVisitor- Parameters:
node- the class node to process
-
visitConstructor
Visits a constructor and records static-compilation metadata for it.- Specified by:
visitConstructorin interfaceGroovyClassVisitor- Overrides:
visitConstructorin classStaticTypeCheckingVisitor- Parameters:
node- the constructor to process
-
visitMethod
Visits a method and records static-compilation metadata for it.- Specified by:
visitMethodin interfaceGroovyClassVisitor- Overrides:
visitMethodin classStaticTypeCheckingVisitor- Parameters:
node- the method to process
-
visitMethodCallExpression
Resolves and stores the direct target for a statically compiled method call.- Specified by:
visitMethodCallExpressionin interfaceGroovyCodeVisitor- Overrides:
visitMethodCallExpressionin classStaticTypeCheckingVisitor- Parameters:
call- the method call expression to inspect
-
visitConstructorCallExpression
Resolves and stores the direct target for a statically compiled constructor call.- Specified by:
visitConstructorCallExpressionin interfaceGroovyCodeVisitor- Overrides:
visitConstructorCallExpressionin classStaticTypeCheckingVisitor- Parameters:
call- the constructor call expression to inspect
-
visitForLoop
Finalizes inferred loop-variable types for statically compiledforloops.- Specified by:
visitForLoopin interfaceGroovyCodeVisitor- Overrides:
visitForLoopin classStaticTypeCheckingVisitor- Parameters:
statement- the loop statement to process
-
visitLambdaExpression
Removes post-type-checking lambda rewrites that are incompatible with static compilation.- Specified by:
visitLambdaExpressionin interfaceGroovyCodeVisitor- Overrides:
visitLambdaExpressionin classCodeVisitorSupport- Parameters:
expression- the lambda expression to process
-
visitPropertyExpression
Marks receivers for property expressions that still require dynamic resolution.- Specified by:
visitPropertyExpressionin interfaceGroovyCodeVisitor- Overrides:
visitPropertyExpressionin classStaticTypeCheckingVisitor- Parameters:
expression- the property expression to process
-
existsProperty
protected boolean existsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor) Resolves a property reference and records the owning type for later static compilation.- Overrides:
existsPropertyin classStaticTypeCheckingVisitor- Parameters:
pexp- the property expression being resolvedcheckForReadOnly- whether read-only properties should be consideredvisitor- an optional callback for the matched member- Returns:
trueif a matching property-like member exists
-
findMethodOrFail
protected MethodNode findMethodOrFail(Expression expr, ClassNode receiver, String name, ClassNode... args) Stores binary-expression target metadata alongside the resolved method.- Overrides:
findMethodOrFailin classStaticTypeCheckingVisitor- Parameters:
expr- the expression being resolvedreceiver- the candidate receiver typename- the method nameargs- the argument types- Returns:
- the resolved method, or
nullif resolution fails
-
isStaticallyCompiled
Indicates whether the supplied node is marked for static compilation.- Parameters:
node- the class or method node to inspect- Returns:
trueif the node is statically compiled
-