Class 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 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:
  • 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: