Class ClassHierarchy

  • All Implemented Interfaces:
    IClassHierarchy, java.lang.Iterable<IClass>

    public class ClassHierarchy
    extends java.lang.Object
    implements IClassHierarchy
    Simple implementation of a class hierarchy.

    Note that this class hierarchy implementation is mutable. You can add classes via addClass(). You can add a class even if c.getClassLoader() does not appear in getLoaders().

    • Method Detail

      • addClass

        public boolean addClass​(IClass klass)
        Specified by:
        addClass in interface IClassHierarchy
        Returns:
        true if the add succeeded; false if it failed for some reason
        Throws:
        java.lang.IllegalArgumentException - if klass is null
      • getPossibleTargets

        public java.util.Set<IMethod> getPossibleTargets​(MethodReference ref)
        Find the possible targets of a call to a method reference. Note that if the reference is to an instance initialization method, we assume the method was called with invokespecial rather than invokevirtual.
        Specified by:
        getPossibleTargets in interface IClassHierarchy
        Parameters:
        ref - method reference
        Returns:
        the set of IMethods that this call can resolve to.
        Throws:
        java.lang.IllegalArgumentException - if ref is null
      • getPossibleTargets

        public java.util.Set<IMethod> getPossibleTargets​(IClass declaredClass,
                                                         MethodReference ref)
        Find the possible receivers of a call to a method reference
        Specified by:
        getPossibleTargets in interface IClassHierarchy
        Parameters:
        ref - method reference
        declaredClass - the class of the receiver
        Returns:
        the set of IMethods that this call can resolve to.
      • resolveMethod

        public IMethod resolveMethod​(MethodReference m)
        Description copied from interface: IClassHierarchy
        Return the unique receiver of an invocation of method on an object of type m.getDeclaredClass(). Note that for Java, m.getDeclaredClass() must represent a class, not an interface. This method does not work for finding inherited methods in interfaces.
        Specified by:
        resolveMethod in interface IClassHierarchy
        Returns:
        IMethod, or null if no appropriate receiver is found.
      • resolveField

        public IField resolveField​(FieldReference f)
        Specified by:
        resolveField in interface IClassHierarchy
        Returns:
        the canonical IField that represents a given field , or null if none found
        Throws:
        java.lang.IllegalArgumentException - if f is null
      • resolveField

        public IField resolveField​(IClass klass,
                                   FieldReference f)
        Specified by:
        resolveField in interface IClassHierarchy
        Returns:
        the canonical IField that represents a given field , or null if none found
        Throws:
        java.lang.IllegalArgumentException - if f is null
        java.lang.IllegalArgumentException - if klass is null
      • resolveMethod

        public IMethod resolveMethod​(IClass receiverClass,
                                     Selector selector)
        Description copied from interface: IClassHierarchy
        Return the unique target of an invocation of method on an object of type receiverClass
        Specified by:
        resolveMethod in interface IClassHierarchy
        Parameters:
        receiverClass - type of receiver. Note that for Java, receiverClass must represent a class, not an interface. This method does not work for finding inherited methods in interfaces.
        selector - method signature
        Returns:
        Method resolved method abstraction
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • lookupClass

        public IClass lookupClass​(TypeReference a)
        Find a class in this class hierarchy.
        Specified by:
        lookupClass in interface IClassHierarchy
        Returns:
        the IClass for a if found; null if can't find the class.
        Throws:
        java.lang.IllegalArgumentException - if A is null
      • isSubclassOf

        public boolean isSubclassOf​(IClass c,
                                    IClass t)
        Is c a subclass of T?
        Specified by:
        isSubclassOf in interface IClassHierarchy
        Throws:
        java.lang.IllegalArgumentException - if c is null
      • implementsInterface

        public boolean implementsInterface​(IClass c,
                                           IClass i)
        Does c implement i?
        Specified by:
        implementsInterface in interface IClassHierarchy
        Returns:
        true iff i is an interface and c is a class that implements i, or c is an interface that extends i.
      • computeSubClasses

        public java.util.Collection<IClass> computeSubClasses​(TypeReference type)
        Return set of all subclasses of type in the Class Hierarchy TODO: Tune this implementation. Consider caching if necessary.
        Specified by:
        computeSubClasses in interface IClassHierarchy
      • getJavaLangErrorTypes

        public java.util.Collection<TypeReference> getJavaLangErrorTypes()
        Solely for optimization; return a Collection<TypeReference> representing the subclasses of Error

        kind of ugly. a better scheme?

        Specified by:
        getJavaLangErrorTypes in interface IClassHierarchy
      • getJavaLangRuntimeExceptionTypes

        public java.util.Collection<TypeReference> getJavaLangRuntimeExceptionTypes()
        Solely for optimization; return a Collection<TypeReference> representing the subclasses of RuntimeException

        kind of ugly. a better scheme?

        Specified by:
        getJavaLangRuntimeExceptionTypes in interface IClassHierarchy
      • getImplementors

        public java.util.Set<IClass> getImplementors​(TypeReference type)
        TODO: tune this if necessary
        Specified by:
        getImplementors in interface IClassHierarchy
        Parameters:
        type - an interface
        Returns:
        Set of IClass that represent implementors of the interface
      • iterator

        public java.util.Iterator<IClass> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<IClass>
      • getNumberOfClasses

        public int getNumberOfClasses()
        Specified by:
        getNumberOfClasses in interface IClassHierarchy
        Returns:
        The number of classes present in the class hierarchy.
      • getNumberOfImmediateSubclasses

        public int getNumberOfImmediateSubclasses​(IClass klass)
        Specified by:
        getNumberOfImmediateSubclasses in interface IClassHierarchy
        Returns:
        the number of classes that immediately extend klass. if klass is an array class A[][]...[], we return number of immediate subclasses of A. If A is primitive, we return 0.
      • getImmediateSubclasses

        public java.util.Collection<IClass> getImmediateSubclasses​(IClass klass)
        Specified by:
        getImmediateSubclasses in interface IClassHierarchy
        Returns:
        the classes that immediately extend klass. if klass is an array class A[][]...[], we return array classes B[][]...[] (same dimensionality) where B is an immediate subclass of A. If A is primitive, we return the empty set.
      • isRootClass

        public boolean isRootClass​(IClass c)
                            throws java.lang.IllegalArgumentException
        Specified by:
        isRootClass in interface IClassHierarchy
        Throws:
        java.lang.IllegalArgumentException
      • isAssignableFrom

        public boolean isAssignableFrom​(IClass c1,
                                        IClass c2)
        Does an expression c1 x := c2 y typecheck?

        i.e. is c2 a subtype of c1?

        Specified by:
        isAssignableFrom in interface IClassHierarchy
        Throws:
        java.lang.IllegalArgumentException - if c1 is null
        java.lang.IllegalArgumentException - if c2 is null