This document describes changes to the Java Virtual Machine Specification to support the Java language records feature. See JEP draft JDK-8222777.
Changes are described with respect to existing sections of the JVM Specification. New text is indicated like this and deleted text is indicated like this. Explanation and discussion, as needed, is set aside in grey boxes.
Chapter 4: The class File Format
4.7 Attributes
Attributes are used in the ClassFile, field_info, method_info, and Code_attribute, and component_info structures of the class file format (4.1, 4.5, 4.6, 4.7.3, 4.7.30).
All attributes have the following general format:
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
For all attributes, the attribute_name_index item must be a valid unsigned 16-bit index into the constant pool of the class. The constant_pool entry at attribute_name_index must be a CONSTANT_Utf8_info structure (4.4.7) representing the name of the attribute. The value of the attribute_length item indicates the length of the subsequent information in bytes. The length does not include the initial six bytes that contain the attribute_name_index and attribute_length items.
28 29 attributes are predefined by this specification. They are listed three times, for ease of navigation:
Table 4.7-A is ordered by the attributes' section numbers in this chapter. Each attribute is shown with the first version of the
classfile format in which it was defined. Also shown is the version of the Java SE Platform which introduced that version of theclassfile format (4.1).Table 4.7-B is ordered by the first version of the
classfile format in which each attribute was defined.Table 4.7-C is ordered by the location in a
classfile where each attribute is defined to appear.
Within the context of their use in this specification, that is, in the attributes tables of the class file structures in which they appear, the names of these predefined attributes are reserved.
Any conditions on the presence of a predefined attribute in an attributes table are specified explicitly in the section which describes the attribute. If no conditions are specified, then the attribute may appear any number of times in an attributes table.
The predefined attributes are categorized into three groups according to their purpose:
Six attributes are critical to correct interpretation of the
classfile by the Java Virtual Machine:ConstantValueCodeStackMapTableBootstrapMethodsNestHostNestMembers
In a
classfile whose version number is v, each of these attributes must be recognized and correctly read by an implementation of the Java Virtual Machine if the implementation supports version v of theclassfile format, and the attribute was first defined in version v or earlier of theclassfile format, and the attribute appears in a location where it is defined to appear.NineTen attributes are not critical to correct interpretation of theclassfile by the Java Virtual Machine, but are either critical to correct interpretation of theclassfile by the class libraries of the Java SE Platform, or are useful for tools (in which case the section that specifies an attribute describes it as "optional"):ExceptionsInnerClassesEnclosingMethodSyntheticSignatureSourceFileLineNumberTableLocalVariableTableLocalVariableTypeTableRecord
In a
classfile whose version number is v, each of these attributes must be recognized and correctly read by an implementation of the Java Virtual Machine if the implementation supports version v of theclassfile format, and the attribute was first defined in version v or earlier of theclassfile format, and the attribute appears in a location where it is defined to appear.Thirteen attributes are not critical to correct interpretation of the
classfile by the Java Virtual Machine, but contain metadata about theclassfile that is either exposed by the class libraries of the Java SE Platform, or made available by tools (in which case the section that specifies an attribute describes it as "optional"):SourceDebugExtensionDeprecatedRuntimeVisibleAnnotationsRuntimeInvisibleAnnotationsRuntimeVisibleParameterAnnotationsRuntimeInvisibleParameterAnnotationsRuntimeVisibleTypeAnnotationsRuntimeInvisibleTypeAnnotationsAnnotationDefaultMethodParametersModuleModulePackagesModuleMainClass
An implementation of the Java Virtual Machine may use the information that these attributes contain, or otherwise must silently ignore these attributes.
Table 4.7-A. Predefined class file attributes (by section)
| Attribute | Section | class file |
Java SE |
|---|---|---|---|
ConstantValue |
4.7.2 | 45.3 | 1.0.2 |
Code |
4.7.3 | 45.3 | 1.0.2 |
StackMapTable |
4.7.4 | 50.0 | 6 |
Exceptions |
4.7.5 | 45.3 | 1.0.2 |
InnerClasses |
4.7.6 | 45.3 | 1.1 |
EnclosingMethod |
4.7.7 | 49.0 | 5.0 |
Synthetic |
4.7.8 | 45.3 | 1.1 |
Signature |
4.7.9 | 49.0 | 5.0 |
SourceFile |
4.7.10 | 45.3 | 1.0.2 |
SourceDebugExtension |
4.7.11 | 49.0 | 5.0 |
LineNumberTable |
4.7.12 | 45.3 | 1.0.2 |
LocalVariableTable |
4.7.13 | 45.3 | 1.0.2 |
LocalVariableTypeTable |
4.7.14 | 49.0 | 5.0 |
Deprecated |
4.7.15 | 45.3 | 1.1 |
RuntimeVisibleAnnotations |
4.7.16 | 49.0 | 5.0 |
RuntimeInvisibleAnnotations |
4.7.17 | 49.0 | 5.0 |
RuntimeVisibleParameterAnnotations |
4.7.18 | 49.0 | 5.0 |
RuntimeInvisibleParameterAnnotations |
4.7.19 | 49.0 | 5.0 |
RuntimeVisibleTypeAnnotations |
4.7.20 | 52.0 | 8 |
RuntimeInvisibleTypeAnnotations |
4.7.21 | 52.0 | 8 |
AnnotationDefault |
4.7.22 | 49.0 | 5.0 |
BootstrapMethods |
4.7.23 | 51.0 | 7 |
MethodParameters |
4.7.24 | 52.0 | 8 |
Module |
4.7.25 | 53.0 | 9 |
ModulePackages |
4.7.26 | 53.0 | 9 |
ModuleMainClass |
4.7.27 | 53.0 | 9 |
NestHost |
4.7.28 | 55.0 | 11 |
NestMembers |
4.7.29 | 55.0 | 11 |
Record |
4.7.30 | 58.65535 | 14 |
Table 4.7-B. Predefined class file attributes (by class file format)
| Attribute | class file |
Java SE | Section |
|---|---|---|---|
ConstantValue |
45.3 | 1.0.2 | 4.7.2 |
Code |
45.3 | 1.0.2 | 4.7.3 |
Exceptions |
45.3 | 1.0.2 | 4.7.5 |
SourceFile |
45.3 | 1.0.2 | 4.7.10 |
LineNumberTable |
45.3 | 1.0.2 | 4.7.12 |
LocalVariableTable |
45.3 | 1.0.2 | 4.7.13 |
InnerClasses |
45.3 | 1.1 | 4.7.6 |
Synthetic |
45.3 | 1.1 | 4.7.8 |
Deprecated |
45.3 | 1.1 | 4.7.15 |
EnclosingMethod |
49.0 | 5.0 | 4.7.7 |
Signature |
49.0 | 5.0 | 4.7.9 |
SourceDebugExtension |
49.0 | 5.0 | 4.7.11 |
LocalVariableTypeTable |
49.0 | 5.0 | 4.7.14 |
RuntimeVisibleAnnotations |
49.0 | 5.0 | 4.7.16 |
RuntimeInvisibleAnnotations |
49.0 | 5.0 | 4.7.17 |
RuntimeVisibleParameterAnnotations |
49.0 | 5.0 | 4.7.18 |
RuntimeInvisibleParameterAnnotations |
49.0 | 5.0 | 4.7.19 |
AnnotationDefault |
49.0 | 5.0 | 4.7.22 |
StackMapTable |
50.0 | 6 | 4.7.4 |
BootstrapMethods |
51.0 | 7 | 4.7.23 |
RuntimeVisibleTypeAnnotations |
52.0 | 8 | 4.7.20 |
RuntimeInvisibleTypeAnnotations |
52.0 | 8 | 4.7.21 |
MethodParameters |
52.0 | 8 | 4.7.24 |
Module |
53.0 | 9 | 4.7.25 |
ModulePackages |
53.0 | 9 | 4.7.26 |
ModuleMainClass |
53.0 | 9 | 4.7.27 |
NestHost |
55.0 | 11 | 4.7.28 |
NestMembers |
55.0 | 11 | 4.7.29 |
Record |
58.65535 | 14 | 4.7.30 |
Table 4.7-C. Predefined class file attributes (by location)
| Attribute | Location | class file |
|---|---|---|
SourceFile |
ClassFile |
45.3 |
InnerClasses |
ClassFile |
45.3 |
EnclosingMethod |
ClassFile |
49.0 |
SourceDebugExtension |
ClassFile |
49.0 |
BootstrapMethods |
ClassFile |
51.0 |
Module, ModulePackages, ModuleMainClass |
ClassFile |
53.0 |
NestHost, NestMembers |
ClassFile |
55.0 |
Record |
ClassFile |
58.65535 |
ConstantValue |
field_info |
45.3 |
Code |
method_info |
45.3 |
Exceptions |
method_info |
45.3 |
RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations |
method_info |
49.0 |
AnnotationDefault |
method_info |
49.0 |
MethodParameters |
method_info |
52.0 |
Synthetic |
ClassFile, field_info, method_info |
45.3 |
Deprecated |
ClassFile, field_info, method_info |
45.3 |
Signature |
ClassFile, field_info, method_info, component_info |
49.0 |
RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations |
ClassFile, field_info, method_info, component_info |
49.0 |
LineNumberTable |
Code |
45.3 |
LocalVariableTable |
Code |
45.3 |
LocalVariableTypeTable |
Code |
49.0 |
StackMapTable |
Code |
50.0 |
RuntimeVisibleTypeAnnotations, RuntimeInvisibleTypeAnnotations |
ClassFile, field_info, method_info, component_info, Code |
52.0 |
The Synthetic and Deprecated attributes could, in principle, be allowed in component_info structures. But we haven't yet identified compelling use cases.
4.7.8 The Synthetic Attribute
The Synthetic attribute is a fixed-length attribute in the attributes table of a ClassFile, field_info, or method_info structure (4.1, 4.5, 4.6). A class member that does not appear in the source code must be marked using a Synthetic attribute, or else it must have its ACC_SYNTHETIC flag set. The only exceptions to this requirement are compiler-generated methods which are not considered implementation artifacts, namely the instance initialization method representing a default constructor of the Java programming language (2.9.1), the class or interface initialization method (2.9.2), and the mandated members of enums and records.Enum.values() and Enum.valueOf() methods
The
Syntheticattribute was introduced in JDK 1.1 to support nested classes and interfaces.
The Synthetic attribute has the following format:
Synthetic_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
The items of the Synthetic_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 "Synthetic".- attribute_length
The value of the
attribute_lengthitem must be zero.
4.7.9 The Signature Attribute
The Signature attribute is a fixed-length attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure (4.1, 4.5, 4.6, 4.7.30). A Signature attribute records a signature (4.7.9.1) for a class, interface, constructor, method, or field, or record component whose declaration in the Java programming language uses type variables or parameterized types. See The Java Language Specification, Java SE 12 Edition for details about these constructs.
There may be at most one Signature attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure.
The Signature attribute has the following format:
Signature_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 signature_index;
}
The items of the Signature_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 "Signature".- attribute_length
The value of the
attribute_lengthitem must be two.- signature_index
The value of the
signature_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (4.4.7) representing a class signature if thisSignatureattribute is an attribute of aClassFilestructure; a method signature if thisSignatureattribute is an attribute of amethod_infostructure; or a field signature otherwise.
Oracle's Java Virtual Machine implementation does not check the well-formedness of
Signatureattributes during class loading or linking. Instead,Signatureattributes are checked by methods of the Java SE Platform class libraries which expose generic signatures of classes, interfaces, constructors, methods, and fields. Examples includegetGenericSuperclassinClassandtoGenericStringinjava.lang.reflect.Executable.
4.7.9.1 Signatures
Signatures encode declarations written in the Java programming language that use types outside the type system of the Java Virtual Machine. They support reflection and debugging, as well as compilation when only class files are available.
A Java compiler must emit a signature for any class, interface, constructor, method, or field, or record component whose declaration uses type variables or parameterized types. Specifically, a Java compiler must emit:
A class signature for any class or interface declaration which is either generic, or has a parameterized type as a superclass or superinterface, or both.
A method signature for any method or constructor declaration which is either generic, or has a type variable or parameterized type as the return type or a formal parameter type, or has a type variable in a
throwsclause, or any combination thereof.If the
throwsclause of a method or constructor declaration does not involve type variables, then a compiler may treat the declaration as having nothrowsclause for the purpose of emitting a method signature.A field signature for any field, formal parameter,
orlocal variable, or record component declaration whose type uses a type variable or a parameterized type.
Signatures are specified using a grammar which follows the notation of 4.3.1. In addition to that notation:
The syntax [x] on the right-hand side of a production denotes zero or one occurrences of x. That is, x is an optional symbol. The alternative which contains the optional symbol actually defines two alternatives: one that omits the optional symbol and one that includes it.
A very long right-hand side may be continued on a second line by clearly indenting the second line.
The grammar includes the terminal symbol Identifier to denote the name of a type, field, method, formal parameter, local variable, or type variable, as generated by a Java compiler. Such a name must not contain any of the ASCII characters . ; [ / < > : (that is, the characters forbidden in method names (4.2.2) and also colon) but may contain characters that must not appear in an identifier in the Java programming language (JLS §3.8).
Signatures rely on a hierarchy of nonterminals known as type signatures:
A Java type signature represents either a reference type or a primitive type of the Java programming language.
- JavaTypeSignature:
- ReferenceTypeSignature
- BaseType
The following production from 4.3.2 is repeated here for convenience:
- BaseType:
- (one of)
BCDFIJSZ
A reference type signature represents a reference type of the Java programming language, that is, a class or interface type, a type variable, or an array type.
A class type signature represents a (possibly parameterized) class or interface type. A class type signature must be formulated such that it can be reliably mapped to the binary name of the class it denotes by erasing any type arguments and converting each
.character to a$character.A type variable signature represents a type variable.
An array type signature represents one dimension of an array type.
- ReferenceTypeSignature:
- ClassTypeSignature
- TypeVariableSignature
- ArrayTypeSignature
- ClassTypeSignature:
L[PackageSpecifier]
SimpleClassTypeSignature {ClassTypeSignatureSuffix};- PackageSpecifier:
- Identifier
/{PackageSpecifier} - SimpleClassTypeSignature:
- Identifier [TypeArguments]
- TypeArguments:
<TypeArgument {TypeArgument}>- TypeArgument:
- [WildcardIndicator] ReferenceTypeSignature
*- WildcardIndicator:
+-- ClassTypeSignatureSuffix:
.SimpleClassTypeSignature- TypeVariableSignature:
TIdentifier;- ArrayTypeSignature:
[JavaTypeSignature
A class signature encodes type information about a (possibly generic) class declaration. It describes any type parameters of the class, and lists its (possibly parameterized) direct superclass and direct superinterfaces, if any. A type parameter is described by its name, followed by any class bound and interface bounds.
- ClassSignature:
- [TypeParameters] SuperclassSignature {SuperinterfaceSignature}
- TypeParameters:
<TypeParameter {TypeParameter}>- TypeParameter:
- Identifier ClassBound {InterfaceBound}
- ClassBound:
:[ReferenceTypeSignature]- InterfaceBound:
:ReferenceTypeSignature- SuperclassSignature:
- ClassTypeSignature
- SuperinterfaceSignature:
- ClassTypeSignature
A method signature encodes type information about a (possibly generic) method declaration. It describes any type parameters of the method; the (possibly parameterized) types of any formal parameters; the (possibly parameterized) return type, if any; and the types of any exceptions declared in the method's throws clause.
- MethodSignature:
- [TypeParameters]
({JavaTypeSignature})Result {ThrowsSignature} - Result:
- JavaTypeSignature
- VoidDescriptor
- ThrowsSignature:
^ClassTypeSignature^TypeVariableSignature
The following production from 4.3.3 is repeated here for convenience:
- VoidDescriptor:
V
A method signature encoded by the
Signatureattribute may not correspond exactly to the method descriptor in themethod_infostructure (4.3.3). In particular, there is no assurance that the number of formal parameter types in the method signature is the same as the number of parameter descriptors in the method descriptor. The numbers are the same for most methods, but certain constructors in the Java programming language have an implicitly declared parameter which a compiler represents with a parameter descriptor but may omit from the method signature. See the note in 4.7.18 for a similar situation involving parameter annotations.
A field signature encodes the (possibly parameterized) type of a field, formal parameter, or local variable, or record component declaration.
- FieldSignature:
- ReferenceTypeSignature
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, or component_info structure (4.1, 4.5, 4.6, 4.7.30). The RuntimeVisibleAnnotations attribute records run-time visible annotations on the declaration of the corresponding class, field, or method, or record component.
There may be at most one RuntimeVisibleAnnotations attribute in the attributes table of a ClassFile, field_info, or method_info, or component_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.17 The RuntimeInvisibleAnnotations Attribute
The RuntimeInvisibleAnnotations attribute is a variable-length attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure (4.1, 4.5, 4.6, 4.7.30). The RuntimeInvisibleAnnotations attribute records run-time invisible annotations on the declaration of the corresponding class, method, or field, or record component.
There may be at most one RuntimeInvisibleAnnotations attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure.
The
RuntimeInvisibleAnnotationsattribute is similar to theRuntimeVisibleAnnotationsattribute (4.7.16), except that the annotations represented by aRuntimeInvisibleAnnotationsattribute must not be made available for return by reflective APIs, unless the Java Virtual Machine has been instructed to retain these annotations via some implementation-specific mechanism such as a command line flag. In the absence of such instructions, the Java Virtual Machine ignores this attribute.
The RuntimeInvisibleAnnotations attribute has the following format:
RuntimeInvisibleAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
annotation annotations[num_annotations];
}
The items of the RuntimeInvisibleAnnotations_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 "RuntimeInvisibleAnnotations".- 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 invisible annotations represented by the structure.- annotations[]
Each entry in the
annotationstable represents a single run-time invisible annotation on a declaration. Theannotationstructure is specified in 4.7.16.
4.7.20 The RuntimeVisibleTypeAnnotations Attribute
The RuntimeVisibleTypeAnnotations attribute is an variable-length attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure, or Code attribute (4.1, 4.5, 4.6, 4.7.30, 4.7.3). The RuntimeVisibleTypeAnnotations attribute records run-time visible annotations on types used in the declaration of the corresponding class, field, or method, or record component, or in an expression in the corresponding method body. The RuntimeVisibleTypeAnnotations attribute also records 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, or method_info, or 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
implementsclause of a class declaration are recorded in theRuntimeVisibleTypeAnnotationsattribute of the class'sClassFilestructure. Meanwhile, all annotations on the type in a field declaration are recorded in theRuntimeVisibleTypeAnnotationsattribute of the field'sfield_infostructure.
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, 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 |
It may be appropriate to introduce a new target_type value for record components rather than re-using 0x13.
4.7.20.1 The target_info union
...
The
empty_targetitem indicates that an annotation appears on either the type in a field declaration, the type in a record component declaration, the return type of a method, the type of a newly constructed object, or the receiver type of a method or constructor.empty_target { }Only one type appears in each of these locations, so there is no per-type information to represent in the
target_infounion.
...
4.7.21 The RuntimeInvisibleTypeAnnotations Attribute
The RuntimeInvisibleTypeAnnotations attribute is an variable-length attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure, or Code attribute (4.1, 4.5, 4.6, 4.7.30, 4.7.3). The RuntimeInvisibleTypeAnnotations attribute records run-time invisible annotations on types used in the corresponding declaration of a class, field, or method, or record component, or in an expression in the corresponding method body. The RuntimeInvisibleTypeAnnotations attribute also records annotations on type parameter declarations of generic classes, interfaces, methods, and constructors.
There may be at most one RuntimeInvisibleTypeAnnotations attribute in the attributes table of a ClassFile, field_info, or method_info, or component_info structure, or Code attribute.
An attributes table contains a RuntimeInvisibleTypeAnnotations attribute only if types are annotated in kinds of declaration or expression that correspond to the parent structure or attribute of the attributes table.
The RuntimeInvisibleTypeAnnotations attribute has the following format:
RuntimeInvisibleTypeAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
type_annotation annotations[num_annotations];
}
The items of the RuntimeInvisibleTypeAnnotations_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 "RuntimeInvisibleTypeAnnotations".- 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 invisible type annotations represented by the structure.- annotations[]
Each entry in the
annotationstable represents a single run-time invisible annotation on a type used in a declaration or expression. Thetype_annotationstructure is specified in 4.7.20.
4.7.30 The Record Attribute
The Record attribute is a variable-length attribute in the attributes table of a ClassFile structure. A class with the Record attribute is a record class, and has a specified list of components.
There may be at most one Record attribute in the attributes table of a ClassFile structure.
The Record attribute has the following format:
Record_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 components_count;
component_info components[components_count];
}
The items of the Record_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 "Record".- attribute_length
The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes.- components_count
The value of the
components_countitem indicates the number of entries in thecomponentstable.- components[]
Each component of the record must have exactly one corresponding entry in the
componentsarray, in the order in which the components are declared.The
component_infostructure has the following form:component_info { u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }The items of the
component_infostructure are as follows:- name_index
The value of the
name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure that represents the unqualified name (4.2.2) of the record component.- descriptor_index
The value of the
descriptor_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing a field descriptor which encodes the JVM type of the record component (4.3.2).- attributes_count
The value of the
attributes_countitem indicates the number of additional attributes of this record component.- attributes[]
Each value of the
attributestable must be anattribute_infostructure (4.7).A record component can have any number of optional attributes associated with it.
The attributes defined by this specification as appearing in the
attributestable of acomponent_infostructure are listed in Table 4.7-C.The rules concering attributes defined to appear in the
attributestable of acomponent_infostructure are given in 4.7.The rules concerning non-predefined attributes in the
attributestable of acomponent_infostructure are given in [4.7.1].
We're being intentionally vague here about just what it means for a class to have a "component". While it's smart to avoid too many commitments, it may make sense to be more assertive—e.g., by claiming that a method with the given name is declared by the class.