JVM Knowledge

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_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 representing the string "RuntimeVisibleTypeAnnotations".

  • 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 type annotations represented by the structure.

  • annotations[]

    Each entry in the annotations table represents a single run-time visible annotation on a type used in a declaration or expression. The type_annotation structure 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, and target_path - specify the precise location of the annotated type. The last three items - type_index, num_element_value_pairs, and element_value_pairs[] - specify the annotation's own type and element-value pairs.

    The items of the type_annotation structure are as follows:

    • target_type

      The value of the target_type item 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_type are specified in Table 4.7.20-A and Table 4.7.20-B. Each value is a one-byte tag indicating which item of the target_info union follows the target_type item 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_type values 0x10-0x17 and 0x40-0x42 correspond to type contexts 1-10, while target_type values 0x43-0x4B correspond to type contexts 11-16.

      The value of the target_type item determines whether the type_annotation structure appears in a RuntimeVisibleTypeAnnotations attribute in a ClassFile structure, a field_info structure, a method_info structure, or a Code attribute. Table 4.7.20-C gives the location of the RuntimeVisibleTypeAnnotations attribute for a type_annotation structure with each legal target_type value.

    • target_info

      The value of the target_info item denotes precisely which type in a declaration or expression is annotated.

      The items of the target_info union are specified in §4.7.20.1.

    • target_path

      The value of the target_path item denotes precisely which part of the type indicated by target_info is annotated.

      The format of the type_path structure is specified in §4.7.20.2.

    • type_index, num_element_value_pairs, element_value_pairs[]

      The meaning of these items in the type_annotation structure is the same as their meaning in the annotation structure (§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: