The class File Format
This chapter describes the Java virtual machine
class file format. Each class file contains the definition of a single class or interface. Although a class or interface need not have an external representation literally contained in a file (for instance, because the class is generated by a class loader), we will colloquially refer to any valid representation of a class or interface as being in the class file format. A
class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In the Java and Java 2 platforms, this format is supported by interfaces java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.This chapter defines its own set of data types representing
class file data: The types u1, u2, and u4 represent an unsigned one-, two-, or four-byte quantity, respectively. In the Java and Java 2 platforms, these types may be read by methods such as readUnsignedByte, readUnsignedShort, and readInt of the interface java.io.DataInput.This chapter presents the
class file format using pseudostructures written in a C-like structure notation. To avoid confusion with the fields of classes and class instances, etc., the contents of the structures describing the class file format are referred to as items. Successive items are stored in the class file sequentially, without padding or alignment. Tables, consisting of zero or more variable-sized items, are used in several
class file structures. Although we use C-like array syntax to refer to table items, the fact that tables are streams of varying-sized structures means that it is not possible to translate a table index directly to a byte offset into the table. Where we refer to a data structure as an array, it consists of zero or more contiguous fixed-sized items and can be indexed like an array.
4.1 The ClassFile Structure
A class file consists of a single ClassFile structure: The items in theClassFile {u4 magic;u2 minor_version;u2 major_version;u2 constant_pool_count;cp_info constant_pool[constant_pool_count-1];u2 access_flags;u2 this_class;u2 super_class;u2 interfaces_count;u2 interfaces[interfaces_count];u2 fields_count;field_info fields[fields_count];u2 methods_count;method_info methods[methods_count];u2 attributes_count;attribute_info attributes[attributes_count];}
ClassFile structure are as follows: magic- The
magicitem supplies the magic number identifying theclassfile format; it has the value0xCAFEBABE. minor_version,major_version- The values of the
minor_versionandmajor_versionitems are the minor and major version numbers of thisclassfile.Together, a major and a minor version number determine the version of theclassfile format. If aclassfile has major version number M and minor version number m, we denote the version of itsclassfile format as M.m. Thus,classfile format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1. A Java virtual machine implementation can support aclassfile format of version v if and only if v lies in some contiguous range Mi.0
v
Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.1 constant_pool_count- The value of the
constant_pool_countitem is equal to the number of entries in theconstant_pooltable plus one. Aconstant_poolindex is considered valid if it is greater than zero and less thanconstant_pool_count, with the exception for constants of typelonganddoublenoted in §4.4.5. constant_pool[]- The
constant_poolis a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within theClassFilestructure and its substructures. The format of eachconstant_pooltable entry is indicated by its first "tag" byte. Theconstant_pooltable is indexed from1toconstant_pool_count-1. access_flags- The value of the
access_flagsitem is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation of each flag, when set, is as shown in Table 4.1.
An interface is distinguished by its
ACC_INTERFACE flag being set. If its ACC_INTERFACE flag is not set, this class file defines a class, not an interface.If the
ACC_INTERFACE flag of this class file is set, its ACC_ABSTRACT flag must also be set (§2.13.1) and its ACC_PUBLIC flag may be set. Such a class file may not have any of the other flags in Table 4.1 set.If the
ACC_INTERFACE flag of this class file is not set, it may have any of the other flags in Table 4.1 set. However, such a class file cannot have both its ACC_FINAL and ACC_ABSTRACT flags set (§2.8.2). The setting of the
ACC_SUPER flag indicates which of two alternative semantics for its invokespecial instruction the Java virtual machine is to express; the ACC_SUPER flag exists for backward compatibility for code compiled by Sun's older compilers for the Java programming language. All new implementations of the Java virtual machine should implement the semantics for invokespecial documented in this specification. All new compilers to the instruction set of the Java virtual machine should set the ACC_SUPER flag. Sun's older compilers generated ClassFile flags with ACC_SUPER unset. Sun's older Java virtual machine implementations ignore the flag if it is set.All bits of the
access_flags item not assigned in Table 4.1 are reserved for future use. They should be set to zero in generated class files and should be ignored by Java virtual machine implementations.this_class- The value of the
this_classitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the class or interface defined by thisclassfile. super_class- For a class, the value of the
super_classitem either must be zero or must be a valid index into theconstant_pooltable. If the value of thesuper_classitem is nonzero, theconstant_poolentry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the direct superclass of the class defined by thisclassfile. Neither the direct superclass nor any of its superclasses may be afinalclass. If the value of thesuper_classitem is zero, then thisclassfile must represent the classObject, the only class or interface without a direct superclass. For an interface, the value of thesuper_classitem must always be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_infostructure representing the classObject. interfaces_count- The value of the
interfaces_countitem gives the number of direct superinterfaces of this class or interface type. interfaces[]- Each value in the
interfacesarray must be a valid index into theconstant_pooltable. Theconstant_poolentry at each value ofinterfaces[i], where0
i < interfaces_count, must be aCONSTANT_Class_info(§4.4.1) structure representing an interface that is a direct superinterface of this class or interface type, in the left-to-right order given in the source for the type. - fields_count
- The value of the
fields_countitem gives the number offield_infostructures in thefieldstable. Thefield_info(§4.5) structures represent all fields, both class variables and instance variables, declared by this class or interface type. fields[]- Each value in the
fieldstable must be afield_info(§4.5) structure giving a complete description of a field in this class or interface. Thefieldstable includes only those fields that are declared by this class or interface. It does not include items representing fields that are inherited from superclasses or superinterfaces. methods_count- The value of the
methods_countitem gives the number ofmethod_infostructures in themethodstable. methods[]- Each value in the
methodstable must be amethod_info(§4.6) structure giving a complete description of a method in this class or interface. If the method is notnativeorabstract, the Java virtual machine instructions implementing the method are also supplied. Themethod_infostructures represent all methods declared by this class or interface type, including instance methods, class (static) methods, instance initialization methods (§3.9), and any class or interface initialization method (§3.9). Themethodstable does not include items representing methods that are inherited from superclasses or superinterfaces. attributes_count- The value of the
attributes_countitem gives the number of attributes (§4.7) in theattributestable of this class. attributes[]- Each value of the
attributestable must be an attribute structure (§4.7). The only attributes defined by this specification as appearing in theattributestable of aClassFilestructure are theSourceFileattribute (§4.7.7) and theDeprecated(§4.7.10) attribute. A Java virtual machine implementation is required to silently ignore any or all attributes in theattributestable of aClassFilestructure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclassfile, but only to provide additional descriptive information (§4.7.1).
4.2 The Internal Form of Fully Qualified Class and Interface Names
Class and interface names that appear inclass file structures are always represented in a fully qualified form (§2.7.5). Such names are always represented as CONSTANT_Utf8_info (§4.4.7) structures and thus may be drawn, where not further constrained, from the entire Unicode character set. Class names and interfaces are referenced both from those CONSTANT_NameAndType_info (§4.4.6) structures that have such names as part of their descriptor (§4.3) and from all CONSTANT_Class_info (§4.4.1) structures. For historical reasons the syntax of fully qualified class and interface names that appear in class file structures differs from the familiar syntax of fully qualified names documented in §2.7.5. In this internal form, the ASCII periods ('.') that normally separate the identifiers that make up the fully qualified name are replaced by ASCII forward slashes ('/'). For example, the normal fully qualified name of class Thread is java.lang.Thread. In the form used in descriptors in the class file format, a reference to the name of class Thread is implemented using a CONSTANT_Utf8_info structure representing the string "java/lang/Thread".4.3 Descriptors
A descriptor is a string representing the type of a field or method. Descriptors are represented in theclass file format using UTF-8 strings (§4.4.7) and thus may be drawn, where not further constrained, from the entire Unicode character set. 4.3.1 Grammar Notation
Descriptors are specified using a grammar. This grammar is a set of productions that describe how sequences of characters can form syntactically correct descriptors of various types. Terminal symbols of the grammar are shown in bold fixed-width font. Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined, followed by a colon. One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the production: FieldType:BaseType
ObjectType
ArrayType
states that a FieldType may represent either a BaseType, an ObjectType, or an ArrayType.
A nonterminal symbol on the right-hand side of a production that is followed by an asterisk (*) represents zero or more possibly different values produced from that nonterminal, appended without any intervening space. The production:
MethodDescriptor:
( ParameterDescriptor* ) ReturnDescriptor
states that a MethodDescriptor represents a left parenthesis, followed by zero or more ParameterDescriptor values, followed by a right parenthesis, followed by a ReturnDescriptor.
4.3.2 Field Descriptors
A field descriptor represents the type of a class, instance, or local variable. It is a series of characters generated by the grammar: FieldDescriptor:FieldTypeComponentType:
FieldTypeFieldType:
BaseType ObjectTypeBaseType:
ArrayType
B CObjectType:
D
F
I
J
S
Z
L <classname> ;ArrayType:
[ ComponentTypeThe characters of BaseType, the L and ; of ObjectType, and the [ of ArrayType are all ASCII characters. The <classname> represents a fully qualified class or interface name. For historical reasons it is encoded in internal form (§4.2). The interpretation of the field types is as shown in Table 4.2.
For example, the descriptor of an instance variable of type
int is simply I. The descriptor of an instance variable of type Object is Ljava/lang/Object;. Note that the internal form of the fully qualified name for class Object is used. The descriptor of an instance variable that is a multidimensional double array, double d[][][]; is [[[D
4.3.3 Method Descriptors
A method descriptor represents the parameters that the method takes and the value that it returns:MethodDescriptor:A parameter descriptor represents a parameter passed to a method:
( ParameterDescriptor* ) ReturnDescriptor
ParameterDescriptor:A return descriptor represents the type of the value returned from a method. It is a series of characters generated by the grammar:
FieldType
ReturnDescriptor:The character V indicates that the method returns no value (its return type is
FieldType
V
void). A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations. The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.For example, the method descriptor for the method
Object mymethod(int i, double d, Thread t) is (IDLjava/lang/Thread;)Ljava/lang/Object;Note that internal forms of the fully qualified names of
Thread and Object are used in the method descriptor. The method descriptor for mymethod is the same whether mymethod is a class or an instance method. Although an instance method is passed this, a reference to the current class instance, in addition to its intended parameters, that fact is not reflected in the method descriptor. (A reference to this is not passed to a class method.) The reference to this is passed implicitly by the method invocation instructions of the Java virtual machine used to invoke instance methods.4.4 The Constant Pool
Java virtual machine instructions do not rely on the runtime layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in theconstant_pool table. All constant_pool table entries have the following general format:Each item in thecp_info {u1 tag;u1 info[];}
constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.4.4.1 The CONSTANT_Class_info Structure
The CONSTANT_Class_info structure is used to represent a class or an interface: The items of theCONSTANT_Class_info {u1 tag;u2 name_index;}
CONSTANT_Class_info structure are the following: tag- The
tagitem has the valueCONSTANT_Class(7). 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_info(§4.4.7) structure representing a valid fully qualified class or interface name (§2.8.1) encoded in internal form (§4.2).
CONSTANT_Class_info (§4.4.1) structures in the constant_pool table. For such array classes, the name of the class is the descriptor of the array type. For example, the class name representing a two-dimensional int array type int[][] is [[I The class name representing the type array of class Thread Thread[] is [Ljava/lang/Thread; An array type descriptor is valid only if it represents 255 or fewer dimensions. 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures
Fields, methods, and interface methods are represented by similar structures: The items of these structures are as follows:CONSTANT_Fieldref_info {u1 tag;u2 class_index;u2 name_and_type_index;}CONSTANT_Methodref_info {u1 tag;u2 class_index;u2 name_and_type_index;}CONSTANT_InterfaceMethodref_info {u1 tag;u2 class_index;u2 name_and_type_index;}
tag- The
tagitem of aCONSTANT_Fieldref_infostructure has the valueCONSTANT_Fieldref(9). Thetagitem of aCONSTANT_Methodref_infostructure has the valueCONSTANT_Methodref(10). Thetagitem of aCONSTANT_InterfaceMethodref_infostructure has the valueCONSTANT_InterfaceMethodref(11). class_index- The value of the
class_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the class or interface type that contains the declaration of the field or method. Theclass_indexitem of aCONSTANT_Methodref_infostructure must be a class type, not an interface type. Theclass_indexitem of aCONSTANT_InterfaceMethodref_infostructure must be an interface type. Theclass_indexitem of aCONSTANT_Fieldref_infostructure may be either a class type or an interface type. name_and_type_index- The value of the
name_and_type_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_NameAndType_info(§4.4.6) structure. Thisconstant_poolentry indicates the name and descriptor of the field or method. In aCONSTANT_Fieldref_infothe indicated descriptor must be a field descriptor (§4.3.2). Otherwise, the indicated descriptor must be a method descriptor (§4.3.3). If the name of the method of aCONSTANT_Methodref_infostructure begins with a' <'('\u003c'), then the name must be the special name<init>, representing an instance initialization method (§3.9). Such a method must return no value.
4.4.3 The CONSTANT_String_info Structure
The CONSTANT_String_info structure is used to represent constant objects of the type String: The items of theCONSTANT_String_info {u1 tag;u2 string_index;}
CONSTANT_String_info structure are as follows: tag
- The
tagitem of theCONSTANT_String_infostructure has the valueCONSTANT_String(8). string_index
- The value of the
string_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the sequence of characters to which theStringobject is to be initialized.
4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures
The CONSTANT_Integer_info and CONSTANT_Float_info structures represent 4-byte numeric (int and float) constants: The items of these structures are as follows:CONSTANT_Integer_info {u1 tag;u4 bytes;}CONSTANT_Float_info {u1 tag;u4 bytes;}
tag- The
tagitem of theCONSTANT_Integer_infostructure has the valueCONSTANT_Integer(3). Thetagitem of theCONSTANT_Float_infostructure has the valueCONSTANT_Float(4). bytes- The
bytesitem of theCONSTANT_Integer_infostructure represents the value of theintconstant. The bytes of the value are stored in big-endian (high byte first) order. Thebytesitem of theCONSTANT_Float_infostructure represents the value of thefloatconstant in IEEE 754 floating-point single format (§3.3.2). The bytes of the single format representation are stored in big-endian (high byte first) order. The value represented by theCONSTANT_Float_infostructure is determined as follows. The bytes of the value are first converted into anintconstant bits. Then:- If bits is
0x7f800000, thefloatvalue will be positive infinity. - If bits is
0xff800000, thefloatvalue will be negative infinity. - If bits is in the range
0x7f800001through0x7fffffffor in the range0xff800001through0xffffffff, thefloatvalue will be NaN. - In all other cases, let
s,e, andmbe three values that might be computed from bits:
int s = ((bits>> 31) == 0) ? 1 : -1;int e = ((bits>> 23) & 0xff);int m = (e == 0) ?(bits& 0x7fffff) << 1 :(bits& 0x7fffff) | 0x800000;Then thefloatvalue equals the result of the mathematical expressions·m·2e-150.4.4.5 The
TheCONSTANT_Long_infoandCONSTANT_Double_infoStructuresCONSTANT_Long_infoandCONSTANT_Double_inforepresent 8-byte numeric (longanddouble) constants:
All 8-byte constants take up two entries in theCONSTANT_Long_info {u1 tag;u4 high_bytes;u4 low_bytes;}CONSTANT_Double_info {u1 tag;u4 high_bytes;u4 low_bytes;}constant_pooltable of theclassfile. If aCONSTANT_Long_infoorCONSTANT_Double_infostructure is the item in theconstant_pooltable at indexn, then the next usable item in the pool is located at indexn+2. Theconstant_poolindexn+1must be valid but is considered unusable.2 The items of these structures are as follows:tag- The
tagitem of theCONSTANT_Long_infostructure has the valueCONSTANT_Long(5). Thetagitem of theCONSTANT_Double_infostructure has the valueCONSTANT_Double(6). high_bytes,low_bytes- The unsigned
high_bytesandlow_bytesitems of theCONSTANT_Long_infostructure together represent the value of thelongconstant ((long)high_bytes<< 32) +low_bytes, where the bytes of each ofhigh_bytesandlow_bytesare stored in big-endian (high byte first) order. Thehigh_bytesandlow_bytesitems of theCONSTANT_Double_infostructure together represent thedoublevalue in IEEE 754 floating-point double format (§3.3.2). The bytes of each item are stored in big-endian (high byte first) order. The value represented by theCONSTANT_Double_infostructure is determined as follows. Thehigh_bytesandlow_bytesitems are first converted into thelongconstant bits, which is equal to ((long)high_bytes<< 32) +low_bytes. Then:- If bits is
0x7ff0000000000000L, thedoublevalue will be positive infinity. - If bits is
0xfff0000000000000L, thedoublevalue will be negative infinity. - If bits is in the range
0x7ff0000000000001Lthrough0x7fffffffffffffffLor in the range0xfff0000000000001Lthrough0xffffffffffffffffL, thedoublevalue will be NaN. - In all other cases, let
s,e, andmbe three values that might be computed from bits:
int s = ((bits>> 63) == 0) ? 1 : -1;int e = (int)((bits>> 52) & 0x7ffL);long m = (e == 0) ?(bits& 0xfffffffffffffL) << 1 :(bits& 0xfffffffffffffL) | 0x10000000000000L;- Then the floating-point value equals the
doublevalue of the mathematical expressions·m·2e-1075. - If bits is
4.4.6 The
TheCONSTANT_NameAndType_infoStructureCONSTANT_NameAndType_infostructure is used to represent a field or method, without indicating which class or interface type it belongs to:
The items of theCONSTANT_NameAndType_info {u1 tag;u2 name_index;u2 descriptor_index;}CONSTANT_NameAndType_infostructure are as follows:
tag- The
tagitem of theCONSTANT_NameAndType_infostructure has the valueCONSTANT_NameAndType(12). 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_info(§4.4.7) structure representing either a valid field or method name (§2.7) stored as a simple name (§2.7.1), that is, as a Java programming language identifier (§2.2) or as the special method name<init>(§3.9). 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_info(§4.4.7) structure representing a valid field descriptor (§4.3.2) or method descriptor (§4.3.3).
4.4.7 The
TheCONSTANT_Utf8_infoStructureCONSTANT_Utf8_infostructure is used to represent constant string values. UTF-8 strings are encoded so that character sequences that contain only non-null ASCII characters can be represented using only 1 byte per character, but characters of up to 16 bits can be represented. All characters in the range'\u0001'to'\u007F'are represented by a single byte:
0 bits 6-0
The 7 bits of data in the byte give the value of the character represented. The null character ('\u0000') and characters in the range'\u0080'to'\u07FF'are represented by a pair of bytes x and y:
x:
1 1 0 bits 10-6
y:
1 0 bits 5-0
The bytes represent the character with the value ((x &0x1f) <<6) + (y &0x3f).
Characters in the range'\u0800'to'\uFFFF'are represented by 3 bytes x, y, and z:
x:
1 1 1 0 bits 15-12
y:
1 0 bits 11-6
z:
1 0 bits 5-0
The character with the value ((x &0xf) <<12) + ((y &0x3f) <<6) + (z &0x3f) is represented by the bytes.
The bytes of multibyte characters are stored in theclassfile in big-endian (high byte first) order.
There are two differences between this format and the "standard" UTF-8 format. First, the null byte(byte)0is encoded using the 2-byte format rather than the 1-byte format, so that Java virtual machine UTF-8 strings never have embedded nulls. Second, only the 1-byte, 2-byte, and 3-byte formats are used. The Java virtual machine does not recognize the longer UTF-8 formats.
For more information regarding the UTF-8 format, see File System Safe UCS Transformation Format (FSS_UTF), X/Open Preliminary Specification (X/Open Company Ltd., Document Number: P316). This information also appears in ISO/IEC 10646, Annex P.
TheCONSTANT_Utf8_infostructure is
The items of theCONSTANT_Utf8_info {u1 tag;u2 length;u1 bytes[length];}CONSTANT_Utf8_infostructure are the following:
tag- The
tagitem of theCONSTANT_Utf8_infostructure has the valueCONSTANT_Utf8(1). length- The value of the
lengthitem gives the number of bytes in thebytesarray (not the length of the resulting string). The strings in theCONSTANT_Utf8_infostructure are not null-terminated. bytes[]- The
bytesarray contains the bytes of the string. No byte may have the value(byte)0or lie in the range(byte)0xf0-(byte)0xff.
4.5 Fields
Each field is described by afield_infostructure. No two fields in oneclassfile may have the same name and descriptor (§4.3.2). The format of this structure is
The items of thefield_info {u2 access_flags;u2 name_index;u2 descriptor_index;u2 attributes_count;attribute_info attributes[attributes_count];}field_infostructure are as follows:
access_flags- The value of the
access_flagsitem is a mask of flags used to denote access permission to and properties of this field. The interpretation of each flag, when set, is as shown in Table 4.4. Fields of classes may set any of the flags in Table 4.4. However, a specific field of a class may have at most one of itsACC_PRIVATE,ACC_PROTECTED, andACC_PUBLICflags set (§2.7.4) and may not have both itsACC_FINALandACC_VOLATILEflags set (§2.9.1). - Table 4.4. However, a specific field of a class may have at most one of its
ACC_PRIVATE,ACC_PROTECTED, andACC_PUBLICflags set (§2.7.4) and may not have both itsACC_FINALandACC_VOLATILEflags set (§2.9.1). All fields of interfaces must have theirACC_PUBLIC,ACC_STATIC, andACC_FINALflags set and may not have any of the other flags in Table 4.4 set (§2.13.3.1). All bits of theaccess_flagsitem not assigned in Table 4.4 are reserved for future use. They should be set to zero in generatedclassfiles and should be ignored by Java virtual machine implementations. 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_info(§4.4.7) structure which must represent a valid field name (§2.7) stored as a simple name (§2.7.1), that is, as a Java programming language identifier (§2.2). 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_info(§4.4.7) structure that must represent a valid field descriptor (§4.3.2). attributes_count- The value of the
attributes_countitem indicates the number of additional attributes (§4.7) of this field. attributes[]- Each value of the
attributestable must be an attribute structure (§4.7). A field can have any number of attributes associated with it. The attributes defined by this specification as appearing in theattributestable of afield_infostructure are theConstantValue(§4.7.2),Synthetic(§4.7.6), andDeprecated(§4.7.10) attributes. A Java virtual machine implementation must recognize and correctly readConstantValue(§4.7.2) attributes found in theattributestable of afield_infostructure. A Java virtual machine implementation is required to silently ignore any or all other attributes in theattributestable that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclassfile, but only to provide additional descriptive information (§4.7.1).
4.6 Methods
Each method, including each instance initialization method (§3.9) and the class or interface initialization method (§3.9), is described by amethod_infostructure. No two methods in oneclassfile may have the same name and descriptor (§4.3.3). The structure has the following format:
The items of themethod_info {u2 access_flags;u2 name_index;u2 descriptor_index;u2 attributes_count;attribute_info attributes[attributes_count];}method_infostructure are as follows:
access_flags- The value of the
access_flagsitem is a mask of flags used to denote access permission to and properties of this method. The interpretation of each flag, when set, is as shown in Table 4.5. Table 4.5. However, a specific method of a class may have at most one of itsACC_PRIVATE,ACC_PROTECTED, andACC_PUBLICflags set (§2.7.4). If such a method has itsACC_ABSTRACTflag set it may not have any of itsACC_FINAL,ACC_NATIVE,ACC_PRIVATE,ACC_STATIC,ACC_STRICT, orACC_SYNCHRONIZEDflags set (§2.13.3.2). All interface methods must have theirACC_ABSTRACTandACC_PUBLICflags set and may not have any of the other flags in Table 4.5 set (§2.13.3.2). A specific instance initialization method (§3.9) may have at most one of itsACC_PRIVATE,ACC_PROTECTED, andACC_PUBLICflags set and may also have itsACC_STRICTflag set, but may not have any of the other flags in Table 4.5 set. Class and interface initialization methods (§3.9) are called implicitly by the Java virtual machine; the value of theiraccess_flagsitem is ignored except for the settings of theACC_STRICTflag. All bits of theaccess_flagsitem not assigned in Table 4.5 are reserved for future use. They should be set to zero in generatedclassfiles and should be ignored by Java virtual machine implementations. 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_info(§4.4.7) structure representing either one of the special method names (§3.9),<init>or<clinit>, or a valid method name in the Java programming language (§2.7), stored as a simple name (§2.7.1). 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_info(§4.4.7) structure representing a valid method descriptor (§4.3.3). attributes_count- The value of the
attributes_countitem indicates the number of additional attributes (§4.7) of this method. attributes[]- Each value of the
attributestable must be an attribute structure (§4.7). A method can have any number of optional attributes associated with it. The only attributes defined by this specification as appearing in theattributestable of amethod_infostructure are theCode(§4.7.3),Exceptions(§4.7.4),Synthetic(§4.7.6), andDeprecated(§4.7.10) attributes. A Java virtual machine implementation must recognize and correctly readCode(§4.7.3) andExceptions(§4.7.4) attributes found in theattributestable of amethod_infostructure. A Java virtual machine implementation is required to silently ignore any or all other attributes in theattributestable of amethod_infostructure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclassfile, but only to provide additional descriptive information (§4.7.1).
4.7 Attributes
Attributes are used in theClassFile(§4.1),field_info(§4.5),method_info(§4.6), andCode_attribute(§4.7.3) structures of theclassfile format. All attributes have the following general format:
For all attributes, theattribute_info {u2 attribute_name_index;u4 attribute_length;u1 info[attribute_length];}attribute_name_indexmust be a valid unsigned 16-bit index into the constant pool of the class. Theconstant_poolentry atattribute_name_indexmust be aCONSTANT_Utf8_info(§4.4.7) structure representing the name of the attribute. The value of theattribute_lengthitem indicates the length of the subsequent information in bytes. The length does not include the initial six bytes that contain theattribute_name_indexandattribute_lengthitems. Certain attributes are predefined as part of theclassfile specification. The predefined attributes are theSourceFile(§4.7.7),ConstantValue(§4.7.2),Code(§4.7.3),Exceptions(§4.7.4),InnerClasses(§4.7.5),Synthetic(§4.7.6),LineNumberTable(§4.7.8),LocalVariableTable(§4.7.9), andDeprecated(§4.7.10) attributes. Within the context of their use in this specification, that is, in theattributestables of theclassfile structures in which they appear, the names of these predefined attributes are reserved.
Of the predefined attributes, theCode,ConstantValue, andExceptionsattributes must be recognized and correctly read by aclassfile reader for correct interpretation of theclassfile by a Java virtual machine implementation. TheInnerClassesandSyntheticattributes must be recognized and correctly read by aclassfile reader in order to properly implement the Java and Java 2 platform class libraries (§3.12). Use of the remaining predefined attributes is optional; aclassfile reader may use the information they contain, or otherwise must silently ignore those attributes.
4.7.1 Defining and Naming New Attributes
Compilers are permitted to define and emitclassfiles containing new attributes in theattributestables ofclassfile structures. Java virtual machine implementations are permitted to recognize and use new attributes found in theattributestables ofclassfile structures. However, any attribute not defined as part of this Java virtual machine specification must not affect the semantics of class or interface types. Java virtual machine implementations are required to silently ignore attributes they do not recognize. For instance, defining a new attribute to support vendor-specific debugging is permitted. Because Java virtual machine implementations are required to ignore attributes they do not recognize,classfiles intended for that particular Java virtual machine implementation will be usable by other implementations even if those implementations cannot make use of the additional debugging information that theclassfiles contain.
Java virtual machine implementations are specifically prohibited from throwing an exception or otherwise refusing to useclassfiles simply because of the presence of some new attribute. Of course, tools operating onclassfiles may not run correctly if givenclassfiles that do not contain all the attributes they require.
Two attributes that are intended to be distinct, but that happen to use the same attribute name and are of the same length, will conflict on implementations that recognize either attribute. Attributes defined other than by Sun must have names chosen according to the package naming convention defined by The Java Language Specification. For instance, a new attribute defined by Netscape might have the name"com.Netscape.new-attribute".3
Sun may define additional attributes in future versions of thisclassfile specification.
4.7.2 The
TheConstantValueAttributeConstantValueattribute is a fixed-length attribute used in theattributestable of thefield_info(§4.5) structures. AConstantValueattribute represents the value of a constant field that must be (explicitly or implicitly)static; that is, theACC_STATICbit (Table 4.4) in theflagsitem of thefield_infostructure must be set. There can be no more than oneConstantValueattribute in theattributestable of a givenfield_infostructure. The constant field represented by thefield_infostructure is assigned the value referenced by itsConstantValueattribute as part of the initialization of the class or interface declaring the constant field (§2.17.4). This occurs immediately prior to the invocation of the class or interface initialization method (§3.9) of that class or interface. If afield_infostructure representing a non-staticfield has aConstantValueattribute, then that attribute must silently be ignored. Every Java virtual machine implementation must recognizeConstantValueattributes.
TheConstantValueattribute has the following format:
The items of theConstantValue_attribute {u2 attribute_name_index;u4 attribute_length;u2 constantvalue_index;}ConstantValue_attributestructure 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_info(§4.4.7) structure representing the string"ConstantValue". attribute_length- The value of the
attribute_lengthitem of aConstantValue_attributestructure must be2. constantvalue_index- The value of the
constantvalue_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index gives the constant value represented by this attribute. Theconstant_poolentry must be of a type appropriate to the field, as shown by Table 4.6.Field Type Entry Type longCONSTANT_LongfloatCONSTANT_FloatdoubleCONSTANT_Doubleint,short,char,byte,booleanCONSTANT_IntegerStringCONSTANT_String
4.7.3 The
TheCodeAttributeCodeattribute is a variable-length attribute used in theattributestable ofmethod_infostructures. ACodeattribute contains the Java virtual machine instructions and auxiliary information for a single method, instance initialization method (§3.9), or class or interface initialization method (§3.9). Every Java virtual machine implementation must recognizeCodeattributes. If the method is eithernativeorabstract, itsmethod_infostructure must not have aCodeattribute. Otherwise, itsmethod_infostructure must have exactly oneCodeattribute. TheCodeattribute has the following format:
The items of theCode_attribute {u2 attribute_name_index;u4 attribute_length;u2 max_stack;u2 max_locals;u4 code_length;u1 code[code_length];u2 exception_table_length;{ u2 start_pc;u2 end_pc;u2 handler_pc;u2 catch_type;} exception_table[exception_table_length];u2 attributes_count;attribute_info attributes[attributes_count];}Code_attributestructure 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_info(§4.4.7) structure representing the string"Code". attribute_length- The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. max_stack- The value of the
max_stackitem gives the maximum depth (§3.6.2) of the operand stack of this method at any point during execution of the method. max_locals- The value of the
max_localsitem gives the number of local variables in the local variable array allocated upon invocation of this method, including the local variables used to pass parameters to the method on its invocation. The greatest local variable index for a value of typelongordoubleismax_locals-2. The greatest local variable index for a value of any other type ismax_locals-1. code_length- The value of the
code_lengthitem gives the number of bytes in thecodearray for this method. The value ofcode_lengthmust be greater than zero; thecodearray must not be empty. code[]- The
codearray gives the actual bytes of Java virtual machine code that implement the method. When thecodearray is read into memory on a byte-addressable machine, if the first byte of the array is aligned on a 4-byte boundary, the tableswitch and lookupswitch 32-bit offsets will be 4-byte aligned. (Refer to the descriptions of those instructions for more information on the consequences ofcodearray alignment.) The detailed constraints on the contents of thecodearray are extensive and are given in a separate section (§4.8). exception_table_length- The value of the
exception_table_lengthitem gives the number of entries in theexception_tabletable. exception_table[]- Each entry in the
exception_tablearray describes one exception handler in thecodearray. The order of the handlers in theexception_tablearray is significant. See Section 3.10 for more details. Eachexception_tableentry contains the following four items:start_pc,end_pc- The values of the two items
start_pcandend_pcindicate the ranges in thecodearray at which the exception handler is active. The value ofstart_pcmust be a valid index into thecodearray of the opcode of an instruction. The value ofend_pceither must be a valid index into thecodearray of the opcode of an instruction or must be equal tocode_length, the length of thecodearray. The value ofstart_pcmust be less than the value ofend_pc. Thestart_pcis inclusive andend_pcis exclusive; that is, the exception handler must be active while the program counter is within the interval [start_pc,end_pc).4 handler_pc- The value of the
handler_pcitem indicates the start of the exception handler. The value of the item must be a valid index into thecodearray and must be the index of the opcode of an instruction. catch_type- If the value of the
catch_typeitem is nonzero, it must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing a class of exceptions that this exception handler is designated to catch. This class must be the classThrowableor one of its subclasses. The exception handler will be called only if the thrown exception is an instance of the given class or one of its subclasses. If the value of thecatch_typeitem is zero, this exception handler is called for all exceptions. This is used to implementfinally(see Section 7.13, "Compilingfinally").
attributes_count- The value of the
attributes_countitem indicates the number of attributes of theCodeattribute. attributes[]- Each value of the
attributestable must be an attribute structure (§4.7). ACodeattribute can have any number of optional attributes associated with it. Currently, theLineNumberTable(§4.7.8) andLocalVariableTable(§4.7.9) attributes, both of which contain debugging information, are defined and used with theCodeattribute.
A Java virtual machine implementation is permitted to silently ignore any or all attributes in theattributestable of aCodeattribute. Attributes not defined in this specification are not allowed to affect the semantics of theclassfile, but only to provide additional descriptive information (§4.7.1).
4.7.4 The
TheExceptionsAttributeExceptionsattribute is a variable-length attribute used in theattributestable of amethod_info(§4.6) structure. TheExceptionsattribute indicates which checked exceptions a method may throw. There may be at most oneExceptionsattribute in eachmethod_infostructure. TheExceptionsattribute has the following format:
The items of theExceptions_attribute {u2 attribute_name_index;u4 attribute_length;u2 number_of_exceptions;u2 exception_index_table[number_of_exceptions];}Exceptions_attributestructure 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 betheCONSTANT_Utf8_info(§4.4.7) structure representing the string"Exceptions". attribute_length- The value of the
attribute_lengthitem indicates the attribute length, excluding the initial six bytes. number_of_exceptions- The value of the
number_of_exceptionsitem indicates the number of entries in theexception_index_table. exception_index_table[]- Each value in the
exception_index_tablearray must be a valid index into theconstant_pooltable. Theconstant_poolentry referenced by each table item must be aCONSTANT_Class_info(§4.4.1) structure representing a class type that this method is declared to throw.
- The exception is an instance of
RuntimeExceptionor one of its subclasses. - The exception is an instance of
Erroror one of its subclasses. - The exception is an instance of one of the exception classes specified in the
exception_index_tablejust described, or one of their subclasses.
4.7.5 The
TheInnerClassesAttributeInnerClassesattribute5 is a variable-length attribute in theattributestable of theClassFile(§4.1) structure. If the constant pool of a class or interface refers to any class or interface that is not a member of a package, itsClassFilestructure must have exactly oneInnerClassesattribute in itsattributestable. TheInnerClassesattribute has the following format:
The items of theInnerClasses_attribute {u2 attribute_name_index;u4 attribute_length;u2 number_of_classes;{ u2 inner_class_info_index;u2 outer_class_info_index;u2 inner_name_index;u2 inner_class_access_flags;} classes[number_of_classes];}InnerClasses_attributestructure 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_info(§4.4.7) structure representing the string"InnerClasses". attribute_length- The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. number_of_classes- The value of the
number_of_classesitem indicates the number of entries in theclassesarray. classes[]- Every
CONSTANT_Class_infoentry in theconstant_pooltable which represents a class or interface C that is not a package member must have exactly one corresponding entry in theclassesarray. If a class has members that are classes or interfaces, itsconstant_pooltable (and hence itsInnerClassesattribute) must refer to each such member, even if that member is not otherwise mentioned by the class. These rules imply that a nested class or interface member will haveInnerClassesinformation for each enclosing class and for each immediate member. Eachclassesarray entry contains the following four items: inner_class_info_index- The value of the
inner_class_info_indexitem must be zero or a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing C. The remaining items in theclassesarray entry give information about C. outer_class_info_index- If C is not a member, the value of the
outer_class_info_indexitem must be zero. Otherwise, the value of theouter_class_info_indexitem must be a valid index into theconstant_pooltable, and the entry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the class or interface of which C is a member. inner_name_index- If C is anonymous, the value of the
inner_name_indexitem must be zero. Otherwise, the value of theinner_name_indexitem must be a valid index into theconstant_pooltable, and the entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure that represents the original simple name of C, as given in the source code from which thisclassfile was compiled. inner_class_access_flags- The value of the
inner_class_access_flagsitem is a mask of flags used to denote access permissions to and properties of class or interface C as declared in the source code from which thisclassfile was compiled. It is used by compilers to recover the original information when source code is not available. The flags are shown in Table 4.7. All bits of theinner_class_access_flagsitem not assigned in Table 4.7 are reserved for future use. They should be set to zero in generatedclassfiles and should be ignored by Java virtual machine implementations.
InnerClassesattribute with anyclassfile actually representing a class or interface referenced by the attribute.
4.7.6 The
TheSyntheticAttributeSyntheticattribute6 is a fixed-length attribute in theattributestable ofClassFile(§4.1),field_info(§4.5), andmethod_info(§4.6) structures. A class member that does not appear in the source code must be marked using aSyntheticattribute.
TheSyntheticattribute has the following format:
The items of theSynthetic_attribute {u2 attribute_name_index;u4 attribute_length;}Synthetic_attributestructure 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_info(§4.4.7) structure representing the string"Synthetic". attribute_length- The value of the
attribute_lengthitem is zero.
4.7.7 The
TheSourceFileAttributeSourceFileattribute is an optional fixed-length attribute in theattributestable of theClassFile(§4.1) structure. There can be no more than oneSourceFileattribute in theattributestable of a givenClassFilestructure. TheSourceFileattribute has the following format:
The items of theSourceFile_attribute {u2 attribute_name_index;u4 attribute_length;u2 sourcefile_index;}SourceFile_attributestructure 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_info(§4.4.7) structure representing the string"SourceFile". attribute_length- The value of the
attribute_lengthitem of aSourceFile_attributestructure must be2. sourcefile_index- The value of the
sourcefile_indexitem must be a valid index into theconstant_pooltable. The constant pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing a string. The string referenced by thesourcefile_indexitem will be interpreted as indicating the name of the source file from which thisclassfile was compiled. It will not be interpreted as indicating the name of a directory containing the file or an absolute path name for the file; such platform-specific additional information must be supplied by the runtime interpreter or development tool at the time the file name is actually used.
4.7.8 The
TheLineNumberTableAttributeLineNumberTableattribute is an optional variable-length attribute in theattributestable of aCode(§4.7.3) attribute. It may be used by debuggers to determine which part of the Java virtual machinecodearray corresponds to a given line number in the original source file. IfLineNumberTableattributes are present in theattributestable of a givenCodeattribute, then they may appear in any order. Furthermore, multipleLineNumberTableattributes may together represent a given line of a source file; that is,LineNumberTableattributes need not be one-to-one with source lines. TheLineNumberTableattribute has the following format:
The items of theLineNumberTable_attribute {u2 attribute_name_index;u4 attribute_length;u2 line_number_table_length;{ u2 start_pc;u2 line_number;} line_number_table[line_number_table_length];}LineNumberTable_attributestructure 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_info(§4.4.7) structure representing the string"LineNumberTable". attribute_length- The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. line_number_table_length- The value of the
line_number_table_lengthitem indicates the number of entries in theline_number_tablearray. line_number_table[]- Each entry in the
line_number_tablearray indicates that the line number in the original source file changes at a given point in thecodearray. Eachline_number_tableentry must contain the following two items: start_pc- The value of the
start_pcitem must indicate the index into thecodearray at which the code for a new line in the original source file begins. The value ofstart_pcmust be less than the value of thecode_lengthitem of theCodeattribute of which thisLineNumberTableis an attribute. line_number- The value of the
line_numberitem must give the corresponding line number in the original source file.
4.7.9 The
TheLocalVariableTableAttributeLocalVariableTableattribute is an optional variable-length attribute of aCode(§4.7.3) attribute. It may be used by debuggers to determine the value of a given local variable during the execution of a method. IfLocalVariableTableattributes are present in theattributestable of a givenCodeattribute, then they may appear in any order. There may be no more than oneLocalVariableTableattribute per local variable in theCodeattribute. TheLocalVariableTableattribute has the following format:
The items of theLocalVariableTable_attribute {u2 attribute_name_index;u4 attribute_length;u2 local_variable_table_length;{ u2 start_pc;u2 length;u2 name_index;u2 descriptor_index;u2 index;} local_variable_table[local_variable_table_length];}LocalVariableTable_attributestructure 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_info(§4.4.7) structure representing the string"LocalVariableTable". attribute_length- The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. local_variable_table_length- The value of the
local_variable_table_lengthitem indicates the number of entries in thelocal_variable_tablearray. local_variable_table[]- Each entry in the
local_variable_tablearray indicates a range ofcodearray offsets within which a local variable has a value. It also indicates the index into the local variable array of the current frame at which that local variable can be found. Each entry must contain the following five items: start_pc,length- The given local variable must have a value at indices into the
codearray in the interval [start_pc,start_pc+length], that is, betweenstart_pcandstart_pc+lengthinclusive. The value ofstart_pcmust be a valid index into thecodearray of thisCodeattribute and must be the index of the opcode of an instruction. Either the value ofstart_pc+lengthmust be a valid index into thecodearray of thisCodeattribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of thatcodearray. name_index,descriptor_index- The value of the
name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must contain aCONSTANT_Utf8_info(§4.4.7)structure representing a valid local variable name stored as a simple name (§2.7.1). The value of thedescriptor_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must contain aCONSTANT_Utf8_info(§4.4.7) structure representing a field descriptor (§4.3.2) encoding the type of a local variable in the source program. index- The given local variable must be at
indexin the local variable array of the current frame. If the local variable atindexis of typedoubleorlong, it occupies bothindexandindex+1.
4.7.10 The
TheDeprecatedAttributeDeprecatedattribute7 is an optional fixed-length attribute in theattributestable ofClassFile(§4.1),field_info(§4.5), andmethod_info(§4.6) structures. A class, interface, method, or field may be marked using aDeprecatedattribute to indicate that the class, interface, method, or field has been superseded. A runtime interpreter or tool that reads theclassfile format, such as a compiler, can use this marking to advise the user that a superseded class, interface, method, or field is being referred to. The presence of aDeprecatedattribute does not alter the semantics of a class or interface. TheDeprecatedattribute has the following format:
The items of theDeprecated_attribute {u2 attribute_name_index;u4 attribute_length;}Deprecated_attributestructure 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_info(§4.4.7) structure representing the string"Deprecated". attribute_length- The value of the
attribute_lengthitem is zero.
4.8 Constraints on Java Virtual Machine Code
The Java virtual machine code for a method, instance initialization method (§3.9), or class or interface initialization method (§3.9) is stored in thecodearray of theCodeattribute of amethod_infostructure of aclassfile. This section describes the constraints associated with the contents of theCode_attributestructure.
4.8.1 Static Constraints
The static constraints on aclassfile are those defining the well-formedness of the file. With the exception of the static constraints on the Java virtual machine code of theclassfile, these constraints have been given in the previous section. The static constraints on the Java virtual machine code in aclassfile specify how Java virtual machine instructions must be laid out in thecodearray and what the operands of individual instructions must be.
The static constraints on the instructions in thecodearray are as follows:
- The
codearray must not be empty, so thecode_lengthitem cannot have the value0. - The value of the
code_lengthitem must be less than65536. - The opcode of the first instruction in the
codearray begins at index0. - Only instances of the instructions documented in Section 6.4 may appear in the
codearray. Instances of instructions using the reserved opcodes (§6.2) or any opcodes not documented in this specification may not appear in thecodearray. - For each instruction in the
codearray except the last, the index of the opcode of the next instruction equals the index of the opcode of the current instruction plus the length of that instruction, including all its operands. The wide instruction is treated like any other instruction for these purposes; the opcode specifying the operation that a wide instruction is to modify is treated as one of the operands of that wide instruction. That opcode must never be directly reachable by the computation. - The last byte of the last instruction in the
codearray must be the byte at indexcode_length-1.
codearray are as follows:
- The target of each jump and branch instruction (jsr, jsr_w, goto, goto_w, ifeq, ifne, ifle, iflt, ifge, ifgt, ifnull, ifnonnull, if_icmpeq, if_icmpne, if_icmple, if_icmplt, if_icmpge, if_icmpgt, if_acmpeq, if_acmpne) must be the opcode of an instruction within this method. The target of a jump or branch instruction must never be the opcode used to specify the operation to be modified by a wide instruction; a jump or branch target may be the wide instruction itself.
- Each target, including the default, of each tableswitch instruction must be the opcode of an instruction within this method. Each tableswitch instruction must have a number of entries in its jump table that is consistent with the value of its low and high jump table operands, and its low value must be less than or equal to its high value. No target of a tableswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a tableswitch target may be a wide instruction itself.
- Each target, including the default, of each lookupswitch instruction must be the opcode of an instruction within this method. Each lookupswitch instruction must have a number of match-offset pairs that is consistent with the value of its npairs operand. The match-offset pairs must be sorted in increasing numerical order by signed match value. No target of a lookupswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a lookupswitch target may be a wide instruction itself.
- The operand of each ldc instruction must be a valid index into the
constant_pooltable. The operands of each ldc_w instruction must represent a valid index into theconstant_pooltable. In both cases the constant pool entry referenced by that index must be of typeCONSTANT_Integer,CONSTANT_Float, orCONSTANT_String. - The operands of each ldc2_w instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_LongorCONSTANT_Double. In addition, the subsequent constant pool index must also be a valid index into the constant pool, and the constant pool entry at that index must not be used. - The operands of each getfield, putfield, getstatic, and putstatic instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Fieldref. - The indexbyte operands of each invokevirtual, invokespecial, and invokestatic instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Methodref. - Only the invokespecial instruction is allowed to invoke an instance initialization method (§3.9). No other method whose name begins with the character
'<'('\u003c') may be called by the method invocation instructions. In particular, the class or interface initialization method specially named<clinit>is never called explicitly from Java virtual machine instructions, but only implicitly by the Java virtual machine itself. - The indexbyte operands of each invokeinterface instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_InterfaceMethodref. The value of the count operand of each invokeinterface instruction must reflect the number of local variables necessary to store the arguments to be passed to the interface method, as implied by the descriptor of theCONSTANT_NameAndType_infostructure referenced by theCONSTANT_InterfaceMethodrefconstant pool entry. The fourth operand byte of each invokeinterface instruction must have the value zero. - The operands of each instanceof, checkcast, new, and anewarray instruction and the indexbyte operands of each multianewarray instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Class. - No anewarray instruction may be used to create an array of more than 255 dimensions.
- No new instruction may reference a
CONSTANT_Classconstant_pooltable entry representing an array class. The new instruction cannot be used to create an array. The new instruction also cannot be used to create an instance of an interface or an instance of anabstractclass. - A multianewarray instruction must be used only to create an array of a type that has at least as many dimensions as the value of its dimensions operand. That is, while a multianewarray instruction is not required to create all of the dimensions of the array type referenced by its indexbyte operands, it must not attempt to create more dimensions than are in the array type. The dimensions operand of each multianewarray instruction must not be zero.
- The atype operand of each newarray instruction must take one of the values
T_BOOLEAN(4),T_CHAR(5),T_FLOAT(6),T_DOUBLE(7),T_BYTE(8),T_SHORT(9),T_INT(10), orT_LONG(11). - The index operand of each iload, fload, aload, istore, fstore, astore, iinc, and ret instruction must be a nonnegative integer no greater than
max_locals-1. - The implicit index of each iload_<n>, fload_<n>, aload_<n>, istore_<n>, fstore_<n>, and astore_<n> instruction must be no greater than the value of
max_locals-1. - The index operand of each lload, dload, lstore, and dstore instruction must be no greater than the value of
max_locals-2. - The implicit index of each lload_<n>, dload_<n>, lstore_<n>, and dstore_<n> instruction must be no greater than the value of
max_locals-2. - The indexbyte operands of each wide instruction modifying an iload, fload, aload, istore, fstore, astore, ret, or iinc instruction must represent a nonnegative integer no greater than
max_locals-1. The indexbyte operands of each wide instruction modifying an lload, dload, lstore, or dstore instruction must represent a nonnegative integer no greater thanmax_locals-2.
4.8.2 Structural Constraints
The structural constraints on thecodearray specify constraints on relationships between Java virtual machine instructions. The structural constraints are as follows:
- Each instruction must only be executed with the appropriate type and number of arguments in the operand stack and local variable array, regardless of the execution path that leads to its invocation. An instruction operating on values of type
intis also permitted to operate on values of typeboolean,byte,char, andshort. (As noted in §3.3.4 and §3.11.1, the Java virtual machine internally converts values of typesboolean,byte,char, andshortto typeint.) - If an instruction can be executed along several different execution paths, the operand stack must have the same depth (§3.6.2) prior to the execution of the instruction, regardless of the path taken.
- At no point during execution can the order of the local variable pair holding a value of type
longordoublebe reversed or the pair split up. At no point can the local variables of such a pair be operated on individually. - No local variable (or local variable pair, in the case of a value of type
longordouble) can be accessed before it is assigned a value. - At no point during execution can the operand stack grow to a depth (§3.6.2) greater than that implied by the
max_stackitem. - At no point during execution can more values be popped from the operand stack than it contains.
- Each invokespecial instruction must name an instance initialization method (§3.9), a method in the current class, or a method in a superclass of the current class.
- When the instance initialization method (§3.9) is invoked, an uninitialized class instance must be in an appropriate position on the operand stack. An instance initialization method must never be invoked on an initialized class instance.
- When any instance method is invoked or when any instance variable is accessed, the class instance that contains the instance method or instance variable must already be initialized.
- There must never be an uninitialized class instance on the operand stack or in a local variable when any backwards branch is taken. There must never be an uninitialized class instance in a local variable in code protected by an exception handler. However, an uninitialized class instance may be on the operand stack in code protected by an exception handler. When an exception is thrown, the contents of the operand stack are discarded.
- Each instance initialization method (§3.9), except for the instance initialization method derived from the constructor of class
Object, must call either another instance initialization method ofthisor an instance initialization method of its direct superclasssuperbefore its instance members are accessed. However, instance fields ofthisthat are declared in the current class may be assigned before calling any instance initialization method. - The arguments to each method invocation must be method invocation compatible (§2.6.8) with the method descriptor (§4.3.3).
- The type of every class instance that is the target of a method invocation instruction must be assignment compatible (§2.6.7) with the class or interface type specified in the instruction.
- Each return instruction must match its method's return type. If the method returns a
boolean,byte,char,short, orint, only the ireturn instruction may be used. If the method returns afloat,long, ordouble, only an freturn, lreturn, or dreturn instruction, respectively, may be used. If the method returns areferencetype, it must do so using an areturn instruction, and the type of the returned value must be assignment compatible (§2.6.7) with the return descriptor (§4.3.3) of the method. All instance initialization methods, class or interface initialization methods, and methods declared to returnvoidmust use only the return instruction. - If getfield or putfield is used to access a
protectedfield of a superclass, then the type of the class instance being accessed must be the same as or a subclass of the current class. If invokevirtual or invokespecial is used to access aprotectedmethod of a superclass, then the type of the class instance being accessed must be the same as or a subclass of the current class. - The type of every class instance accessed by a getfield instruction or modified by a putfield instruction must be assignment compatible (§2.6.7) with the class type specified in the instruction.
- The type of every value stored by a putfield or putstatic instruction must be compatible with the descriptor of the field (§4.3.2) of the class instance or class being stored into. If the descriptor type is
boolean,byte,char,short, orint, then the value must be anint. If the descriptor type isfloat,long, ordouble, then the value must be afloat,long, ordouble, respectively. If the descriptor type is areferencetype, then the value must be of a type that is assignment compatible (§2.6.7) with the descriptor type. - The type of every value stored into an array of type
referenceby an aastore instruction must be assignment compatible (§2.6.7) with the component type of the array. - Each athrow instruction must throw only values that are instances of class
Throwableor of subclasses ofThrowable. - Execution never falls off the bottom of the
codearray. - No return address (a value of type
returnAddress) may be loaded from a local variable. - The instruction following each jsr or jsr_w instruction may be returned to only by a single ret instruction.
- No jsr or jsr_w instruction may be used to recursively call a subroutine if that subroutine is already present in the subroutine call chain. (Subroutines can be nested when using
try-finallyconstructs from within afinallyclause. For more information on Java virtual machine subroutines, see §4.9.6.) - Each instance of type
returnAddresscan be returned to at most once. If a ret instruction returns to a point in the subroutine call chain above the ret instruction corresponding to a given instance of typereturnAddress, then that instance can never be used as a return address.
4.9 Verification of
Even though Sun's compiler for the Java programming language attempts to produce only class files that satisfy all the static constraints in the previous sections, the Java virtual machine has no guarantee that any file it is asked to load was generated by that compiler or is properly formed. Applications such as Sun's HotJava World Wide Web browser do not download source code, which they then compile; these applications download already-compiledclassFilesclassfiles. The HotJava browser needs to determine whether theclassfile was produced by a trustworthy compiler or by an adversary attempting to exploit the virtual machine. An additional problem with compile-time checking is version skew. A user may have successfully compiled a class, sayPurchaseStockOptions, to be a subclass ofTradingClass. But the definition ofTradingClassmight have changed since the time the class was compiled in a way that is not compatible with preexisting binaries. Methods might have been deleted or had their return types or modifiers changed. Fields might have changed types or changed from instance variables to class variables. The access modifiers of a method or variable may have changed frompublictoprivate. For a discussion of these issues, see Chapter 13, "Binary Compatibility," in the first edition of The Java Language Specification or the equivalent chapter in the second edition.
Because of these potential problems, the Java virtual machine needs to verify for itself that the desired constraints are satisfied by theclassfiles it attempts to incorporate. A Java virtual machine implementation verifies that eachclassfile satisfies the necessary constraints at linking time (§2.17.3). Structural constraints on the Java virtual machine code may be checked using a simple theorem prover.
Linking-time verification enhances the performance of the interpreter. Expensive checks that would otherwise have to be performed to verify constraints at run time for each interpreted instruction can be eliminated. The Java virtual machine can assume that these checks have already been performed. For example, the Java virtual machine will already know the following:
- There are no operand stack overflows or underflows.
- All local variable uses and stores are valid.
- The arguments to all the Java virtual machine instructions are of valid types.
classfile verifier is independent of any compiler. It should certify all code generated by Sun's compiler for the Java programming language; it should also certify code that other compilers can generate, as well as code that the current compiler could not possibly generate. Anyclassfile that satisfies the structural criteria and static constraints will be certified by the verifier. Theclassfile verifier is also independent of the Java programming language. Programs written in other languages can be compiled into theclassfile format, but will pass verification only if all the same constraints are satisfied.
4.9.1 The Verification Process
Theclassfile verifier operates in four passes: Pass 1:
When a prospectiveclassfile is loaded (§2.17.2) by the Java virtual machine, the Java virtual machine first ensures that the file has the basic format of aclassfile. The first four bytes must contain the right magic number. All recognized attributes must be of the proper length. Theclassfile must not be truncated or have extra bytes at the end. The constant pool must not contain any superficially unrecognizable information.
Whileclassfile verification properly occurs during class linking (§2.17.3), this check for basicclassfile integrity is necessary for any interpretation of theclassfile contents and can be considered to be logically part of the verification process.
Pass 2:
When theclassfile is linked, the verifier performs all additional verification that can be done without looking at thecodearray of theCodeattribute (§4.7.3). The checks performed by this pass include the following:
- Ensuring that
finalclasses are not subclassed and thatfinalmethods are not overridden. - Checking that every class (except
Object)has a direct superclass. - Ensuring that the constant pool satisfies the documented static constraints: for example, that each
CONSTANT_Class_infostructure in the constant pool contains in itsname_indexitem a valid constant pool index for aCONSTANT_Utf8_infostructure. - Checking that all field references and method references in the constant pool have valid names, valid classes, and a valid type descriptor.
During linking, the verifier checks thecodearray of theCodeattribute for each method of theclassfile by performing data-flow analysis on each method. The verifier ensures that at any given point in the program, no matter what code path is taken to reach that point, the following is true:
- The operand stack is always the same size and contains the same types of values.
- No local variable is accessed unless it is known to contain a value of an appropriate type.
- Methods are invoked with the appropriate arguments.
- Fields are assigned only using values of appropriate types.
- All opcodes have appropriate type arguments on the operand stack and in the local variable array.
For efficiency reasons, certain tests that could in principle be performed in Pass 3 are delayed until the first time the code for the method is actually invoked. In so doing, Pass 3 of the verifier avoids loadingclassfiles unless it has to.
For example, if a method invokes another method that returns an instance of classA, and that instance is assigned only to a field of the same type, the verifier does not bother to check if the classAactually exists. However, if it is assigned to a field of the typeB, the definitions of bothAandBmust be loaded in to ensure thatAis a subclass ofB.
Pass 4 is a virtual pass whose checking is done by the appropriate Java virtual machine instructions. The first time an instruction that references a type is executed, the executing instruction does the following:
- Loads in the definition of the referenced type if it has not already been loaded.
- Checks that the currently executing type is allowed to reference the type.
- Ensures that the referenced method or field exists in the given class.
- Checks that the referenced method or field has the indicated descriptor.
- Checks that the currently executing method has access to the referenced method or field.
LinkageErrorto be thrown. A Java virtual machine implementation is allowed to perform any or all of the Pass 4 steps as part of Pass 3; see 2.17.1, "Virtual Machine Start-up" for an example and more discussion.
In one of Sun's Java virtual machine implementations, after the verification has been performed, the instruction in the Java virtual machine code is replaced with an alternative form of the instruction. This alternative instruction indicates that the verification needed by this instruction has taken place and does not need to be performed again. Subsequent invocations of the method will thus be faster. It is illegal for these alternative instruction forms to appear inclassfiles, and they should never be encountered by the verifier.
4.9.2 The Bytecode Verifier
As indicated earlier, Pass 3 of the verification process is the most complex of the four passes ofclassfile verification. This section looks at the verification of Java virtual machine code in Pass 3 in more detail. The code for each method is verified independently. First, the bytes that make up the code are broken up into a sequence of instructions, and the index into thecodearray of the start of each instruction is placed in an array. The verifier then goes through the code a second time and parses the instructions. During this pass a data structure is built to hold information about each Java virtual machine instruction in the method. The operands, if any, of each instruction are checked to make sure they are valid. For instance:
- Branches must be within the bounds of the
codearray for the method. - The targets of all control-flow instructions are each the start of an instruction. In the case of a wide instruction, the wide opcode is considered the start of the instruction, and the opcode giving the operation modified by that wide instruction is not considered to start an instruction. Branches into the middle of an instruction are disallowed.
- No instruction can access or modify a local variable at an index greater than or equal to the number of local variables that its method indicates it allocates.
- All references to the constant pool must be to an entry of the appropriate type. For example: the instruction ldc can be used only for data of type
intorfloator for instances of classString; the instruction getfield must reference a field. - The code does not end in the middle of an instruction.
- Execution cannot fall off the end of the code.
- For each exception handler, the starting and ending point of code protected by the handler must be at the beginning of an instruction or, in the case of the ending point, immediately past the end of the code. The starting point must be before the ending point. The exception handler code must start at a valid instruction, and it may not start at an opcode being modified by the wide instruction.
byte,short,char) when determining the value types on the operand stack. Next, a data-flow analyzer is initialized. For the first instruction of the method, the local variables that represent parameters initially contain values of the types indicated by the method's type descriptor; the operand stack is empty. All other local variables contain an illegal value. For the other instructions, which have not been examined yet, no information is available regarding the operand stack or local variables.
Finally, the data-flow analyzer is run. For each instruction, a "changed" bit indicates whether this instruction needs to be looked at. Initially, the "changed" bit is set only for the first instruction. The data-flow analyzer executes the following loop:
- Select a virtual machine instruction whose "changed" bit is set. If no instruction remains whose "changed" bit is set, the method has successfully been verified. Otherwise, turn off the "changed" bit of the selected instruction.
- Model the effect of the instruction on the operand stack and local variable array by doing the following:
- If the instruction uses values from the operand stack, ensure that there are a sufficient number of values on the stack and that the top values on the stack are of an appropriate type. Otherwise, verification fails.
- If the instruction uses a local variable, ensure that the specified local variable contains a value of the appropriate type. Otherwise, verification fails.
- If the instruction pushes values onto the operand stack, ensure that there is sufficient room on the operand stack for the new values. Add the indicated types to the top of the modeled operand stack.
- If the instruction modifies a local variable, record that the local variable now contains the new type.
- Determine the instructions that can follow the current instruction. Successor instructions can be one of the following:
- The next instruction, if the current instruction is not an unconditional control transfer instruction (for instance goto, return, or athrow). Verification fails if it is possible to "fall off" the last instruction of the method.
- The target(s) of a conditional or unconditional branch or switch.
- Any exception handlers for this instruction.
- Merge the state of the operand stack and local variable array at the end of the execution of the current instruction into each of the successor instructions. In the special case of control transfer to an exception handler, the operand stack is set to contain a single object of the exception type indicated by the exception handler information.
- If this is the first time the successor instruction has been visited, record that the operand stack and local variable values calculated in steps 2 and 3 are the state of the operand stack and local variable array prior to executing the successor instruction. Set the "changed" bit for the successor instruction.
- If the successor instruction has been seen before, merge the operand stack and local variable values calculated in steps 2 and 3 into the values already there. Set the "changed" bit if there is any modification to the values.
- Continue at step 1.
referencevalues may appear at corresponding places on the two stacks. In this case, the merged operand stack contains areferenceto an instance of the first common superclass of the two types. Such a reference type always exists because the typeObjectis a superclass of all class and interface types. If the operand stacks cannot be merged, verification of the method fails. To merge two local variable array states, corresponding pairs of local variables are compared. If the two types are not identical, then unless both containreferencevalues, the verifier records that the local variable contains an unusable value. If both of the pair of local variables containreferencevalues, the merged state contains areferenceto an instance of the first common superclass of the two types.
If the data-flow analyzer runs on a method without reporting a verification failure, then the method has been successfully verified by Pass 3 of theclassfile verifier.
Certain instructions and data types complicate the data-flow analyzer. We now examine each of these in more detail.
4.9.3 Values of Types
Values of thelonganddoublelonganddoubletypes are treated specially by the verification process. Whenever a value of typelongordoubleis moved into a local variable at indexn, indexn+1is specially marked to indicate that it has been reserved by the value at indexnand may not be used as a local variable index. Any value previously at indexn+1becomes unusable.
Whenever a value is moved to a local variable at indexn, the indexn-1is examined to see if it is the index of a value of typelongordouble. If so, the local variable at indexn-1is changed to indicate that it now contains an unusable value. Since the local variable at indexnhas been overwritten, the local variable at indexn-1cannot represent a value of typelongordouble.
Dealing with values of typeslongordoubleon the operand stack is simpler; the verifier treats them as single values on the stack. For example, the verification code for the dadd opcode (add twodoublevalues) checks that the top two items on the stack are both of typedouble. When calculating operand stack length, values of typelonganddoublehave length two.
Untyped instructions that manipulate the operand stack must treat values of typedoubleandlongas atomic (indivisible). For example, the verifier reports a failure if the top value on the stack is adoubleand it encounters an instruction such as pop or dup. The instructions pop2 or dup2 must be used instead.
4.9.4 Instance Initialization Methods and Newly Created Objects
Creating a new class instance is a multistep process. The statement
can be implemented by the following:...new myClass(i, j, k);...
... new #1 // Allocate uninitialized space for
This instruction sequence leaves the newly created and initialized object on top of the operand stack. (Additional examples of compilation to the instruction set of the Java virtual machine are given in Chapter 7, "Compiling for the Java Virtual Machine.") The instance initialization method (§3.9) for classmyClassdup // Duplicate object on the operand stack iload_1 // Push i iload_2 // Push j iload_3 // Push k invokespecial #5 // InvokemyClass.<init>...myClasssees the new uninitialized object as itsthisargument in local variable0. Before that method invokes another instance initialization method ofmyClassor its direct superclass onthis, the only operation the method can perform onthisis assigning fields declared withinmyClass.
When doing dataflow analysis on instance methods, the verifier initializes local variable0to contain an object of the current class, or, for instance initialization methods, local variable0contains a special type indicating an uninitialized object. After an appropriate instance initialization method is invoked (from the current class or the current superclass) on this object, all occurrences of this special type on the verifier's model of the operand stack and in the local variable array are replaced by the current class type. The verifier rejects code that uses the new object before it has been initialized or that initializes the object more than once. In addition, it ensures that every normal return of the method has invoked an instance initialization method either in the class of this method or in the direct superclass.
Similarly, a special type is created and pushed on the verifier's model of the operand stack as the result of the Java virtual machine instruction new. The special type indicates the instruction by which the class instance was created and the type of the uninitialized class instance created. When an instance initialization method is invoked on that class instance, all occurrences of the special type are replaced by the intended type of the class instance. This change in type may propagate to subsequent instructions as the dataflow analysis proceeds.
The instruction number needs to be stored as part of the special type, as there may be multiple not-yet-initialized instances of a class in existence on the operand stack at one time. For example, the Java virtual machine instruction sequence that implements
may have two uninitialized instances ofnew InputStream(new Foo(), new InputStream("foo"))InputStreamon the operand stack at once. When an instance initialization method is invoked on a class instance, only those occurrences of the special type on the operand stack or in the local variable array that are the same object as the class instance are replaced.
A valid instruction sequence must not have an uninitialized object on the operand stack or in a local variable during a backwards branch, or in a local variable in code protected by an exception handler or afinallyclause. Otherwise, a devious piece of code might fool the verifier into thinking it had initialized a class instance when it had, in fact, initialized a class instance created in a previous pass through a loop.
4.9.5 Exception Handlers
Java virtual machine code produced by Sun's compiler for the Java programming language always generates exception handlers such that:
- Either the ranges of instructions protected by two different exception handlers always are completely disjoint, or else one is a subrange of the other. There is never a partial overlap of ranges.
- The handler for an exception will never be inside the code that is being protected.
- The only entry to an exception handler is through an exception. It is impossible to fall through or "goto" the exception handler.
classfile verifier since they do not pose a threat to the integrity of the Java virtual machine. As long as every nonexceptional path to the exception handler causes there to be a single object on the operand stack, and as long as all other criteria of the verifier are met, the verifier will pass the code.
4.9.6 Exceptions and
Given the code fragmentfinally
the Java programming language guarantees that...try {startFaucet();waterLawn();} finally {stopFaucet();}...stopFaucetis invoked (the faucet is turned off) whether we finish watering the lawn or whether an exception occurs while starting the faucet or watering the lawn. That is, thefinallyclause is guaranteed to be executed whether itstryclause completes normally or completes abruptly by throwing an exception. To implement thetry-finallyconstruct, Sun's compiler for the Java programming language uses the exception-handling facilities together with two special instructions: jsr ("jump to subroutine") and ret ("return from subroutine"). Thefinallyclause is compiled as a subroutine within the Java virtual machine code for its method, much like the code for an exception handler. When a jsr instruction that invokes the subroutine is executed, it pushes its return address, the address of the instruction after the jsr that is being executed, onto the operand stack as a value of typereturnAddress. The code for the subroutine stores the return address in a local variable. At the end of the subroutine, a ret instruction fetches the return address from the local variable and transfers control to the instruction at the return address.
Control can be transferred to thefinallyclause (thefinallysubroutine can be invoked) in several different ways. If thetryclause completes normally, thefinallysubroutine is invoked via a jsr instruction before evaluating the next expression. Abreakorcontinueinside thetryclause that transfers control outside thetryclause executes a jsr to the code for thefinallyclause first. If thetryclause executes areturn, the compiled code does the following:
- Saves the return value (if any) in a local variable.
- Executes a jsr to the code for the
finallyclause. - Upon return from the
finallyclause, returns the value saved in the local variable.
tryclause. If an exception is thrown in thetryclause, this exception handler does the following:
- Saves the exception in a local variable.
- Executes a jsr to the
finallyclause. - Upon return from the
finallyclause, rethrows the exception.
try-finallyconstruct, see Section 7.13, "Compilingfinally." The code for thefinallyclause presents a special problem to the verifier. Usually, if a particular instruction can be reached via multiple paths and a particular local variable contains incompatible values through those multiple paths, then the local variable becomes unusable. However, afinallyclause might be called from several different places, yielding several different circumstances:
- The invocation from the exception handler may have a certain local variable that contains an exception.
- The invocation to implement
returnmay have some local variable that contains the return value. - The invocation from the bottom of the
tryclause may have an indeterminate value in that same local variable.
finallyclause itself might pass verification, but after completing the updating all the successors of the ret instruction, the verifier would note that the local variable that the exception handler expects to hold an exception, or that the return code expects to hold a return value, now contains an indeterminate value. Verifying code that contains afinallyclause is complicated. The basic idea is the following:
- Each instruction keeps track of the list of jsr targets needed to reach that instruction. For most code, this list is empty. For instructions inside code for the
finallyclause, it is of length one. For multiply nestedfinallycode (extremely rare!), it may be longer than one. - For each instruction and each jsr needed to reach that instruction, a bit vector is maintained of all local variables accessed or modified since the execution of the jsr instruction.
- When executing the ret instruction, which implements a return from a subroutine, there must be only one possible subroutine from which the instruction can be returning. Two different subroutines cannot "merge" their execution to a single ret instruction.
- To perform the data-flow analysis on a ret instruction, a special procedure is used. Since the verifier knows the subroutine from which the instruction must be returning, it can find all the jsr instructions that call the subroutine and merge the state of the operand stack and local variable array at the time of the ret instruction into the operand stack and local variable array of the instructions following the jsr. Merging uses a special set of values for local variables:
- For any local variable that the bit vector (constructed above) indicates has been accessed or modified by the subroutine, use the type of the local variable at the time of the ret.
- For other local variables, use the type of the local variable before the jsr instruction.
4.10 Limitations of the Java Virtual Machine
The following limitations of the Java virtual machine are implicit in theclassfile format:
- The per-class or per-interface constant pool is limited to 65535 entries by the 16-bit
constant_pool_countfield of theClassFilestructure (§4.1). This acts as an internal limit on the total complexity of a single class or interface. - The amount of code per non-
native, non-abstractmethod is limited to 65536 bytes by the sizes of the indices in theexception_tableof theCodeattribute (§4.7.3), in theLineNumberTableattribute (§4.7.8), and in theLocalVariableTableattribute (§4.7.9). - The greatest number of local variables in the local variables array of a frame created upon invocation of a method is limited to 65535 by the size of the
max_localsitem of theCodeattribute (§4.7.3) giving the code of the method. Note that values of typelonganddoubleare each considered to reserve two local variables and contribute two units toward themax_localsvalue, so use of local variables of those types further reduces this limit. - The number of fields that may be declared by a class or interface is limited to 65535 by the size of the
fields_countitem of theClassFilestructure (§4.1). Note that the value of thefields_countitem of theClassFilestructure does not include fields that are inherited from superclasses or superinterfaces. - The number of methods that may be declared by a class or interface is limited to 65535 by the size of the
methods_countitem of theClassFilestructure (§4.1). Note that the value of themethods_countitem of theClassFilestructure does not include methods that are inherited from superclasses or superinterfaces. - The number of direct superinterfaces of a class or interface is limited to 65535 by the size of the
interfaces_countitem of theClassFilestructure (§4.1). - The size of an operand stack in a frame (§3.6) is limited to 65535 values by the
max_stackfield of theCode_attributestructure (§4.7.3). Note that values of typelonganddoubleare each considered to contribute two units toward themax_stackvalue, so use of values of these types on the operand stack further reduces this limit. - The number of local variables in a frame (§3.6) is limited to 65535 by the
max_localsfield of theCode_attributestructure (§4.7.3) and the 16-bit local variable indexing of the Java virtual machine instruction set. - The number of dimensions in an array is limited to 255 by the size of the dimensions opcode of the multianewarray instruction and by the constraints imposed on the multianewarray, anewarray, and newarray instructions by §4.8.2.
- The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for
thisin the case of instance or interface method invocations. Note that a method descriptor is defined in terms of a notion of method parameter length in which a parameter of typelongordoublecontributes two units to the length, so parameters of these types further reduce the limit. - The length of field and method names, field and method descriptors, and other constant string values is limited to 65535 characters by the 16-bit unsigned
lengthitem of theCONSTANT_Utf8_infostructure (§4.4.7). Note that the limit is on the number of bytes in the encoding and not on the number of encoded characters. UTF-8 encodes some characters using two or three bytes. Thus, strings incorporating multibyte characters are further constrained.
1 The Java virtual machine implementation of Sun's JDK release 1.0.2 supportsclassfile format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can supportclassfile formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can supportclassfile formats of versions in the range 45.0 through 46.0 inclusive. 2 In retrospect, making 8-byte constants take two constant pool entries was a poor choice.
3 The first edition of The Java Language Specification required that "com" be in uppercase in this example. The second edition will reverse that convention and use lowercase.
4 The fact thatend_pcis exclusive is a historical mistake in the design of the Java virtual machine: if the Java virtual machine code for a method is exactly 65535 bytes long and ends with an instruction that is 1 byte long, then that instruction cannot be protected by an exception handler. A compiler writer can work around this bug by limiting the maximum size of the generated Java virtual machine code for any method, instance initialization method, or static initializer (the size of anycodearray) to 65534 bytes.
5 TheInnerClassesattribute was introduced in JDK release 1.1 to support nested classes and interfaces.
6 TheSyntheticattribute was introduced in JDK release 1.1 to support nested classes and interfaces.
7 TheDeprecatedattribute was introduced in JDK release 1.1 to support the@deprecatedtag in documentation comments.
Contents | Prev | Next | Index
The Java Virtual Machine Specification
Copyright © 1999 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections through our feedback form - If bits is
No comments:
Post a Comment