Skip to content

Call graph

Which method can call which, over the whole program. See the call graph guide.

CallGraph

The call graph of the whole program: which method can call which.

Get it from AnalysisContext.call_graph. It is built on the first read and kept.

nodes property

nodes: CallNodeView

Every method in the graph, as a read-only sequence that copies nothing.

edges property

edges: CallEdgeView

Every call-site edge in the graph, as a read-only sequence that copies nothing.

empty property

empty: bool

True when the graph has no nodes.

find

find(id: MethodId) -> Optional[CallNode]

The node of the method with this identity, or None when the graph has no such method.

method_for

method_for(node: CallNode) -> Optional[Method]

The Method of a DEFINED node, or None for an EXTERNAL or CALL_SITE node. Same as node.method.

CallNode

A node of the call graph.

Most nodes are methods. A method defined in a loaded file has the kind DEFINED. A method the app calls but does not contain, such as a framework method, is a node too, of kind EXTERNAL. An invoke-custom call site is a node of kind CALL_SITE.

index property

index: int

Position of the node in CallGraph.nodes.

id property

The method's identity. For a CALL_SITE node it is opaque: it only keeps call sites apart and names no method.

kind property

What the node stands for: a defined method, an external method, or a call site.

outgoing property

outgoing: list[int]

Indices in CallGraph.edges of the call sites in this method. out_edges gives the edges.

incoming property

incoming: list[int]

Indices in CallGraph.edges of the calls into this method. in_edges gives the edges.

out_edges property

out_edges: CallEdgeView

The call sites in this method, as edges.

in_edges property

in_edges: CallEdgeView

The calls into this method, as edges.

dispatch_targets property

dispatch_targets: CallNodeView

The other methods a virtual or interface call declared against this method may run: the overrides in loaded subclasses and implementers.

The list is stored once on the node and shared by all its call sites. Every target is a DEFINED node. It is empty when no such call names this method.

dispatch_sources property

dispatch_sources: CallNodeView

The reverse of dispatch_targets: the declared methods through which a virtual or interface call can reach this method.

method property

method: Optional[Method]

The Method of a DEFINED node, or None for an EXTERNAL or CALL_SITE node.

callees

callees() -> CallNodeView

Every method this method may call: the callee of each outgoing edge, plus the dispatch targets of each virtual and interface call. No duplicates, in index order.

Each call collects and sorts the list again, so keep the result when you need it more than once.

callers

callers() -> CallNodeView

Every method that may call this method, directly or through virtual dispatch. No duplicates, in index order.

Each call collects and sorts the list again. For a method that is called from many places, that means going through every one of those call sites, so keep the result when you need it more than once.

CallNodeKind

Bases: Enum

What a call-graph node stands for.

DEFINED class-attribute instance-attribute

DEFINED = 0

A method defined in a loaded file. CallNode.method gives its Method.

EXTERNAL class-attribute instance-attribute

EXTERNAL = 1

A method that loaded code calls but no loaded file defines, such as a framework method.

CALL_SITE class-attribute instance-attribute

CALL_SITE = 2

An invoke-custom call site. Its bootstrap method picks the target at runtime, so the node stands for the call site itself. Its id is opaque: it only keeps call sites apart, names no method, and may change between versions.

CallEdge

One call site: an invoke instruction in a caller, pointing at a callee.

A call site has one DECLARED edge, to the target the bytecode names. It has a second, INHERITED_RESOLUTION edge when the named class does not define the method itself. Both edges have the same caller, kind and offset.

caller_node property

caller_node: int

Index in CallGraph.nodes of the calling method.

callee_node property

callee_node: int

Index in CallGraph.nodes of the called method.

caller property

caller: CallNode

The calling method's node.

callee property

callee: CallNode

The called method's node.

kind property

kind: InvokeKind

The kind of invoke instruction.

code_offset property

code_offset: int

Offset of the invoke instruction in the caller's code, in code units.

origin property

origin: EdgeOrigin

Whether the callee is the declared target or the one found by walking up the hierarchy.

dispatch_targets property

dispatch_targets: CallNodeView

The other methods this call site may run because of virtual dispatch.

For a DECLARED edge of an invoke-virtual or invoke-interface it is the same as callee.dispatch_targets. It is empty for every other edge.

InvokeKind

Bases: Enum

The kind of invoke instruction at a call site.

VIRTUAL class-attribute instance-attribute

VIRTUAL = 0

invoke-virtual: a normal call on an object. The method that runs depends on the class of the object.

SUPER class-attribute instance-attribute

SUPER = 1

invoke-super: a call to the superclass's version of a method.

DIRECT class-attribute instance-attribute

DIRECT = 2

invoke-direct: a call to a constructor or a private method.

STATIC class-attribute instance-attribute

STATIC = 3

invoke-static: a call to a static method.

INTERFACE class-attribute instance-attribute

INTERFACE = 4

invoke-interface: a call through an interface type.

POLYMORPHIC class-attribute instance-attribute

POLYMORPHIC = 5

invoke-polymorphic: a call through a method handle, such as MethodHandle.invoke.

CUSTOM class-attribute instance-attribute

CUSTOM = 6

invoke-custom: a call site that is bound at runtime. Compilers emit it for lambdas and for string concatenation.

EdgeOrigin

Bases: Enum

How a call-graph edge came to point at its callee.

DECLARED class-attribute instance-attribute

DECLARED = 0

The target exactly as the bytecode names it. Every call site has exactly one edge of this kind.

INHERITED_RESOLUTION class-attribute instance-attribute

INHERITED_RESOLUTION = 1

The class the bytecode names does not define the method itself. The edge points at the nearest definition in a loaded superclass or interface, which is the method Android finds at runtime. A call site has at most one edge of this kind.

CallNodeView

Bases: Sequence[CallNode]

A read-only sequence of call-graph nodes that copies nothing: all nodes of a graph, or a subset such as the dispatch targets of a node or the result of CallNode.callers().

It supports len(), indexing, slicing and iteration.

CallEdgeView

Bases: Sequence[CallEdge]

A read-only sequence of call-graph edges that copies nothing.

It supports len(), indexing, slicing and iteration.

where

where(
    *,
    origin: Optional[EdgeOrigin] = None,
    kind: Optional[InvokeKind] = None,
) -> CallEdgeView

The edges of this view that match every given filter.

The filtering is done inside the library. A large app has millions of call sites, so this is much faster than a Python loop. graph.edges.where(origin=EdgeOrigin.DECLARED) gives exactly one edge per call site.

Parameters:

Name Type Description Default
origin Optional[EdgeOrigin]

Keep only edges with this origin.

None
kind Optional[InvokeKind]

Keep only edges with this kind of invoke.

None