Class TypeResolver


  • public class TypeResolver
    extends Object
    Resolves source-level type, field and method references to JVM names and descriptors, consulting the class being lowered, the class pool and the running JVM in that order.
    • Constructor Detail

      • TypeResolver

        public TypeResolver​(ClassPool classPool,
                            String currentClass)
        Creates a resolver over a class pool and installs its superclass lookup as the frame-generation merge resolver.
        Parameters:
        classPool - the pool holding the classes being lowered
        currentClass - the internal name of the class being lowered
    • Method Detail

      • getClassPool

        public ClassPool getClassPool()
        Returns:
        the class pool
      • getCurrentClass

        public String getCurrentClass()
        Returns:
        the current class
      • setCurrentClassDecl

        public void setCurrentClassDecl​(TypeDecl currentClassDecl)
        Sets the source declaration consulted before the class pool.
        Parameters:
        currentClassDecl - the declaration being lowered
      • setImports

        public void setImports​(List<ImportDecl> imports)
        Sets the imports used to qualify simple type names.
        Parameters:
        imports - the import declarations in source order
      • getSuperclassName

        public String getSuperclassName​(String internalName)
        Resolves the direct superclass, taking user classes from the pool and falling back to loading JDK and system classes.
        Parameters:
        internalName - the class to look up
        Returns:
        the superclass internal name, or null for java/lang/Object, an unresolvable class, or any lookup failure
      • resolveFieldType

        public SourceType resolveFieldType​(String ownerClass,
                                           String fieldName)
        Resolves a field's declared type, searching the source declaration, the pool class and its supertypes and interfaces, then reflection and the current class's original constant pool.
        Parameters:
        ownerClass - the owner's class name, simple or internal
        fieldName - the field name
        Returns:
        the field type, or null when nothing declares the field
      • findFieldType

        public SourceType findFieldType​(String ownerClass,
                                        String fieldName)
        Resolves a field's declared type, searching the current class declaration first, then the field tables of the owner class and its superclasses via the ClassPool.
        Parameters:
        ownerClass - internal name of the declaring class
        fieldName - the field to look up
        Returns:
        the declared type, or null if the field cannot be found
      • isStaticField

        public boolean isStaticField​(String ownerClass,
                                     String fieldName)
        Tests whether a field is declared static, searching the current class declaration first, then the owner class and its superclasses.
        Parameters:
        ownerClass - internal name of the declaring class
        fieldName - the field to look up
        Returns:
        true if the field is static; false if it is not, or cannot be located
      • resolveSamMethod

        public String[] resolveSamMethod​(String interfaceName)
        Resolves the single abstract method of a functional interface, looking the interface up in the ClassPool first, then falling back to a table of common JDK functional interfaces.
        Parameters:
        interfaceName - internal name of the functional interface
        Returns:
        [name, descriptor] of the abstract method, or null if it cannot be determined
      • returnTypeFromDescriptor

        public SourceType returnTypeFromDescriptor​(String methodDescriptor)
        Parses the return type of a method descriptor.
        Parameters:
        methodDescriptor - the method descriptor to parse
        Returns:
        the return type
      • descriptorOfMethod

        public String descriptorOfMethod​(String ownerClass,
                                         String methodName)
        Looks up the descriptor of a directly declared method, used to recover the specific parameter types that a lambda's untyped x -> source form drops.
        Parameters:
        ownerClass - internal name of the declaring class
        methodName - the method name, assumed unique on the class (e.g. a synthetic lambda$...)
        Returns:
        the method descriptor, or null if the class or method is not found
      • findLambdaMethod

        public String[] findLambdaMethod​(String ownerClass,
                                         String enclosingMethod,
                                         int index)
        Finds an already-declared synthetic lambda method by position.
        Parameters:
        ownerClass - internal name of the class declaring the lambda
        enclosingMethod - the method the lambda appears in
        index - position among that method's lambdas, ordered by the trailing counter
        Returns:
        [name, descriptor] of the lambda method, or null if the class or index has no match
      • paramTypesFromDescriptor

        public List<SourceType> paramTypesFromDescriptor​(String methodDescriptor)
        Parses the parameter types of a method descriptor.
        Parameters:
        methodDescriptor - the method descriptor to parse
        Returns:
        the parameter types in declaration order
      • isStaticMethodInCurrentClass

        public boolean isStaticMethodInCurrentClass​(String methodName)
        Tests whether a method of the class being lowered is static, consulting the parsed class declaration (which may hold methods not yet present on the ClassFile) so that unqualified self-calls can be resolved as static or virtual.
        Parameters:
        methodName - the method to look up
        Returns:
        true if the method is static; false if it is not, or is not declared
      • resolveMethodReturnType

        public SourceType resolveMethodReturnType​(String ownerClass,
                                                  String methodName,
                                                  List<SourceType> argTypes)
        Resolves a call's return type, searching the source declaration, the pool class and its supertypes, then reflection when the exact parameter descriptor does not match.
        Parameters:
        ownerClass - the receiver's class name, simple or internal
        methodName - the method name
        argTypes - the argument types at the call site
        Returns:
        the return type, or null when nothing resolves the call
      • resolveArrayElementType

        public SourceType resolveArrayElementType​(SourceType arrayType)
        Strips one level off an array type.
        Parameters:
        arrayType - the array type
        Returns:
        the element type
        Throws:
        LoweringException - if the type is not an array
      • resolveMethodDescriptor

        public String resolveMethodDescriptor​(String ownerClass,
                                              String methodName,
                                              List<IRType> argTypes)
        Resolves the declared descriptor of the best-matching overload, choosing among same-arity candidates by argument-type compatibility (exact descriptor, then primitive/reference kind) and searching the superclass and interfaces.
        Parameters:
        ownerClass - internal name of the receiver class
        methodName - the method name to match
        argTypes - the IR types of the call arguments, in order
        Returns:
        the declared descriptor, or null when the class or a compatible method is not in the pool, leaving the caller to fall back to the argument types
      • functionalConstructorParamType

        public SourceType functionalConstructorParamType​(String ownerClass,
                                                         int arity,
                                                         int index)
        Resolves the declared type of a constructor parameter that expects a functional argument.
        Parameters:
        ownerClass - internal name of the class being constructed
        arity - the number of constructor arguments
        index - position of the parameter to type
        Returns:
        the interface type at that position, or null when no candidate constructor has one or more than one candidate does
      • resolveMethodDescriptorViaReflection

        public String resolveMethodDescriptorViaReflection​(String ownerClass,
                                                           String methodName,
                                                           List<IRType> argTypes)
        Resolves a descriptor against the running JVM, picking the most specific overload the arguments are assignable to.
        Parameters:
        ownerClass - the internal name of the declaring class
        methodName - the method name, or the JVM constructor name to match constructors instead
        argTypes - the argument types at the call site
        Returns:
        the descriptor, or null when the class is not loadable or no overload accepts the arguments
      • isVarargsMethod

        public boolean isVarargsMethod​(String ownerClass,
                                       String methodName,
                                       String descriptor)
        Tests whether the method with this exact descriptor on the owner (or a supertype) is declared ACC_VARARGS, falling back to reflection for classes absent from the pool.
        Parameters:
        ownerClass - internal name of the declaring class
        methodName - the method name
        descriptor - the exact descriptor to match
        Returns:
        true if that method is varargs
      • resolveMethodDescriptor

        public String resolveMethodDescriptor​(String ownerClass,
                                              String methodName,
                                              int expectedParamCount)
        Picks a method descriptor by arity, searching the superclass chain and interfaces when the class declares no overload of that name.
        Parameters:
        ownerClass - the internal name of the class to search
        methodName - the method name
        expectedParamCount - the wanted parameter count, negative to skip arity matching
        Returns:
        the descriptor, or null when nothing declares the method
      • resolveConstructorDescriptor

        public String resolveConstructorDescriptor​(String ownerClass,
                                                   List<IRType> argTypes)
        Picks the constructor whose parameters best match the given argument IR types (exact descriptor preferred, then same primitive/reference kind), disambiguating same-arity overloads such as ArrayList(int) vs ArrayList(Collection).
        Parameters:
        ownerClass - internal name of the class being constructed
        argTypes - the IR types of the call arguments, in order
        Returns:
        the declared constructor descriptor, or null when the class is absent from the pool or no kind-compatible constructor exists, leaving the caller to build one from the argument types
      • resolveConstructorDescriptor

        public String resolveConstructorDescriptor​(String ownerClass,
                                                   int expectedParamCount)
        Picks a constructor descriptor by arity, preferring the no-arg form when the count does not match.
        Parameters:
        ownerClass - the internal name of the class being constructed
        expectedParamCount - the wanted parameter count, negative to skip arity matching
        Returns:
        the descriptor, or "()V" when the class is absent from the pool or declares no constructor
      • findAllMethodDescriptors

        public List<String> findAllMethodDescriptors​(String ownerClass,
                                                     String methodName)
        Collects every overload of a method name, falling back to the superclass and interfaces when the class itself declares none.
        Parameters:
        ownerClass - the internal name of the class to search
        methodName - the method name
        Returns:
        the matching descriptors, empty if the class is absent from the pool or declares no match
      • isStaticMethod

        public boolean isStaticMethod​(String ownerClass,
                                      String methodName,
                                      String descriptor)
        Checks the ACC_STATIC flag of a method declared on a pooled class.
        Parameters:
        ownerClass - the internal name of the declaring class
        methodName - the method name
        descriptor - the method descriptor
        Returns:
        true if the method is declared static, false if it is not or the class is absent from the pool
      • resolveClassName

        public String resolveClassName​(String simpleName)
        Qualifies a type name to its internal form, trying type-variable erasure, explicit and wildcard imports, a java.lang shortlist, the current class, and finally the loaded classes.
        Parameters:
        simpleName - the name as written in source, simple, dotted or already internal
        Returns:
        the internal name, or the input unchanged when nothing resolves it
      • resolveInternalName

        public String resolveInternalName​(String rawName)
        Resolves a parsed type name to its fully-qualified internal name.
        Parameters:
        rawName - the type name as written in source
        Returns:
        the internal name
      • signatureOf

        public String signatureOf​(SourceType type)
        Builds the JVM generic signature of a declared type.
        Parameters:
        type - the declared type
        Returns:
        the signature, or null when the type carries no generic information.
      • descriptorOf

        public String descriptorOf​(ParameterDecl param)
        Builds the descriptor of a declared parameter.
        Parameters:
        param - the declared parameter
        Returns:
        the field descriptor of the parameter's type
      • descriptorOf

        public String descriptorOf​(SourceType type)
        Builds the JVM descriptor for a source type, erasing generics and resolving reference names through the imports and the pool.
        Parameters:
        type - the source type
        Returns:
        the descriptor
      • isInterface

        public boolean isInterface​(String internalName)
        Tests whether a class is an interface, consulting the ClassPool and then reflection.
        Parameters:
        internalName - the class to look up
        Returns:
        true if the class is an interface; false if it is not, or cannot be resolved
      • classExists

        public boolean classExists​(String internalName)
        Tests whether a class is resolvable via the pool - already loaded, or loadable from the system class path.
        Parameters:
        internalName - the class to look up
        Returns:
        true if the class resolves