Class Resolver

java.lang.Object
org.checkerframework.javacutil.Resolver

public class Resolver extends Object
A utility class to find symbols corresponding to string references (identifiers).
  • Constructor Details

  • Method Details

    • getEnvForPath

      public com.sun.tools.javac.comp.Env<com.sun.tools.javac.comp.AttrContext> getEnvForPath(TreePath path)
      Determine the environment for the given path. The environment is cached per leaf, and the expression statements of a run of expression statements share one environment (see pathWithSameScope(com.sun.source.util.TreePath)).

      The leaf of path must be a tree of the compilation unit; for an artificial tree, pass the path of the tree it was created for (see StringToJavaExpression#atPath).

      Parameters:
      path - the tree path to the local scope
      Returns:
      the corresponding attribution environment
    • findPackage

      public @Nullable com.sun.tools.javac.code.Symbol.PackageSymbol findPackage(String name, TreePath path)
      Finds the package with name name.
      Parameters:
      name - the name of the package
      path - the tree path to the local scope
      Returns:
      the PackageSymbol for the package if it is found, null otherwise
    • findField

      public @Nullable VariableElement findField(String name, TypeMirror type, TreePath path)
      Finds the field with name name in type or a superclass or superinterface of type.

      The method adheres to all the rules of Java's scoping (while also considering the imports) for name resolution.

      Parameters:
      name - the name of the field
      type - the type of the receiver (i.e., the type in which to look for the field)
      path - the tree path to the local scope
      Returns:
      the element for the field, null otherwise
    • findLocalVariableOrParameter

      public @Nullable VariableElement findLocalVariableOrParameter(String name, TreePath path)
      Finds the local variable (including formal parameters) with name name in the given scope.

      If the declaration of the variable is found in the source (see localDeclarationInScope(com.sun.tools.javac.util.Name, com.sun.source.util.TreePath)), the result is the symbol of that declaration. Otherwise, as for a pattern variable, a variable of an earlier case of an old-style switch, or a local variable of an enclosing method used in a local or anonymous class, the result is the symbol from javac's attributed copy of the enclosing code, which Trees#getScope creates. The two are different objects, and they differ in flags: the copy can lack EFFECTIVELY_FINAL, so that ElementUtils.isEffectivelyFinal(javax.lang.model.element.Element) is false for it, which makes the Lock Checker report lock.expression.not.final for an effectively final variable (eisop#2375). A caller that compares the result with another symbol by identity, or that tests its flags, must allow for either. LocalVariable compares variables by name, position, and owner, so both give the same LocalVariable.

      Parameters:
      name - the name of the local variable
      path - the tree path to the local scope
      Returns:
      the element for the local variable, null otherwise
    • findClass

      public Element findClass(String name, TreePath path)
      Finds the class literal with name name.

      The method adheres to all the rules of Java's scoping (while also considering the imports) for name resolution.

      Parameters:
      name - the name of the class
      path - the tree path to the local scope
      Returns:
      the element for the class
    • findClassInPackage

      public @Nullable com.sun.tools.javac.code.Symbol.ClassSymbol findClassInPackage(String name, com.sun.tools.javac.code.Symbol.PackageSymbol pck, TreePath path)
      Finds the class with name name in a given package.
      Parameters:
      name - the name of the class
      pck - the PackageSymbol for the package
      path - the tree path to the local scope
      Returns:
      the ClassSymbol for the class if it is found, null otherwise
    • findMethod

      public @Nullable ExecutableElement findMethod(String methodName, TypeMirror receiverType, TreePath path, List<TypeMirror> argumentTypes)
      Finds the method element for a given name and list of expected parameter types.

      The method adheres to all the rules of Java's scoping (while also considering the imports) for name resolution.

      (This method takes into account autoboxing.)

      This method is a wrapper around com.sun.tools.javac.comp.Resolve.findMethod.

      Parameters:
      methodName - name of the method to find
      receiverType - type of the receiver of the method
      path - tree path
      argumentTypes - types of arguments passed to the method call
      Returns:
      the method element (if found)
    • buildMethodContext

      Build an instance of Resolve$MethodResolutionContext.
      Returns:
      a MethodResolutionContext
      Throws:
      InstantiationException - if there is trouble constructing the instance
      IllegalAccessException - if there is trouble constructing the instance
      InvocationTargetException - if there is trouble constructing the instance