Class ClassResolver


  • public class ClassResolver
    extends Object
    Resolver of classes, methods, and fields against a class pool and its hierarchy, with memoized lookups.
    • Constructor Detail

      • ClassResolver

        public ClassResolver​(ClassPool classPool)
        Creates a resolver that builds its own hierarchy from the pool.
        Parameters:
        classPool - the pool to resolve against
      • ClassResolver

        public ClassResolver​(ClassPool classPool,
                             ClassHierarchy hierarchy)
        Creates a resolver using a prebuilt hierarchy.
        Parameters:
        classPool - the pool to resolve against
        hierarchy - the hierarchy to consult for supertype walks
    • Method Detail

      • getClassPool

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

        public ClassHierarchy getHierarchy()
        Returns:
        the hierarchy
      • resolveClass

        public ClassFile resolveClass​(String className)
        Looks up a class in the pool, caching the result.
        Parameters:
        className - internal name of the class
        Returns:
        the resolved class file
        Throws:
        ResolutionException - if the class is not in the pool
      • registerClass

        public void registerClass​(ClassFile classFile)
        Adds a class file to the pool.
        Parameters:
        classFile - the class to register
      • resolveMethod

        public ResolvedMethod resolveMethod​(String owner,
                                            String name,
                                            String descriptor)
        Resolves a method by searching the owner, then its superclasses, then inherited interfaces.
        Parameters:
        owner - internal name of the class named in the reference
        name - method name
        descriptor - method descriptor
        Returns:
        the resolved method with its invoke kind
        Throws:
        ResolutionException - if the owner or the method cannot be found
      • resolveVirtualMethod

        public ResolvedMethod resolveVirtualMethod​(String type,
                                                   String name,
                                                   String desc)
        Resolves a concrete implementation for virtual dispatch, skipping abstract methods.
        Parameters:
        type - internal name of the receiver's runtime class
        name - method name
        desc - method descriptor
        Returns:
        the resolved concrete method
        Throws:
        ResolutionException - if no concrete implementation exists
      • resolveInterfaceMethod

        public ResolvedMethod resolveInterfaceMethod​(String interfaceName,
                                                     String name,
                                                     String desc)
        Resolves an interface method, searching the interface then its superinterfaces.
        Parameters:
        interfaceName - internal name of the interface
        name - method name
        desc - method descriptor
        Returns:
        the resolved method with INTERFACE invoke kind
        Throws:
        ResolutionException - if the interface or the method cannot be found
      • resolveSpecialMethod

        public ResolvedMethod resolveSpecialMethod​(String owner,
                                                   String name,
                                                   String desc)
        Resolves a method declared directly on the named owner for invokespecial dispatch.
        Parameters:
        owner - internal name of the declaring class
        name - method name
        desc - method descriptor
        Returns:
        the resolved method with SPECIAL invoke kind
        Throws:
        ResolutionException - if the owner or the method cannot be found
      • resolveField

        public ResolvedField resolveField​(String owner,
                                          String name,
                                          String descriptor)
        Resolves a field by searching the owner, then its superclasses, then inherited interfaces.
        Parameters:
        owner - internal name of the class named in the reference
        name - field name
        descriptor - field descriptor
        Returns:
        the resolved field with its declaring class
        Throws:
        ResolutionException - if the owner or the field cannot be found
      • isAssignableFrom

        public boolean isAssignableFrom​(String targetType,
                                        String sourceType)
        Tests reference assignability, handling Object, covariant arrays, and the class hierarchy.
        Parameters:
        targetType - internal name or array descriptor of the assignment target
        sourceType - internal name or array descriptor of the value being assigned
        Returns:
        true if a value of the source type can be assigned to the target type
      • getSuperclass

        public String getSuperclass​(String className)
        Looks up the superclass of a pooled class.
        Parameters:
        className - internal name of the class
        Returns:
        the superclass internal name, or null if the class is not pooled
      • getInterfaces

        public List<String> getInterfaces​(String className)
        Lists the directly implemented interfaces of a class known to the hierarchy.
        Parameters:
        className - internal name of the class
        Returns:
        the interface names, empty if the class is unknown
      • invalidateCache

        public void invalidateCache()
        Clears all memoized resolution results.
      • hasField

        public boolean hasField​(String className,
                                String fieldName,
                                String descriptor)
        Tests whether a pooled class directly declares a matching field.
        Parameters:
        className - internal name of the class
        fieldName - field name
        descriptor - field descriptor
        Returns:
        true if the class is pooled and declares the field
      • usesCompactStrings

        public boolean usesCompactStrings()
        Detects compact strings by probing the pooled String class for its coder field.
        Returns:
        true if java/lang/String declares a byte coder field