RuntimeVisibleTypeAnnotations attribute
Annotations on uses of types (Java 8): @NonNull String, List<@NonNull String>, a cast, a throws clause.
- Appears on
- class, field, method, Code, record component
- Since
- class file 52.0 (Java 8)
- javac writes it
- for runtime-visible TYPE_USE annotations
- Needed by the JVM
- no: it is for the class libraries (reflection), compilers or tools
Layout
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "RuntimeVisibleTypeAnnotations" |
u4 | attribute_length | bytes after this field |
u2 | num_annotations | |
type_annotation[…] | annotations | target_type, target_info, type_path, then the annotation |
What it is for
Annotations on uses of types (Java 8): @NonNull String, List<@NonNull String>, a cast, a throws clause. Each records exactly which type use it annotates, down to a type argument or array level.
How common it is
JDK 25.0.5's java.base has 6 RuntimeVisibleTypeAnnotations attributes, in 6 of its 7,391 classes (6 on classs).
A real one, byte by byte
From Annotations.class (compiled by javac 25), on method label:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1489 | 14 bytes | RuntimeVisibleTypeAnnotations | |
| 1489 | 00 34 | attribute_name_index | #52 |
| 1491 | 00 00 00 08 | attribute_length | 8 |
| 1495 | 00 01 | num_annotations | 1 |
| 1497 | 6 bytes | type_annotation | |
| 1497 | 14 | target_type | 0x14 |
| 1498 | target_info | ||
| 1498 | 1 bytes | type_path | |
| 1498 | 00 | path_length | 0 |
| 1499 | 00 35 | type_index | #53 |
| 1501 | 00 00 | num_element_value_pairs | 0 |
Specification: JVMS §4.7, The RuntimeVisibleTypeAnnotations Attribute (Java SE 25).