• No results found

The RuntimeVisibleAnnotations Attribute

In document The Java Virtual Machine Specification (Page 145-149)

4.4 The Constant Pool

4.7.16 The RuntimeVisibleAnnotations Attribute

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_index item must be a valid index

into the constant_pool table. The constant_pool entry at that index

must be a CONSTANT_Utf8_info structure (§4.4.7) representing the string

"RuntimeVisibleAnnotations". attribute_length

The value of the attribute_length item indicates the length of the attribute,

excluding the initial six bytes.

num_annotations

The value of the num_annotations item gives the number of run-time visible

annotations represented by the structure.

annotations[]

Each entry in the annotations table represents a single run-time visible

annotation on a declaration. The annotation structure has the following

4.7 Attributes THE CLASS FILE 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 annotation structure are as follows: type_index

The value of the type_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be

a CONSTANT_Utf8_info structure (§4.4.7) representing a field descriptor

(§4.3.2). The field descriptor denotes the type of the annotation represented by this annotation structure.

num_element_value_pairs

The value of the num_element_value_pairs item gives the number of

element-value pairs of the annotation represented by this annotation

structure.

element_value_pairs[]

Each value of the element_value_pairs table represents a single element-

value pair in the annotation represented by this annotation structure. Each element_value_pairs entry contains the following two items:

element_name_index

The value of the element_name_index item must be a valid index into

the constant_pool table. The constant_pool entry at that index must

be a CONSTANT_Utf8_info structure (§4.4.7). The constant_pool

entry denotes the name of the element of the element-value pair represented by this element_value_pairs entry.

In other words, the entry denotes an element of the annotation type specified by

type_index.

value

The value of the value item represents the value of the element-value

pair represented by this element_value_pairs entry.

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:

THE CLASS FILE FORMAT Attributes 4.7 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

4.7 Attributes THE CLASS FILE FORMAT

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_index item denotes either a primitive constant value or a

String literal as the value of this element-value pair.

The value of the const_value_index item must be a valid index into the

constant_pool table. The constant_pool entry at that index must be of a type

appropriate to the tag item, as specified in the fourth column of Table 4.7.16.1- A.

enum_const_value

The enum_const_value item denotes an enum constant as the value of this

element-value pair.

The enum_const_value item consists of the following two items: type_name_index

The value of the type_name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a field descriptor

(§4.3.2). The constant_pool entry gives the internal form of the binary name of the type of the enum constant represented by this element_value

structure (§4.2.1).

const_name_index

The value of the const_name_index item must be a valid index into the

constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7). The constant_pool entry gives the simple name of the enum constant represented by this element_value

structure.

class_info_index

The class_info_index item denotes a class literal as the value of this element- value pair.

The class_info_index item must be a valid index into the constant_pool

table. The constant_pool entry at that index must be a CONSTANT_Utf8_info

structure (§4.4.7) representing a return descriptor (§4.3.3). The return descriptor gives the type corresponding to the class literal represented by this

THE CLASS FILE FORMAT Attributes 4.7

• 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 the

constant_pool will 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 the constant_pool will be

a BaseType character.

• For a class literal void.class, the corresponding type is void. The return

descriptor in the constant_pool will be V.

For example, the class literal Object.class corresponds to the type Object, so the

constant_pool entry is Ljava/lang/Object;, whereas the class literal int.class

corresponds to the type int, so the constant_pool entry is I.

The class literal void.class corresponds to void, so the constant_pool entry is V, whereas the class literal Void.class corresponds to the type Void, so the

constant_pool entry is Ljava/lang/Void;.

annotation_value

The annotation_value item denotes a "nested" annotation as the value of this

element-value pair.

The value of the annotation_value item is an annotation structure (§4.7.16)

that gives the annotation represented by this element_value structure. array_value

The array_value item denotes an array as the value of this element-value pair.

The array_value item consists of the following two items: num_values

The value of the num_values item gives the number of elements in the

array represented by this element_value structure. values[]

Each value in the values table gives the corresponding element of the array

represented by this element_value structure.

In document The Java Virtual Machine Specification (Page 145-149)