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`

public class StaticCompilationVisitor extends StaticTypeCheckingVisitor
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

  Fields
  Modifier and Type
  Field
  Description
  `static final MethodNode`
  `ARRAYLIST_ADD_METHOD`
  `ArrayList.add(Object)` method node.
  `static final ClassNode`
  `ARRAYLIST_CLASSNODE`
  Class node for `ArrayList`.
  `static final MethodNode`
  `ARRAYLIST_CONSTRUCTOR`
  No-arg `ArrayList` constructor node.
  `static final ClassNode`
  `COMPILESTATIC_CLASSNODE`
  Class node for `CompileStatic`.
  `static final ClassNode`
  `TYPECHECKED_CLASSNODE`
  Class node for `TypeChecked`.

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

  Constructors
  Constructor
  Description
  `StaticCompilationVisitor(SourceUnit unit, ClassNode node)`
  Creates a static compilation visitor for the supplied source unit and class.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected boolean`
  `existsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor)`
  Resolves a property reference and records the owning type for later static compilation.
  `protected MethodNode`
  `findMethodOrFail(Expression expr, ClassNode receiver, String name, ClassNode... args)`
  Stores binary-expression target metadata alongside the resolved method.
  `protected ClassNode[]`
  `getTypeCheckingAnnotations()`
  Returns the annotations that trigger static type checking in this visitor.
  `static boolean`
  `isStaticallyCompiled(AnnotatedNode node)`
  Indicates whether the supplied node is marked for static compilation.
  `void`
  `visitClass(ClassNode node)`
  Visits a class and attaches the metadata required by the static compiler.
  `void`
  `visitConstructor(ConstructorNode node)`
  Visits a constructor and records static-compilation metadata for it.
  `void`
  `visitConstructorCallExpression(ConstructorCallExpression call)`
  Resolves and stores the direct target for a statically compiled constructor call.
  `void`
  `visitForLoop(ForStatement statement)`
  Finalizes inferred loop-variable types for statically compiled `for` loops.
  `void`
  `visitLambdaExpression(LambdaExpression expression)`
  Removes post-type-checking lambda rewrites that are incompatible with static compilation.
  `void`
  `visitMethod(MethodNode node)`
  Visits a method and records static-compilation metadata for it.
  `void`
  `visitMethodCallExpression(MethodCallExpression call)`
  Resolves and stores the direct target for a statically compiled method call.
  `void`
  `visitPropertyExpression(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, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, 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, visitSwitchExpression, visitTernaryExpression, visitTryCatchFinally, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, visitYieldStatement, wrapTypeIfNecessary`

  ### Methods inherited from class org.codehaus.groovy.ast.ClassCodeVisitorSupport

  `visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCatchStatement, visitClassCodeContainer, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitImports, visitPackage, visitStatement, visitStatementAnnotations, visitSynchronizedStatement, visitThrowStatement`

  ### Methods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport

  `visitArgumentlistExpression, visitBooleanExpression, visitBytecodeExpression, visitClosureListExpression, visitConstantExpression, visitEmptyStatement, visitFieldExpression, visitGStringExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodReferenceExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitTupleExpression`

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

- ## Field Details

  - ### TYPECHECKED_CLASSNODE

    public static final ClassNode TYPECHECKED_CLASSNODE
    Class node for `TypeChecked`.

  - ### COMPILESTATIC_CLASSNODE

    public static final ClassNode COMPILESTATIC_CLASSNODE
    Class node for `CompileStatic`.

  - ### ARRAYLIST_CLASSNODE

    public static final ClassNode ARRAYLIST_CLASSNODE
    Class node for `ArrayList`.

  - ### ARRAYLIST_ADD_METHOD

    public static final MethodNode ARRAYLIST_ADD_METHOD
    `ArrayList.add(Object)` method node.

  - ### ARRAYLIST_CONSTRUCTOR

    public static final MethodNode ARRAYLIST_CONSTRUCTOR
    No-arg `ArrayList` constructor node.

- ## Constructor Details

  - ### StaticCompilationVisitor

    public StaticCompilationVisitor(SourceUnit unit, ClassNode node)
    Creates a static compilation visitor for the supplied source unit and class.
    Parameters:
      `unit` - the source unit being processed
      `node` - the class node to visit

- ## Method Details

  - ### getTypeCheckingAnnotations

    protected ClassNode[] getTypeCheckingAnnotations()
    Returns the annotations that trigger static type checking in this visitor.
    Overrides:
      `getTypeCheckingAnnotations` in class `StaticTypeCheckingVisitor`
    Returns:
      the supported type-checking annotations

  - ### visitClass

    public void visitClass(ClassNode node)
    Visits a class and attaches the metadata required by the static compiler.
    Specified by:
      `visitClass` in interface `GroovyClassVisitor`
    Overrides:
      `visitClass` in class `StaticTypeCheckingVisitor`
    Parameters:
      `node` - the class node to process

  - ### visitConstructor

    public void visitConstructor(ConstructorNode node)
    Visits a constructor and records static-compilation metadata for it.
    Specified by:
      `visitConstructor` in interface `GroovyClassVisitor`
    Overrides:
      `visitConstructor` in class `StaticTypeCheckingVisitor`
    Parameters:
      `node` - the constructor to process

  - ### visitMethod

    public void visitMethod(MethodNode node)
    Visits a method and records static-compilation metadata for it.
    Specified by:
      `visitMethod` in interface `GroovyClassVisitor`
    Overrides:
      `visitMethod` in class `StaticTypeCheckingVisitor`
    Parameters:
      `node` - the method to process

  - ### visitMethodCallExpression

    public void visitMethodCallExpression(MethodCallExpression call)
    Resolves and stores the direct target for a statically compiled method call.
    Specified by:
      `visitMethodCallExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitMethodCallExpression` in class `StaticTypeCheckingVisitor`
    Parameters:
      `call` - the method call expression to inspect

  - ### visitConstructorCallExpression

    public void visitConstructorCallExpression(ConstructorCallExpression call)
    Resolves and stores the direct target for a statically compiled constructor call.
    Specified by:
      `visitConstructorCallExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitConstructorCallExpression` in class `StaticTypeCheckingVisitor`
    Parameters:
      `call` - the constructor call expression to inspect

  - ### visitForLoop

    public void visitForLoop(ForStatement statement)
    Finalizes inferred loop-variable types for statically compiled `for` loops.
    Specified by:
      `visitForLoop` in interface `GroovyCodeVisitor`
    Overrides:
      `visitForLoop` in class `StaticTypeCheckingVisitor`
    Parameters:
      `statement` - the loop statement to process

  - ### visitLambdaExpression

    public void visitLambdaExpression(LambdaExpression expression)
    Removes post-type-checking lambda rewrites that are incompatible with static compilation.
    Specified by:
      `visitLambdaExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitLambdaExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the lambda expression to process

  - ### visitPropertyExpression

    public void visitPropertyExpression(PropertyExpression expression)
    Marks receivers for property expressions that still require dynamic resolution.
    Specified by:
      `visitPropertyExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitPropertyExpression` in class `StaticTypeCheckingVisitor`
    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:
      `existsProperty` in class `StaticTypeCheckingVisitor`
    Parameters:
      `pexp` - the property expression being resolved
      `checkForReadOnly` - whether read-only properties should be considered
      `visitor` - an optional callback for the matched member
    Returns:
      `true` if 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:
      `findMethodOrFail` in class `StaticTypeCheckingVisitor`
    Parameters:
      `expr` - the expression being resolved
      `receiver` - the candidate receiver type
      `name` - the method name
      `args` - the argument types
    Returns:
      the resolved method, or `null` if resolution fails

  - ### isStaticallyCompiled

    public static boolean isStaticallyCompiled(AnnotatedNode node)
    Indicates whether the supplied node is marked for static compilation.
    Parameters:
      `node` - the class or method node to inspect
    Returns:
      `true` if the node is statically compiled