Skip to content

Class hierarchy and cross-references

Inheritance queries and the index of who uses what. See the hierarchy and cross-references guide.

ClassHierarchy

Inheritance over the whole program.

Get it from AnalysisContext.class_hierarchy. It is built on the first read and kept. Every query takes and returns class descriptors.

A class outside the loaded files is in the hierarchy when a loaded class names it as its superclass or as an interface. Its own ancestors are not known. A query about a descriptor the hierarchy has never seen gives an empty list or False.

empty property

empty: bool

True when the hierarchy has no classes.

supertypes

supertypes(descriptor: str) -> list[str]

The direct superclass, then the directly implemented interfaces.

subclasses

subclasses(descriptor: str) -> list[str]

The classes that directly extend this class.

implementers

implementers(descriptor: str) -> list[str]

The classes that directly implement this interface.

all_supertypes

all_supertypes(descriptor: str) -> list[str]

Every ancestor: superclasses and interfaces, at any distance. The class itself is not included.

all_descendants

all_descendants(descriptor: str) -> list[str]

Every class below this one, at any distance, through both extends and implements. The class itself is not included.

is_subtype_of

is_subtype_of(sub: str, super: str) -> bool

True when sub is super itself or has super among its ancestors.

is_loaded

is_loaded(descriptor: str) -> bool

True when the class is defined in a loaded file.

is_interface

is_interface(descriptor: str) -> bool

True when the class is defined in a loaded file and is an interface. False for a class outside the loaded files, because its kind is not known.

Xrefs

The cross-reference index of the whole program: who uses a method, a field, a string or a type.

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

Every query returns a list of sites. A site has a code_offset and a referrer: the number of the method whose code contains the instruction. Only instructions are indexed.

referrers property

referrers: ReferrerView

The identity of every method that contains at least one indexed instruction. referrers[site.referrer] is the method a site is in.

empty property

empty: bool

True when nothing is indexed.

referrer_id

referrer_id(referrer: int) -> MethodId

The identity of one referrer. Same as referrers[referrer].

Raises:

Type Description
IndexError

The number is out of range.

referrer_method

referrer_method(referrer: int) -> Optional[Method]

The Method of one referrer.

method_refs

method_refs(id: MethodId) -> list[MethodXref]

The call sites whose target, as the bytecode names it, is exactly this method.

Virtual dispatch is not applied: a call to Animal.sound is not listed under Dog.sound. Use the call graph for that.

method_refs_by_name

method_refs_by_name(
    name: str,
) -> list[tuple[MethodId, list[MethodXref]]]

The call sites whose target has this method name, in any class and with any prototype.

Each item of the result pairs one called method with its call sites. It is the quick way to find every call to a name such as exec or loadUrl.

field_refs

field_refs(id: FieldId) -> list[FieldXref]

Every read and write of this field.

field_reads

field_reads(id: FieldId) -> list[FieldXref]

Only the reads of this field.

field_writes

field_writes(id: FieldId) -> list[FieldXref]

Only the writes of this field.

string_refs

string_refs(value: str) -> list[StringXref]

The const-string instructions that load exactly this string. To search by substring or pattern, filter referenced_strings() first.

referenced_strings

referenced_strings() -> list[str]

Every distinct string that some const-string instruction loads.

Each call makes a new list of all of them, so keep the result when you need it more than once.

type_refs

type_refs(descriptor: str) -> list[TypeXref]

The instructions that name this type: const-class, check-cast, instance-of, new-instance, new-array and filled-new-array.

MethodXref

One invoke instruction that calls a method.

referrer property

referrer: int

Index in Xrefs.referrers of the method that contains the instruction.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

kind property

kind: InvokeKind

The kind of invoke instruction.

FieldXref

One instruction that reads or writes a field.

referrer property

referrer: int

Index in Xrefs.referrers of the method that contains the instruction.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

is_write property

is_write: bool

True for a write, false for a read.

is_static property

is_static: bool

True when the instruction is a static field access.

StringXref

One const-string instruction that loads a string.

referrer property

referrer: int

Index in Xrefs.referrers of the method that contains the instruction.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

TypeXref

One instruction that names a type.

referrer property

referrer: int

Index in Xrefs.referrers of the method that contains the instruction.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

kind property

What the instruction does with the type.

TypeUseKind

Bases: Enum

What an instruction does with the type it names.

CONST_CLASS class-attribute instance-attribute

CONST_CLASS = 0

const-class: loads the class object, as Foo.class does.

CHECK_CAST class-attribute instance-attribute

CHECK_CAST = 1

check-cast: a cast to the type.

INSTANCE_OF class-attribute instance-attribute

INSTANCE_OF = 2

instance-of: an instanceof test against the type.

NEW_INSTANCE class-attribute instance-attribute

NEW_INSTANCE = 3

new-instance: creates an object of the type.

NEW_ARRAY class-attribute instance-attribute

NEW_ARRAY = 4

new-array: creates an array. The descriptor is the array type, such as [I.

FILLED_NEW_ARRAY class-attribute instance-attribute

FILLED_NEW_ARRAY = 5

filled-new-array: creates an array and fills it from registers. The descriptor is the array type.

ReferrerView

Bases: Sequence[MethodId]

A read-only sequence of method identities that copies nothing. The referrer number of a cross-reference site is an index into it.

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