Package org.codehaus.groovy.control

# Class ResolveVisitor

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeExpressionTransformer
org.codehaus.groovy.control.ResolveVisitor

All Implemented Interfaces:
  `ExpressionTransformer`, `GroovyClassVisitor`, `GroovyCodeVisitor`, `ErrorCollecting`

Direct Known Subclasses:
  `JavaAwareResolveVisitor`

public class ResolveVisitor extends ClassCodeExpressionTransformer
Visitor to resolve types and convert VariableExpression to ClassExpressions if needed. The ResolveVisitor will try to find the Class for a ClassExpression and prints an error if it fails to do so. Constructions like C[], foo as C, (C) foo will force creation of a ClassExpression for C

Note: the method to start the resolving is `startResolving(ClassNode,SourceUnit)`.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `CAST_RESOLVE_HINT`
  Node-metadata key under which a parser may store an explanatory suffix for a `CastExpression` whose type it suspects may fail to resolve — for example a cast produced by a grammar ambiguity.
  `static final String[]`
  `DEFAULT_IMPORTS`
  Default package imports available to Groovy source.
  `static final String[]`
  `EMPTY_STRING_ARRAY`
  Shared empty array constant for import-prefix lookups.
  `static final String`
  `QUESTION_MARK`
  Placeholder name used for wildcard generic arguments.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ResolveVisitor(CompilationUnit compilationUnit)`
  Creates a resolve visitor for the supplied compilation unit.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected SourceUnit`
  `getSourceUnit()`
  Returns the source unit currently being resolved.
  `protected boolean`
  `resolve(ClassNode type)`
  Resolves a type using the standard Groovy lookup rules.
  `protected boolean`
  `resolve(ClassNode type, boolean testModuleImports, boolean testDefaultImports, boolean testStaticInnerClasses)`
  Resolves a type while selectively enabling parts of the standard lookup sequence.
  `protected boolean`
  `resolveFromCompileUnit(ClassNode type)`
  Resolves a type from classes already known to the current compile unit.
  `protected boolean`
  `resolveFromDefaultImports(ClassNode type)`
  Resolves a type through Groovy's default imports.
  `protected boolean`
  `resolveFromDefaultImports(ClassNode type, String[] packagePrefixes)`
  Resolves a type against the supplied import prefixes.
  `protected boolean`
  `resolveFromModule(ClassNode type, boolean testModuleImports)`
  Resolves a type using module package, regular imports, star imports, and module-expanded imports.
  `protected boolean`
  `resolveFromStaticInnerClasses(ClassNode type)`
  Resolves a qualified type name by treating suffix segments as static inner classes.
  `protected boolean`
  `resolveNestedClass(ClassNode type)`
  Resolves a type name against nested classes visible from the current class hierarchy.
  `protected boolean`
  `resolveToInner(ClassNode type)`
  Attempts to resolve an unqualified type against visible inner classes.
  `protected boolean`
  `resolveToOuter(ClassNode type)`
  Resolves a type via the configured `ClassNodeResolver`.
  `void`
  `setClassNodeResolver(ClassNodeResolver classNodeResolver)`
  Replaces the class-node resolver used for classpath lookups.
  `void`
  `startResolving(ClassNode node, SourceUnit source)`
  Starts resolving class references from the supplied class node.
  `Expression`
  `transform(Expression exp)`
  Resolves and rewrites expressions while traversing the current class.
  `protected Expression`
  `transformAnnotationConstantExpression(AnnotationConstantExpression ace)`
  Resolves annotation constants encountered in expression form.
  `protected Expression`
  `transformBinaryExpression(BinaryExpression be)`
  Resolves type references appearing in binary expressions.
  `protected Expression`
  `transformClosureExpression(ClosureExpression ce)`
  Resolves declared types and defaults inside a closure expression.
  `protected Expression`
  `transformConstructorCallExpression(ConstructorCallExpression cce)`
  Resolves constructor call target types.
  `protected Expression`
  `transformDeclarationExpression(DeclarationExpression de)`
  Resolves declared variable types in a declaration expression.
  `protected Expression`
  `transformMethodCallExpression(MethodCallExpression mce)`
  Resolves receiver, method, and argument types for a method call.
  `protected Expression`
  `transformPropertyExpression(PropertyExpression pe)`
  Resolves type references embedded in a property expression.
  `protected Expression`
  `transformVariableExpression(VariableExpression ve)`
  Resolves variable expressions that may actually reference types.
  `protected void`
  `visitAnnotation(AnnotationNode node)`
  Resolves an annotation type and its member values.
  `void`
  `visitBlockStatement(BlockStatement block)`
  Updates the current variable scope while visiting a block statement.
  `void`
  `visitCatchStatement(CatchStatement cs)`
  Resolves the declared exception type for a catch parameter.
  `void`
  `visitClass(ClassNode node)`
  Resolves imports, headers, and member types for the supplied class.
  `protected void`
  `visitConstructorOrMethod(MethodNode node, boolean isConstructor)`
  Resolves types referenced by a constructor or method signature.
  `void`
  `visitField(FieldNode node)`
  Resolves the declared type of a field before visiting its contents.
  `void`
  `visitForLoop(ForStatement forLoop)`
  Resolves the declared loop variable type for a `for` statement.
  `void`
  `visitProperty(PropertyNode node)`
  Resolves the declared type of a property before visiting its contents.
  `protected void`
  `visitStatementAnnotations(Statement statement)`
  Resolves the class nodes of any annotations attached to a loop statement (stored in statement metadata rather than in `AnnotatedNode`).

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

  `setSourcePosition, visitAssertStatement, visitCaseStatement, visitClosureExpression, visitDoWhileLoop, visitExpressionStatement, visitIfElse, visitReturnStatement, visitSwitch, visitSwitchExpression, visitSynchronizedStatement, visitThrowStatement, visitWhileLoop, visitYieldStatement`

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

  `addError, visitAnnotations, visitAnnotations, visitBreakStatement, visitClassCodeContainer, visitConstructor, visitContinueStatement, visitDeclarationExpression, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitStatement, visitTryCatchFinally`

  ### Methods 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, visitTernaryExpression, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression`

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

  - ### DEFAULT_IMPORTS

    public static final String[] DEFAULT_IMPORTS
    Default package imports available to Groovy source.

  - ### EMPTY_STRING_ARRAY

    public static final String[] EMPTY_STRING_ARRAY
    Shared empty array constant for import-prefix lookups.

  - ### QUESTION_MARK

    public static final String QUESTION_MARK
    Placeholder name used for wildcard generic arguments.
    See Also:
      - Constant Field Values

  - ### CAST_RESOLVE_HINT

    public static final String CAST_RESOLVE_HINT
    Node-metadata key under which a parser may store an explanatory suffix for a `CastExpression` whose type it suspects may fail to resolve — for example a cast produced by a grammar ambiguity. When the cast's type cannot be resolved, the stored text is appended to the `unable to resolve class` error message. The value is the complete hint text; this visitor attaches no meaning to it.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### ResolveVisitor

    public ResolveVisitor(CompilationUnit compilationUnit)
    Creates a resolve visitor for the supplied compilation unit.
    Parameters:
      `compilationUnit` - the compilation unit being resolved

- ## Method Details

  - ### setClassNodeResolver

    public void setClassNodeResolver(ClassNodeResolver classNodeResolver)
    Replaces the class-node resolver used for classpath lookups.
    Parameters:
      `classNodeResolver` - the resolver to use

  - ### startResolving

    public void startResolving(ClassNode node, SourceUnit source)
    Starts resolving class references from the supplied class node.
    Parameters:
      `node` - the class to resolve
      `source` - the source unit containing the class

  - ### getSourceUnit

    protected SourceUnit getSourceUnit()
    Returns the source unit currently being resolved.
    Specified by:
      `getSourceUnit` in class `ClassCodeVisitorSupport`
    Returns:
      the active source unit

  - ### visitField

    public void visitField(FieldNode node)
    Resolves the declared type of a field before visiting its contents.
    Specified by:
      `visitField` in interface `GroovyClassVisitor`
    Overrides:
      `visitField` in class `ClassCodeExpressionTransformer`
    Parameters:
      `node` - the field to visit

  - ### visitProperty

    public void visitProperty(PropertyNode node)
    Resolves the declared type of a property before visiting its contents.
    Specified by:
      `visitProperty` in interface `GroovyClassVisitor`
    Overrides:
      `visitProperty` in class `ClassCodeExpressionTransformer`
    Parameters:
      `node` - the property to visit

  - ### visitConstructorOrMethod

    protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)
    Resolves types referenced by a constructor or method signature.
    Overrides:
      `visitConstructorOrMethod` in class `ClassCodeExpressionTransformer`
    Parameters:
      `node` - the executable member to inspect
      `isConstructor` - whether `node` is a constructor

  - ### resolveToInner

    protected boolean resolveToInner(ClassNode type)
    Attempts to resolve an unqualified type against visible inner classes.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolve

    protected boolean resolve(ClassNode type)
    Resolves a type using the standard Groovy lookup rules.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolve

    protected boolean resolve(ClassNode type, boolean testModuleImports, boolean testDefaultImports, boolean testStaticInnerClasses)
    Resolves a type while selectively enabling parts of the standard lookup sequence.
    Parameters:
      `type` - the type to resolve
      `testModuleImports` - whether module imports should be consulted
      `testDefaultImports` - whether default imports should be consulted
      `testStaticInnerClasses` - whether static inner-class resolution should be attempted
    Returns:
      `true` if the type was resolved

  - ### resolveNestedClass

    protected boolean resolveNestedClass(ClassNode type)
    Resolves a type name against nested classes visible from the current class hierarchy.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolveFromStaticInnerClasses

    protected boolean resolveFromStaticInnerClasses(ClassNode type)
    Resolves a qualified type name by treating suffix segments as static inner classes.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolveFromDefaultImports

    protected boolean resolveFromDefaultImports(ClassNode type)
    Resolves a type through Groovy's default imports.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolveFromDefaultImports

    protected boolean resolveFromDefaultImports(ClassNode type, String[] packagePrefixes)
    Resolves a type against the supplied import prefixes.
    Parameters:
      `type` - the type to resolve
      `packagePrefixes` - the package prefixes to test
    Returns:
      `true` if the type was resolved

  - ### resolveFromCompileUnit

    protected boolean resolveFromCompileUnit(ClassNode type)
    Resolves a type from classes already known to the current compile unit.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### resolveFromModule

    protected boolean resolveFromModule(ClassNode type, boolean testModuleImports)
    Resolves a type using module package, regular imports, star imports, and module-expanded imports.
    Parameters:
      `type` - the type to resolve
      `testModuleImports` - whether module-level imports should be consulted
    Returns:
      `true` if the type was resolved

  - ### resolveToOuter

    protected boolean resolveToOuter(ClassNode type)
    Resolves a type via the configured `ClassNodeResolver`.
    Parameters:
      `type` - the type to resolve
    Returns:
      `true` if the type was resolved

  - ### transform

    public Expression transform(Expression exp)
    Resolves and rewrites expressions while traversing the current class.
    Specified by:
      `transform` in interface `ExpressionTransformer`
    Overrides:
      `transform` in class `ClassCodeExpressionTransformer`
    Parameters:
      `exp` - the expression to transform
    Returns:
      the transformed expression

  - ### transformPropertyExpression

    protected Expression transformPropertyExpression(PropertyExpression pe)
    Resolves type references embedded in a property expression.
    Parameters:
      `pe` - the property expression to transform
    Returns:
      the transformed expression

  - ### transformVariableExpression

    protected Expression transformVariableExpression(VariableExpression ve)
    Resolves variable expressions that may actually reference types.
    Parameters:
      `ve` - the variable expression to transform
    Returns:
      the transformed expression

  - ### transformBinaryExpression

    protected Expression transformBinaryExpression(BinaryExpression be)
    Resolves type references appearing in binary expressions.
    Parameters:
      `be` - the binary expression to transform
    Returns:
      the transformed expression

  - ### transformClosureExpression

    protected Expression transformClosureExpression(ClosureExpression ce)
    Resolves declared types and defaults inside a closure expression.
    Parameters:
      `ce` - the closure expression to transform
    Returns:
      the transformed expression

  - ### transformConstructorCallExpression

    protected Expression transformConstructorCallExpression(ConstructorCallExpression cce)
    Resolves constructor call target types.
    Parameters:
      `cce` - the constructor call to transform
    Returns:
      the transformed expression

  - ### transformMethodCallExpression

    protected Expression transformMethodCallExpression(MethodCallExpression mce)
    Resolves receiver, method, and argument types for a method call.
    Parameters:
      `mce` - the method call to transform
    Returns:
      the transformed expression

  - ### transformDeclarationExpression

    protected Expression transformDeclarationExpression(DeclarationExpression de)
    Resolves declared variable types in a declaration expression.
    Parameters:
      `de` - the declaration expression to transform
    Returns:
      the transformed expression

  - ### transformAnnotationConstantExpression

    protected Expression transformAnnotationConstantExpression(AnnotationConstantExpression ace)
    Resolves annotation constants encountered in expression form.
    Parameters:
      `ace` - the annotation constant expression to transform
    Returns:
      the original expression

  - ### visitAnnotation

    protected void visitAnnotation(AnnotationNode node)
    Resolves an annotation type and its member values.
    Overrides:
      `visitAnnotation` in class `ClassCodeExpressionTransformer`
    Parameters:
      `node` - the annotation to visit

  - ### visitClass

    public void visitClass(ClassNode node)
    Resolves imports, headers, and member types for the supplied class.
    Specified by:
      `visitClass` in interface `GroovyClassVisitor`
    Overrides:
      `visitClass` in class `ClassCodeVisitorSupport`
    Parameters:
      `node` - the class to visit

  - ### visitCatchStatement

    public void visitCatchStatement(CatchStatement cs)
    Resolves the declared exception type for a catch parameter.
    Specified by:
      `visitCatchStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCatchStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `cs` - the catch statement to visit

  - ### visitStatementAnnotations

    protected void visitStatementAnnotations(Statement statement)
    Resolves the class nodes of any annotations attached to a loop statement (stored in statement metadata rather than in `AnnotatedNode`).
    Overrides:
      `visitStatementAnnotations` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the loop statement that may have statement-level annotations

  - ### visitForLoop

    public void visitForLoop(ForStatement forLoop)
    Resolves the declared loop variable type for a `for` statement.
    Specified by:
      `visitForLoop` in interface `GroovyCodeVisitor`
    Overrides:
      `visitForLoop` in class `ClassCodeExpressionTransformer`
    Parameters:
      `forLoop` - the loop to visit

  - ### visitBlockStatement

    public void visitBlockStatement(BlockStatement block)
    Updates the current variable scope while visiting a block statement.
    Specified by:
      `visitBlockStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBlockStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `block` - the block to visit
    See Also:
      - `CodeVisitorSupport.visitForLoop(ForStatement)`
      - `CodeVisitorSupport.visitWhileLoop(WhileStatement)`