RuntimeVisibleAnnotations attribute
Annotations that reflection can see (getAnnotations()).
- Appears on
- class, field, method, record component
- Since
- class file 49.0 (Java 5)
- javac writes it
- for annotations with @Retention(RUNTIME)
- 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: "RuntimeVisibleAnnotations" |
u4 | attribute_length | bytes after this field |
u2 | num_annotations | |
annotation[num_annotations] | annotations | type_index, then element name and value pairs |
What it is for
Annotations that reflection can see (getAnnotations()). Each one is its type as a field descriptor plus element-value pairs; values can be constants, enums, classes, nested annotations or arrays.
How common it is
JDK 25.0.5's java.base has 4,093 RuntimeVisibleAnnotations attributes, in 664 of its 7,391 classes (3,567 on methods, 220 on classs, 306 on fields).
A real one, byte by byte
From Annotations.class (compiled by javac 25), on method old:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1370 | 12 bytes | RuntimeVisibleAnnotations | |
| 1370 | 00 17 | attribute_name_index | #23 |
| 1372 | 00 00 00 06 | attribute_length | 6 |
| 1376 | 00 01 | num_annotations | 1 |
| 1378 | 4 bytes | annotation 0 | @java.lang.Deprecated |
| 1378 | 00 2e | type_index | #46 |
| 1380 | 00 00 | num_element_value_pairs | 0 |
Specification: JVMS §4.7, The RuntimeVisibleAnnotations Attribute (Java SE 25).