Class CallGraph


  • public class CallGraph
    extends Object
    A queryable call graph over a set of classes.
    • Method Detail

      • build

        public static CallGraph build​(ClassPool classPool)
        Builds a call graph from the given ClassPool.
        Parameters:
        classPool - the classes to scan for call sites
        Returns:
        the populated call graph
      • getNode

        public CallGraphNode getNode​(MethodReference ref)
        Gets the node for a method reference.
        Parameters:
        ref - the method to look up
        Returns:
        the node, or null if the method is not in the graph
      • getNode

        public CallGraphNode getNode​(String owner,
                                     String name,
                                     String descriptor)
        Gets the node for a method by owner, name, and descriptor.
        Parameters:
        owner - the internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        the node, or null if the method is not in the graph
      • getNode

        public CallGraphNode getNode​(MethodEntry method)
        Gets the node for a MethodEntry.
        Parameters:
        method - the method to look up
        Returns:
        the node, or null if the method is not in the graph
      • getAllNodes

        public Collection<CallGraphNode> getAllNodes()
        Returns all nodes in the call graph.
        Returns:
        an unmodifiable view of every node
      • getPoolNodes

        public Collection<CallGraphNode> getPoolNodes()
        Returns only nodes that are in the ClassPool (not external references).
        Returns:
        the in-pool nodes
      • getCallers

        public Set<MethodReference> getCallers​(MethodReference method)
        Gets all methods that call the specified method.
        Parameters:
        method - the called method
        Returns:
        its callers, empty if the method has no node
      • getCallers

        public Set<MethodReference> getCallers​(String owner,
                                               String name,
                                               String descriptor)
        Gets all methods that call the specified method.
        Parameters:
        owner - the internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        its callers, empty if the method has no node
      • getCallers

        public Set<MethodReference> getCallers​(MethodEntry method)
        Gets all methods that call the specified method entry.
        Parameters:
        method - the called method
        Returns:
        its callers, empty if the method has no node
      • getCallees

        public Set<MethodReference> getCallees​(MethodReference method)
        Gets all methods called by the specified method.
        Parameters:
        method - the calling method
        Returns:
        its callees, empty if the method has no node
      • getCallees

        public Set<MethodReference> getCallees​(String owner,
                                               String name,
                                               String descriptor)
        Gets all methods called by the specified method.
        Parameters:
        owner - the internal name of the declaring class
        name - the method name
        descriptor - the method descriptor
        Returns:
        its callees, empty if the method has no node
      • getCallees

        public Set<MethodReference> getCallees​(MethodEntry method)
        Gets all methods called by the specified method entry.
        Parameters:
        method - the calling method
        Returns:
        its callees, empty if the method has no node
      • getCallSitesFor

        public Set<CallSite> getCallSitesFor​(MethodReference method)
        Gets all call sites where the specified method is called.
        Parameters:
        method - the called method
        Returns:
        its incoming call sites, empty if the method has no node
      • getCallSitesFrom

        public Set<CallSite> getCallSitesFrom​(MethodReference method)
        Gets all call sites made from the specified method.
        Parameters:
        method - the calling method
        Returns:
        its outgoing call sites, empty if the method has no node
      • getReachableFrom

        public Set<MethodReference> getReachableFrom​(Collection<MethodReference> entryPoints)
        Computes all methods reachable from the given entry points.
        Parameters:
        entryPoints - the methods to start from
        Returns:
        all methods reachable via the call graph
      • getReachableFromMainEntryPoints

        public Set<MethodReference> getReachableFromMainEntryPoints()
        Computes all methods reachable from main methods and static initializers.
        Returns:
        every method reachable from those in-pool entry points
      • getUnreachableFrom

        public Set<MethodReference> getUnreachableFrom​(Collection<MethodReference> entryPoints)
        Finds all methods that are not reachable from the given entry points.
        Parameters:
        entryPoints - the methods to start from
        Returns:
        methods that are not reachable
      • findMethodsWithNoCallers

        public Set<MethodReference> findMethodsWithNoCallers()
        Finds methods with no callers (potential dead code).
        Returns:
        in-pool methods that no call site targets
      • findMethods

        public Set<MethodReference> findMethods​(Predicate<CallGraphNode> predicate)
        Finds all methods whose node matches a predicate.
        Parameters:
        predicate - the node test
        Returns:
        the references of the matching nodes, in insertion order
      • calls

        public boolean calls​(MethodReference caller,
                             MethodReference callee)
        Checks for a direct call edge between two methods.
        Parameters:
        caller - the calling method
        callee - the called method
        Returns:
        true if the caller has the callee among its callees
      • canReach

        public boolean canReach​(MethodReference from,
                                MethodReference to)
        Checks whether one method can reach another transitively.
        Parameters:
        from - the method to start from
        to - the method to look for
        Returns:
        true if a chain of calls leads from one to the other
      • resolveVirtualTargets

        public Set<MethodReference> resolveVirtualTargets​(String owner,
                                                          String name,
                                                          String descriptor)
        Gets all possible targets for a virtual/interface call.
        Parameters:
        owner - the declared owner class
        name - the method name
        descriptor - the method descriptor
        Returns:
        all possible implementation methods
      • getHierarchy

        public ClassHierarchy getHierarchy()
        Returns:
        the class hierarchy used for virtual dispatch resolution, or null if none was supplied
      • getClassPool

        public ClassPool getClassPool()
        Returns:
        the class pool this call graph was built from
      • size

        public int size()
        Returns the number of methods in the call graph.
        Returns:
        the node count, including out-of-pool references
      • edgeCount

        public int edgeCount()
        Returns the number of call edges in the graph.
        Returns:
        the total outgoing call count summed over every node