RuntimeInvisibleAnnotations attribute
Annotations kept in the class file but hidden from reflection: the default retention.
- Appears on
- class, field, method, record component
- Since
- class file 49.0 (Java 5)
- javac writes it
- for annotations with @Retention(CLASS), the default
- 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: "RuntimeInvisibleAnnotations" |
u4 | attribute_length | bytes after this field |
u2 | num_annotations | |
annotation[num_annotations] | annotations |
What it is for
Annotations kept in the class file but hidden from reflection: the default retention. Bytecode tools and annotation processors reading compiled classes can still see them.
How common it is
JDK 25.0.5's java.base has 26 RuntimeInvisibleAnnotations attributes, in 19 of its 7,391 classes (9 on methods, 13 on classs, 4 on fields).
A real one, byte by byte
From Annotations.class (compiled by javac 25), on method old:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1382 | 12 bytes | RuntimeInvisibleAnnotations | |
| 1382 | 00 2f | attribute_name_index | #47 |
| 1384 | 00 00 00 06 | attribute_length | 6 |
| 1388 | 00 01 | num_annotations | 1 |
| 1390 | 4 bytes | annotation 0 | @Annotations$BuildOnly |
| 1390 | 00 30 | type_index | #48 |
| 1392 | 00 00 | num_element_value_pairs | 0 |
Specification: JVMS §4.7, The RuntimeInvisibleAnnotations Attribute (Java SE 25).