The RuntimeVisibleAnnotations attribute is a variable-length attribute in the attributes table of a ClassFile, field_info, or method_info structure (§4.1, §4.5, §4.6). The RuntimeVisibleAnnotations attribute records run-time visible annotations on the declaration of the corresponding class, field, or method. The Java Virtual Machine must make these annotations available so they can be returned by the appropriate reflective APIs.
There may be at most one RuntimeVisibleAnnotations attribute in the attributes table of a ClassFile, field_info, or method_info structure.
The RuntimeVisibleAnnotations attribute has the following format:
RuntimeVisibleAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
annotation annotations[num_annotations];
}
The items of the RuntimeVisibleAnnotations_attribute structure are as follows:
-
attribute_name_index
The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing the string "RuntimeVisibleAnnotations". -
attribute_length
The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. -
num_annotations
The value of the
num_annotationsitem gives the number of run-time visible annotations represented by the structure. -
annotations[]
Each entry in the
annotationstable represents a single run-time visible annotation on a declaration. Theannotationstructure has the following format:annotation { u2 type_index; u2 num_element_value_pairs; { u2 element_name_index; element_value value; } element_value_pairs[num_element_value_pairs]; }The items of the
annotationstructure are as follows:-
type_index
The value of the
type_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing a field descriptor (§4.3.2). The field descriptor denotes the type of the annotation represented by thisannotationstructure. -
num_element_value_pairs
The value of the
num_element_value_pairsitem gives the number of element-value pairs of the annotation represented by thisannotationstructure. -
element_value_pairs[]
Each value of the
element_value_pairstable represents a single element-value pair in the annotation represented by thisannotationstructure. Eachelement_value_pairsentry contains the following two items:-
element_name_index
The value of the
element_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7). Theconstant_poolentry denotes the name of the element of the element-value pair represented by thiselement_value_pairsentry.In other words, the entry denotes an element of the annotation type specified by
type_index. -
value
The value of the
valueitem represents the value of the element-value pair represented by thiselement_value_pairsentry.
-
-
4.7.16.1. The element_value structure
The element_value structure is a discriminated union representing the value of an element-value pair. It has the following format:
element_value {
u1 tag;
union {
u2 const_value_index;
{ u2 type_name_index;
u2 const_name_index;
} enum_const_value;
u2 class_info_index;
annotation annotation_value;
{ u2 num_values;
element_value values[num_values];
} array_value;
} value;
}
The tag item uses a single ASCII character to indicate the type of the value of the element-value pair. This determines which item of the value union is in use. Table 4.7.16.1-A shows the valid characters for the tag item, the type indicated by each character, and the item used in the value union for each character. The table's fourth column is used in the description below of one item of the value union.
Table 4.7.16.1-A. Interpretation of tag values as types
tag Item |
Type | value Item |
Constant Type |
|---|---|---|---|
B |
byte |
const_value_index |
CONSTANT_Integer |
C |
char |
const_value_index |
CONSTANT_Integer |
D |
double |
const_value_index |
CONSTANT_Double |
F |
float |
const_value_index |
CONSTANT_Float |
I |
int |
const_value_index |
CONSTANT_Integer |
J |
long |
const_value_index |
CONSTANT_Long |
S |
short |
const_value_index |
CONSTANT_Integer |
Z |
boolean |
const_value_index |
CONSTANT_Integer |
s |
String |
const_value_index |
CONSTANT_Utf8 |
e |
Enum type | enum_const_value |
Not applicable |
c |
Class |
class_info_index |
Not applicable |
@ |
Annotation type | annotation_value |
Not applicable |
[ |
Array type | array_value |
Not applicable |
The value item represents the value of an element-value pair. The item is a union, whose own items are as follows:
-
const_value_index
The
const_value_indexitem denotes either a primitive constant value or aStringliteral as the value of this element-value pair.The value of the
const_value_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be of a type appropriate to thetagitem, as specified in the fourth column of Table 4.7.16.1-A. -
enum_const_value
The
enum_const_valueitem denotes an enum constant as the value of this element-value pair.The
enum_const_valueitem consists of the following two items:-
type_name_index
The value of the
type_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing a field descriptor (§4.3.2). Theconstant_poolentry gives the internal form of the binary name of the type of the enum constant represented by thiselement_valuestructure (§4.2.1). -
const_name_index
The value of the
const_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7). Theconstant_poolentry gives the simple name of the enum constant represented by thiselement_valuestructure.
-
-
class_info_index
The
class_info_indexitem denotes a class literal as the value of this element-value pair.The
class_info_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing a return descriptor (§4.3.3). The return descriptor gives the type corresponding to the class literal represented by thiselement_valuestructure. Types correspond to class literals as follows:- For a class literal C
.class, where C is the name of a class, interface, or array type, the corresponding type is C. The return descriptor in theconstant_poolwill be an ObjectType or an ArrayType. - For a class literal p
.class, where p is the name of a primitive type, the corresponding type is p. The return descriptor in theconstant_poolwill be a BaseType character. - For a class literal
void.class, the corresponding type isvoid. The return descriptor in theconstant_poolwill be V.
For example, the class literal
Object.classcorresponds to the typeObject, so theconstant_poolentry isLjava/lang/Object;, whereas the class literalint.classcorresponds to the typeint, so theconstant_poolentry isI.The class literal
void.classcorresponds tovoid, so theconstant_poolentry is V, whereas the class literalVoid.classcorresponds to the typeVoid, so theconstant_poolentry isLjava/lang/Void;. - For a class literal C
-
annotation_value
The
annotation_valueitem denotes a "nested" annotation as the value of this element-value pair.The value of the
annotation_valueitem is anannotationstructure (§4.7.16) that gives the annotation represented by thiselement_valuestructure. -
array_value
The
array_valueitem denotes an array as the value of this element-value pair.The
array_valueitem consists of the following two items:-
num_values
The value of the
num_valuesitem gives the number of elements in the array represented by thiselement_valuestructure. -
values[]
Each value in the
valuestable gives the corresponding element of the array represented by thiselement_valuestructure.
-