Class Resolver


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

      Constructors 
      Constructor Description
      Resolver​(javax.annotation.processing.ProcessingEnvironment env)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Object buildMethodContext()
      Build an instance of Resolve$MethodResolutionContext.
      javax.lang.model.element.Element findClass​(java.lang.String name, com.sun.source.util.TreePath path)
      Finds the class literal with name name.
      @Nullable com.sun.tools.javac.code.Symbol.ClassSymbol findClassInPackage​(java.lang.String name, com.sun.tools.javac.code.Symbol.PackageSymbol pck, com.sun.source.util.TreePath path)
      Finds the class with name name in a given package.
      @Nullable javax.lang.model.element.VariableElement findField​(java.lang.String name, javax.lang.model.type.TypeMirror type, com.sun.source.util.TreePath path)
      Finds the field with name name in type or a superclass or superinterface of type.
      @Nullable javax.lang.model.element.VariableElement findLocalVariableOrParameter​(java.lang.String name, com.sun.source.util.TreePath path)
      Finds the local variable (including formal parameters) with name name in the given scope.
      @Nullable javax.lang.model.element.ExecutableElement findMethod​(java.lang.String methodName, javax.lang.model.type.TypeMirror receiverType, com.sun.source.util.TreePath path, java.util.List<javax.lang.model.type.TypeMirror> argumentTypes)
      Finds the method element for a given name and list of expected parameter types.
      @Nullable com.sun.tools.javac.code.Symbol.PackageSymbol findPackage​(java.lang.String name, com.sun.source.util.TreePath path)
      Finds the package with name name.
      com.sun.tools.javac.comp.Env<com.sun.tools.javac.comp.AttrContext> getEnvForPath​(com.sun.source.util.TreePath path)
      Determine the environment for the given path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Resolver

        public Resolver​(javax.annotation.processing.ProcessingEnvironment env)
    • Method Detail

      • getEnvForPath

        public com.sun.tools.javac.comp.Env<com.sun.tools.javac.comp.AttrContext> getEnvForPath​(com.sun.source.util.TreePath path)
        Determine the environment for the given path.
        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​(java.lang.String name,
                                                                                   com.sun.source.util.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 javax.lang.model.element.VariableElement findField​(java.lang.String name,
                                                                            javax.lang.model.type.TypeMirror type,
                                                                            com.sun.source.util.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 javax.lang.model.element.VariableElement findLocalVariableOrParameter​(java.lang.String name,
                                                                                               com.sun.source.util.TreePath path)
        Finds the local variable (including formal parameters) with name name in the given scope.
        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 javax.lang.model.element.Element findClass​(java.lang.String name,
                                                          com.sun.source.util.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​(java.lang.String name,
                                                                                        com.sun.tools.javac.code.Symbol.PackageSymbol pck,
                                                                                        com.sun.source.util.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 javax.lang.model.element.ExecutableElement findMethod​(java.lang.String methodName,
                                                                               javax.lang.model.type.TypeMirror receiverType,
                                                                               com.sun.source.util.TreePath path,
                                                                               java.util.List<javax.lang.model.type.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

        protected java.lang.Object buildMethodContext()
                                               throws java.lang.ClassNotFoundException,
                                                      java.lang.InstantiationException,
                                                      java.lang.IllegalAccessException,
                                                      java.lang.reflect.InvocationTargetException,
                                                      java.lang.NoSuchFieldException
        Build an instance of Resolve$MethodResolutionContext.
        Returns:
        a MethodResolutionContext
        Throws:
        java.lang.ClassNotFoundException - if there is trouble constructing the instance
        java.lang.InstantiationException - if there is trouble constructing the instance
        java.lang.IllegalAccessException - if there is trouble constructing the instance
        java.lang.reflect.InvocationTargetException - if there is trouble constructing the instance
        java.lang.NoSuchFieldException - if there is trouble constructing the instance