Skip to content

Annotations and values

Annotations on classes, methods and fields, and the values of their elements. See annotations in the code guide.

Annotation

One annotation on a class, a method or a field.

An annotation is a copy of the data. It stays valid on its own.

type_descriptor property

type_descriptor: str

Descriptor of the annotation's class, such as Ljava/lang/Deprecated;.

visibility property

Who can see the annotation.

elements property

elements: dict[str, AnnotationValue]

The elements the source set, by name.

An element left at its default value is not in the dictionary. The DEX format stores defaults once, on the annotation's own class.

AnnotationVisibility

Bases: Enum

Who can see an annotation.

BUILD class-attribute instance-attribute

BUILD = 0

Only tools that read the file. The annotation is not visible to the running app. @Override is an example in source code.

RUNTIME class-attribute instance-attribute

RUNTIME = 1

The running app can read the annotation through reflection.

SYSTEM class-attribute instance-attribute

SYSTEM = 2

Data the compiler writes for the runtime in dalvik.annotation.* annotations: generic signatures, default values, inner-class information.

AnnotationValue module-attribute

AnnotationValue = Union[
    None,
    bool,
    int,
    float,
    str,
    TypeValue,
    EnumValue,
    FieldValue,
    MethodValue,
    MethodTypeValue,
    MethodHandleValue,
    list["AnnotationValue"],
    "Annotation",
]

The value of an annotation element, and the type of Field.initial_value.

Numbers, booleans, strings and null become plain Python values. All integer types (byte, short, char, int, long) become int, and float and double become float, so the original width is not kept. An array becomes a list. A value that names a type, a member, a prototype or a method handle is one of the small value classes (TypeValue, EnumValue, FieldValue, MethodValue, MethodTypeValue, MethodHandleValue), and a nested annotation is an Annotation.

TypeValue

A class literal as an annotation value, such as String.class.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

descriptor property

descriptor: str

Descriptor of the type.

EnumValue

An enum constant as an annotation value.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

constant property

constant: FieldId

The static field that holds the enum constant.

FieldValue

A field as an annotation value. It is rare outside system annotations.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

field property

field: FieldId

The field.

MethodValue

A method as an annotation value. It is rare outside system annotations.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

method property

method: MethodId

The method.

MethodTypeValue

A method prototype as an annotation value: a java.lang.invoke.MethodType constant.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

proto property

proto: str

The prototype, such as (I)V.

MethodHandleValue

A method handle as an annotation value: a java.lang.invoke.MethodHandle constant, which reads or writes a field, or calls a method.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

kind property

What the handle does with its member.

member property

member: Union[MethodId, FieldId]

The member the handle works on: a FieldId for the four field kinds (STATIC_PUT, STATIC_GET, INSTANCE_PUT, INSTANCE_GET), a MethodId for the five invoke kinds.

In a damaged file, where the handle cannot be read, the kind is INVOKE_STATIC and the member is a MethodId of empty strings.

MethodHandleKind

Bases: Enum

What a method handle does, as MethodHandleValue.kind gives it. The values are the method handle type codes of the DEX format.

STATIC_PUT class-attribute instance-attribute

STATIC_PUT = 0

Writes a static field.

STATIC_GET class-attribute instance-attribute

STATIC_GET = 1

Reads a static field.

INSTANCE_PUT class-attribute instance-attribute

INSTANCE_PUT = 2

Writes an instance field.

INSTANCE_GET class-attribute instance-attribute

INSTANCE_GET = 3

Reads an instance field.

INVOKE_STATIC class-attribute instance-attribute

INVOKE_STATIC = 4

Calls a static method.

INVOKE_INSTANCE class-attribute instance-attribute

INVOKE_INSTANCE = 5

Calls an instance method, with virtual dispatch.

INVOKE_CONSTRUCTOR class-attribute instance-attribute

INVOKE_CONSTRUCTOR = 6

Creates an object with a constructor.

INVOKE_DIRECT class-attribute instance-attribute

INVOKE_DIRECT = 7

Calls an instance method without dispatch, such as a private method.

INVOKE_INTERFACE class-attribute instance-attribute

INVOKE_INTERFACE = 8

Calls an interface method.