The RuntimeVisibleTypeAnnotations attribute is an variable-length attribute in the attributes table of a ClassFile, field_info, method_info, or record_component_info structure, or Code attribute (§4.1, §4.5, §4.6, §4.7.30, §4.7.3). The RuntimeVisibleTypeAnnotations attribute stores run-time visible annotations on types used in the declaration of the corresponding class, field, method, or record component, or in an expression in the corresponding method body. The RuntimeVisibleTypeAnnotations attribute also stores run-time visible annotations on type parameter declarations of generic classes, interfaces, methods, and constructors.
There may be at most one RuntimeVisibleTypeAnnotations attribute in the attributes table of a ClassFile, field_info, method_info, or record_component_info structure, or Code attribute.
An attributes table contains a RuntimeVisibleTypeAnnotations attribute only if types are annotated in kinds of declaration or expression that correspond to the parent structure or attribute of the attributes table.
For example, all annotations on types in the implements clause of a class declaration are recorded in the RuntimeVisibleTypeAnnotations attribute of the class's ClassFile structure. Meanwhile, all annotations on the type in a field declaration are recorded in the RuntimeVisibleTypeAnnotations attribute of the field's field_info structure.
The RuntimeVisibleTypeAnnotations attribute has the following format:
RuntimeVisibleTypeAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
type_annotation annotations[num_annotations];
}
The items of the RuntimeVisibleTypeAnnotations_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 representing the string "RuntimeVisibleTypeAnnotations". -
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 type annotations represented by the structure. -
annotations[]
Each entry in the
annotationstable represents a single run-time visible annotation on a type used in a declaration or expression. Thetype_annotationstructure has the following format:type_annotation { u1 target_type; union { type_parameter_target; supertype_target; type_parameter_bound_target; empty_target; formal_parameter_target; throws_target; localvar_target; catch_target; offset_target; type_argument_target; } target_info; type_path target_path; u2 type_index; u2 num_element_value_pairs; { u2 element_name_index; element_value value; } element_value_pairs[num_element_value_pairs]; }The first three items -
target_type,target_info, andtarget_path- specify the precise location of the annotated type. The last three items -type_index,num_element_value_pairs, andelement_value_pairs[]- specify the annotation's own type and element-value pairs.The items of the
type_annotationstructure are as follows:-
target_type
The value of the
target_typeitem denotes the kind of target on which the annotation appears. The various kinds of target correspond to the type contexts of the Java programming language where types are used in declarations and expressions (JLS §4.11).The legal values of
target_typeare specified in Table 4.7.20-A and Table 4.7.20-B. Each value is a one-byte tag indicating which item of thetarget_infounion follows thetarget_typeitem to give more information about the target.The kinds of target in Table 4.7.20-A and Table 4.7.20-B correspond to the type contexts in JLS §4.11. Namely,
target_typevalues 0x10-0x17 and 0x40-0x42 correspond to type contexts 1-10, whiletarget_typevalues 0x43-0x4B correspond to type contexts 11-16.The value of the
target_typeitem determines whether thetype_annotationstructure appears in aRuntimeVisibleTypeAnnotationsattribute in aClassFilestructure, afield_infostructure, amethod_infostructure, or aCodeattribute. Table 4.7.20-C gives the location of theRuntimeVisibleTypeAnnotationsattribute for atype_annotationstructure with each legaltarget_typevalue. -
target_info
The value of the
target_infoitem denotes precisely which type in a declaration or expression is annotated.The items of the
target_infounion are specified in §4.7.20.1. -
target_path
The value of the
target_pathitem denotes precisely which part of the type indicated bytarget_infois annotated.The format of the
type_pathstructure is specified in §4.7.20.2. -
type_index, num_element_value_pairs, element_value_pairs[]
The meaning of these items in the
type_annotationstructure is the same as their meaning in theannotationstructure (§4.7.16).
-
Table 4.7.20-A. Interpretation of target_type values (Part 1)
| Value | Kind of target | target_info item |
|---|---|---|
| 0x00 | type parameter declaration of generic class or interface | type_parameter_target |
| 0x01 | type parameter declaration of generic method or constructor | type_parameter_target |
| 0x10 | type in extends or implements clause of class declaration (including the direct superclass or direct superinterface of an anonymous class declaration), or in extends clause of interface declaration |
supertype_target |
| 0x11 | type in bound of type parameter declaration of generic class or interface | type_parameter_bound_target |
| 0x12 | type in bound of type parameter declaration of generic method or constructor | type_parameter_bound_target |
| 0x13 | type in field or record component declaration | empty_target |
| 0x14 | return type of method, or type of newly constructed object | empty_target |
| 0x15 | receiver type of method or constructor | empty_target |
| 0x16 | type in formal parameter declaration of method, constructor, or lambda expression | formal_parameter_target |
| 0x17 | type in throws clause of method or constructor |
throws_target |
Table 4.7.20-B. Interpretation of target_type values (Part 2)
| Value | Kind of target | target_info item |
|---|---|---|
| 0x40 | type in local variable declaration | localvar_target |
| 0x41 | type in resource variable declaration | localvar_target |
| 0x42 | type in exception parameter declaration | catch_target |
| 0x43 | type in instanceof expression | offset_target |
| 0x44 | type in new expression | offset_target |
| 0x45 | type in method reference expression using ::new |
offset_target |
| 0x46 | type in method reference expression using ::Identifier |
offset_target |
| 0x47 | type in cast expression | type_argument_target |
| 0x48 | type argument for generic constructor in new expression or explicit constructor invocation statement | type_argument_target |
| 0x49 | type argument for generic method in method invocation expression | type_argument_target |
| 0x4A | type argument for generic constructor in method reference expression using ::new |
type_argument_target |
| 0x4B | type argument for generic method in method reference expression using ::Identifier |
type_argument_target |
Table 4.7.20-C. Location of enclosing attribute for target_type values
| Value | Kind of target | Location |
|---|---|---|
| 0x00 | type parameter declaration of generic class or interface | ClassFile |
| 0x01 | type parameter declaration of generic method or constructor | method_info |
| 0x10 | type in extends clause of class or interface declaration, or in implements clause of interface declaration |
ClassFile |
| 0x11 | type in bound of type parameter declaration of generic class or interface | ClassFile |
| 0x12 | type in bound of type parameter declaration of generic method or constructor | method_info |
| 0x13 | type in field or record component declaration | field_info, record_component_info |
| 0x14 | return type of method or constructor | method_info |
| 0x15 | receiver type of method or constructor | method_info |
| 0x16 | type in formal parameter declaration of method, constructor, or lambda expression | method_info |
| 0x17 | type in throws clause of method or constructor |
method_info |
| 0x40-0x4B | types in local variable declarations, resource variable declarations, exception parameter declarations, expressions | Code |
Sections: