Movatterモバイル変換


[0]ホーム

URL:


Contents |Prev |Next |IndexThe Java Virtual Machine Specification


CHAPTER 4

The File Format


This chapter describes the Java virtual machineclass file format. Eachclass 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 theclass file format.

Aclass 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 interfacesjava.io.DataInput andjava.io.DataOutput and classes such asjava.io.DataInputStream andjava.io.DataOutputStream.

This chapter defines its own set of data types representingclass file data: The typesu1,u2, andu4 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 asreadUnsignedByte,readUnsignedShort, andreadInt of the interfacejava.io.DataInput.

This chapter presents theclass 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 theclass file format are referred to asitems. Successive items are stored in theclass file sequentially, without padding or alignment.

Tables, consisting of zero or more variable-sized items, are used in severalclass 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 TheClassFile Structure

Aclass file consists of a singleClassFile structure:


    ClassFile {    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];    }
The items in theClassFile structure are as follows:

magic
Themagic item supplies the magic number identifying theclass file format; it has the value0xCAFEBABE.

minor_version,major_version
The values of theminor_version andmajor_version items are the minor and major version numbers of thisclass file.Together, a major and a minor version number determine the version of theclass file format. If aclass file has major version number M and minor version number m, we denote the version of itsclass file format as M.m. Thus,class file format versions may be ordered lexicographically, for example, 1.5< 2.0< 2.1.

A Java virtual machine implementation can support aclass file 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 theconstant_pool_count item is equal to the number of entries in theconstant_pool table plus one. Aconstant_pool index is considered valid if it is greater than zero and less thanconstant_pool_count, with the exception for constants of typelong anddouble noted in§4.4.5.

constant_pool[]
Theconstant_pool is a table of structures(§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within theClassFile structure and its substructures. The format of eachconstant_pool table entry is indicated by its first "tag" byte.

Theconstant_pool table is indexed from1 toconstant_pool_count-1.

access_flags
The value of theaccess_flags item 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 inTable 4.1.

Flag NameValueInterpretation
ACC_PUBLIC0x0001Declaredpublic; may be accessed from outside its package.
ACC_FINAL0x0010Declaredfinal; no subclasses allowed.
ACC_SUPER0x0020Treat superclass methods specially when invoked by theinvokespecial instruction.
ACC_INTERFACE0x0200Is an interface, not a class.
ACC_ABSTRACT0x0400Declaredabstract; may not be instantiated.


An interface is distinguished by itsACC_INTERFACE flag being set. If itsACC_INTERFACE flag is not set, thisclass file defines a class, not an interface.

If theACC_INTERFACE flag of thisclass file is set, itsACC_ABSTRACT flag must also be set(§2.13.1) and itsACC_PUBLIC flag may be set. Such aclass file may not have any of the other flags inTable 4.1 set.

If theACC_INTERFACE flag of thisclass file is not set, it may have any of the other flags inTable 4.1 set. However, such aclass file cannot have both itsACC_FINAL andACC_ABSTRACT flags set(§2.8.2).

The setting of theACC_SUPER flag indicates which of two alternative semantics for itsinvokespecial instruction the Java virtual machine is to express; theACC_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 forinvokespecial documented in this specification. All new compilers to the instruction set of the Java virtual machine should set theACC_SUPER flag. Sun's older compilers generatedClassFile flags withACC_SUPER unset. Sun's older Java virtual machine implementations ignore the flag if it is set.

All bits of theaccess_flags item not assigned inTable 4.1 are reserved for future use. They should be set to zero in generatedclass files and should be ignored by Java virtual machine implementations.

this_class
The value of thethis_class item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the class or interface defined by thisclass file.

super_class
For a class, the value of thesuper_class item either must be zero or must be a valid index into theconstant_pool table. If the value of thesuper_class item is nonzero, theconstant_pool entry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing the direct superclass of the class defined by thisclass file. Neither the direct superclass nor any of its superclasses may be afinal class.

If the value of thesuper_class item is zero, then thisclass file must represent the classObject, the only class or interface without a direct superclass.

For an interface, the value of thesuper_class item must always be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Class_info structure representing the classObject.

interfaces_count
The value of theinterfaces_count item gives the number of direct superinterfaces of this class or interface type.

interfaces[]
Each value in theinterfaces array must be a valid index into theconstant_pool table. Theconstant_pool entry 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 thefields_count item gives the number offield_info structures in thefields table. 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 thefields table must be afield_info(§4.5) structure giving a complete description of a field in this class or interface. Thefields table 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 themethods_count item gives the number ofmethod_info structures in themethods table.

methods[]
Each value in themethods table must be amethod_info(§4.6) structure giving a complete description of a method in this class or interface. If the method is notnative orabstract, the Java virtual machine instructions implementing the method are also supplied.

Themethod_info structures 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). Themethods table does not include items representing methods that are inherited from superclasses or superinterfaces.

attributes_count
The value of theattributes_count item gives the number of attributes(§4.7) in theattributes table of this class.

attributes[]
Each value of theattributes table must be an attribute structure(§4.7).

The only attributes defined by this specification as appearing in theattributes table of aClassFile structure are theSourceFile attribute(§4.7.7) and theDeprecated(§4.7.10) attribute.

A Java virtual machine implementation is required to silently ignore any or all attributes in theattributes table of aClassFile structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclass file, 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 representedin a fully qualified form(§2.7.5). Such names are always represented asCONSTANT_Utf8_info(§4.4.7) structures and thus may be drawn, where not furtherconstrained, from the entire Unicode character set. Class names and interfacesare referenced both from thoseCONSTANT_NameAndType_info(§4.4.6) structures that have such names as part of their descriptor(§4.3) and from allCONSTANT_Class_info(§4.4.1) structures.

For historical reasons the syntax of fully qualified class and interface names that appear inclass 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 classThread isjava.lang.Thread. In the form used in descriptors in theclass file format, a reference to the name of classThread is implemented using aCONSTANT_Utf8_info structure representing the string"java/lang/Thread".


4.3 Descriptors

Adescriptor 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 initalic 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 succeedinglines. For example, the production:

FieldType:
     BaseType
     ObjectType
     ArrayType

states that aFieldType may represent either aBaseType, anObjectType, or anArrayType.

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 aMethodDescriptor represents a left parenthesis, followed by zero or moreParameterDescriptor values, followed by a right parenthesis, followed by aReturnDescriptor.

4.3.2 Field Descriptors

Afield descriptor represents the type of a class, instance, or local variable. It is a series of characters generated by the grammar:

       FieldDescriptor:

FieldType

       ComponentType:

FieldType

       FieldType:

BaseType

ObjectType

ArrayType

       BaseType:

B

C

D

F

I

J

S

Z

       ObjectType:
L <classname> ;

       ArrayType:
[ComponentType

The characters ofBaseType, the L and ; ofObjectType, and the [ ofArrayType 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 inTable 4.2.

BaseType CharacterTypeInterpretation
Bbytesigned byte
CcharUnicode character
Ddoubledouble-precision floating-point value
Ffloatsingle-precision floating-point value
Iintinteger
Jlonglong integer
L<classname>;referencean instance of class<classname>
Sshortsigned short
Zbooleantrue orfalse
[ referenceone array dimension


For example, the descriptor of an instance variable of typeint is simply I. The descriptor of an instance variable of typeObject is Ljava/lang/Object;. Note that the internal form of the fully qualified name for classObject is used. The descriptor of an instance variable that is a multidimensionaldouble array,

    double d[][][];
is

    [[[D

4.3.3 Method Descriptors

Amethod descriptor represents the parameters that the method takes and the value that it returns:

MethodDescriptor:
    (ParameterDescriptor* )ReturnDescriptor

Aparameter descriptorrepresents a parameter passed to a method:

ParameterDescriptor:
    FieldType

Areturn descriptorrepresents the type of the value returned from a method. It is a series of characters generated by the grammar:

ReturnDescriptor:
    FieldType
    V

The character V indicates that the method returns no value (its return type isvoid).

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 forthis 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 typelong ordouble 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 ofThread andObject are used in the method descriptor.

The method descriptor formymethod is the same whethermymethod is a class or an instance method. Although an instance method is passedthis, a reference to the current class instance, in addition to its intended parameters, that fact is not reflected in the method descriptor. (A reference tothis is not passed to a class method.) The reference tothis 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 informationin theconstant_pool table.

Allconstant_pool table entries have the following general format:


    cp_info {    u1 tag;    u1 info[];    }
Each item in theconstant_pool table must begin with a 1-byte tag indicating the kind ofcp_info entry. The contents of theinfo array vary with the value oftag. The valid tags and their values are listed inTable 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The formatof the additional information varies with the tag value.

Constant TypeValue
CONSTANT_Class7
CONSTANT_Fieldref9
CONSTANT_Methodref10
CONSTANT_InterfaceMethodref11
CONSTANT_String8
CONSTANT_Integer3
CONSTANT_Float4
CONSTANT_Long5
CONSTANT_Double6
CONSTANT_NameAndType12
CONSTANT_Utf81


4.4.1 TheCONSTANT_Class_info Structure

TheCONSTANT_Class_info structure is used to represent a class or an interface:


    CONSTANT_Class_info {    u1 tag;    u2 name_index;    }
The items of theCONSTANT_Class_info structure are the following:

tag
Thetag item has the valueCONSTANT_Class (7).

name_index
The value of thename_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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).

Because arrays are objects, the opcodesanewarray andmultianewarray can reference array "classes" viaCONSTANT_Class_info(§4.4.1) structures in theconstant_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-dimensionalint array type

    int[][]
is

    [[I
The class name representing the type array of classThread

    Thread[]
is

    [Ljava/lang/Thread;
An array type descriptor is valid only if it represents 255 or fewer dimensions.

4.4.2 TheCONSTANT_Fieldref_info,CONSTANT_Methodref_info, andCONSTANT_InterfaceMethodref_info Structures

Fields, methods, and interface methods are represented by similar structures:


    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;    }
The items of these structures are as follows:

tag
Thetag item of aCONSTANT_Fieldref_info structure has the valueCONSTANT_Fieldref (9).

Thetag item of aCONSTANT_Methodref_info structure has the valueCONSTANT_Methodref (10).

Thetag item of aCONSTANT_InterfaceMethodref_info structure has the valueCONSTANT_InterfaceMethodref (11).

class_index
The value of theclass_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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_index item of aCONSTANT_Methodref_info structure must be a class type, not an interface type. Theclass_index item of aCONSTANT_InterfaceMethodref_info structure must be an interface type. Theclass_index item of aCONSTANT_Fieldref_info structure may be either a class type or an interface type.

name_and_type_index
The value of thename_and_type_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_NameAndType_info(§4.4.6) structure. Thisconstant_pool entry indicates the name and descriptor of the field or method. In aCONSTANT_Fieldref_info the 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_info structure 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 TheCONSTANT_String_info Structure

TheCONSTANT_String_info structure is used to represent constant objects of the typeString:


    CONSTANT_String_info {    u1 tag;    u2 string_index;    }
The items of theCONSTANT_String_info structure are as follows:

tag
Thetag item of theCONSTANT_String_info structure has the valueCONSTANT_String (8).

string_index
The value of thestring_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the sequence of characters to which theString object is to be initialized.

4.4.4 TheCONSTANT_Integer_info andCONSTANT_Float_info Structures

TheCONSTANT_Integer_info andCONSTANT_Float_info structures represent 4-byte numeric (int andfloat) constants:


    CONSTANT_Integer_info {    u1 tag;    u4 bytes;    }

    CONSTANT_Float_info {    u1 tag;    u4 bytes;    }
The items of these structures are as follows:

tag
Thetag item of theCONSTANT_Integer_info structure has the valueCONSTANT_Integer (3).

Thetag item of theCONSTANT_Float_info structure has the valueCONSTANT_Float (4).

bytes
Thebytes item of theCONSTANT_Integer_info structure represents the value of theint constant. The bytes of the value are stored in big-endian (high byte first) order.

Thebytes item of theCONSTANT_Float_info structure represents the value of thefloat constant 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_info structure is determined as follows. The bytes of the value are first converted into anint constant bits. Then:

    int s = ((bits >> 31) == 0) ? 1 : -1;    int e = ((bits >> 23) & 0xff);    int m = (e == 0) ?    (bits & 0x7fffff) << 1 :    (bits & 0x7fffff) | 0x800000;Then thefloat value equals the result of the mathematical expressions·m·2e-150.

4.4.5 TheCONSTANT_Long_info andCONSTANT_Double_info Structures

TheCONSTANT_Long_info andCONSTANT_Double_info represent 8-byte numeric (long anddouble) constants:


    CONSTANT_Long_info {    u1 tag;    u4 high_bytes;    u4 low_bytes;    }

    CONSTANT_Double_info {    u1 tag;    u4 high_bytes;    u4 low_bytes;    }
All 8-byte constants take up two entries in theconstant_pool table of theclass file. If aCONSTANT_Long_info orCONSTANT_Double_info structure is the item in theconstant_pool table at indexn, then the next usable item in the pool is located at indexn+2. Theconstant_pool indexn+1 must be valid but is considered unusable.2

The items of these structures are as follows:

tag
Thetag item of theCONSTANT_Long_info structure has the valueCONSTANT_Long (5).

Thetag item of theCONSTANT_Double_info structure has the valueCONSTANT_Double (6).

high_bytes,low_bytes
The unsignedhigh_bytes andlow_bytes items of theCONSTANT_Long_info structure together represent the value of thelong constant ((long)high_bytes<< 32) +low_bytes, where the bytes of each ofhigh_bytes andlow_bytes are stored in big-endian (high byte first) order.

Thehigh_bytes andlow_bytes items of theCONSTANT_Double_info structure together represent thedouble value 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_info structure is determined as follows. Thehigh_bytes andlow_bytes items are first converted into thelong constant bits, which is equal to ((long)high_bytes<< 32) +low_bytes. Then:

  • If bits is0x7ff0000000000000L, thedouble value will be positive infinity.
  • If bits is0xfff0000000000000L, thedouble value will be negative infinity.
  • If bits is in the range0x7ff0000000000001L through0x7fffffffffffffffL or in the range0xfff0000000000001L through0xffffffffffffffffL, thedouble value will be NaN.
  • In all other cases, lets,e, andm be 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 thedouble value of the mathematical expressions·m·2e-1075.

4.4.6 TheCONSTANT_NameAndType_info Structure

TheCONSTANT_NameAndType_info structure is used to represent a field or method, without indicating which class or interface type it belongs to:


    CONSTANT_NameAndType_info {    u1 tag;    u2 name_index;    u2 descriptor_index;    }
The items of theCONSTANT_NameAndType_info structure are as follows:

tag
Thetag item of theCONSTANT_NameAndType_info structure has the valueCONSTANT_NameAndType (12).

name_index
The value of thename_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 thedescriptor_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 TheCONSTANT_Utf8_info Structure

TheCONSTANT_Utf8_info structure 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 representedby 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 theclass file 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)0 is 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, seeFile 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_info structure is


    CONSTANT_Utf8_info {    u1 tag;    u2 length;    u1 bytes[length];    }
The items of theCONSTANT_Utf8_info structure are the following:

tag
Thetag item of theCONSTANT_Utf8_info structure has the valueCONSTANT_Utf8 (1).

length
The value of thelength item gives the number of bytes in thebytes array (not the length of the resulting string). The strings in theCONSTANT_Utf8_info structure are not null-terminated.

bytes[]
Thebytes array contains the bytes of the string. No byte may have the value(byte)0 or lie in the range(byte)0xf0-(byte)0xff.


4.5 Fields

Each field is described by afield_info structure. No two fields in oneclass file may have the same name and descriptor(§4.3.2). The format of this structure is


    field_info {    u2 access_flags;    u2 name_index;    u2 descriptor_index;    u2 attributes_count;    attribute_info attributes[attributes_count];    }
The items of thefield_info structure are as follows:

access_flags
The value of theaccess_flags item 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 inTable 4.4.

Fields of classes may set any of the flags inTable 4.4. However, a specific field of a class may have at most one of itsACC_PRIVATE,ACC_PROTECTED, andACC_PUBLIC flags set(§2.7.4) and may not have both itsACC_FINAL andACC_VOLATILE flags set(§2.9.1).

Flag NameValueInterpretation
ACC_PUBLIC0x0001Declaredpublic; may be accessed from outside its package.
ACC_PRIVATE0x0002Declaredprivate; usable only within the defining class.
ACC_PROTECTED0x0004Declaredprotected; may be accessed within subclasses.
ACC_STATIC0x0008Declaredstatic.
ACC_FINAL0x0010Declaredfinal; no further assignment after initialization.
ACC_VOLATILE0x0040Declaredvolatile; cannot be cached.
ACC_TRANSIENT0x0080Declaredtransient; not written or read by a persistent object manager.


Fields of classes may set any of the flags inTable 4.4. However, a specific field of a class may have at most one of itsACC_PRIVATE,ACC_PROTECTED, andACC_PUBLIC flags set(§2.7.4) and may not have both itsACC_FINAL andACC_VOLATILE flags set(§2.9.1).

All fields of interfaces must have theirACC_PUBLIC,ACC_STATIC, andACC_FINAL flags set and may not have any of the other flags inTable 4.4 set(§2.13.3.1).

All bits of theaccess_flags item not assigned inTable 4.4 are reserved for future use. They should be set to zero in generatedclass files and should be ignored by Java virtual machine implementations.

name_index
The value of thename_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 thedescriptor_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 theattributes_count item indicates the number of additional attributes(§4.7) of this field.

attributes[]
Each value of theattributes table 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 theattributes table of afield_info structure 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 theattributes table of afield_info structure. A Java virtual machine implementation is required to silently ignore any or all other attributes in theattributes table that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclass file, 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_info structure. No two methods in oneclass file may have the same name and descriptor(§4.3.3).

The structure has the following format:


    method_info {    u2 access_flags;    u2 name_index;    u2 descriptor_index;    u2 attributes_count;    attribute_info attributes[attributes_count];    }
The items of themethod_info structure are as follows:

access_flags
The value of theaccess_flags item 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 inTable 4.5.

Flag NameValueInterpretation
ACC_PUBLIC0x0001Declaredpublic; may be accessed from outside its package.
ACC_PRIVATE0x0002Declaredprivate; accessible only within the defining class.
ACC_PROTECTED0x0004Declaredprotected; may be accessed within subclasses.
ACC_STATIC0x0008Declaredstatic.
ACC_FINAL0x0010Declaredfinal; may not be overridden.
ACC_SYNCHRONIZED0x0020Declaredsynchronized; invocation is wrapped in a monitor lock.
ACC_NATIVE0x0100Declarednative; implemented in a language other than Java.
ACC_ABSTRACT0x0400Declaredabstract; no implementation is provided.
ACC_STRICT0x0800Declaredstrictfp; floating-point mode is FP-strict


Methods of classes may set any of the flags inTable 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_ABSTRACT flag set it may not have any of itsACC_FINAL,ACC_NATIVE,ACC_PRIVATE,ACC_STATIC,ACC_STRICT, orACC_SYNCHRONIZED flags set(§2.13.3.2).

All interface methods must have theirACC_ABSTRACT andACC_PUBLIC flags set and may not have any of the other flags inTable 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_PUBLIC flags set and may also have itsACC_STRICT flag set, but may not have any of the other flags inTable 4.5 set.

Class and interface initialization methods(§3.9) are called implicitly by the Java virtual machine; the value of theiraccess_flags item is ignored except for the settings of theACC_STRICTflag.

All bits of theaccess_flags item not assigned inTable 4.5 are reserved for future use. They should be set to zero in generatedclass files and should be ignored by Java virtual machine implementations.

name_index
The value of thename_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 thedescriptor_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 theattributes_count item indicates the number of additional attributes(§4.7) of this method.

attributes[]
Each value of theattributes table 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 theattributes table of amethod_info structure 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 theattributes table of amethod_info structure. A Java virtual machine implementation is required to silently ignore any or all other attributes in theattributes table of amethod_info structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of theclass file, 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 theclass file format. All attributes have the following general format:


    attribute_info {    u2 attribute_name_index;    u4 attribute_length;    u1 info[attribute_length];    }
For all attributes, theattribute_name_index must be a valid unsigned 16-bit index into the constant pool of the class. Theconstant_pool entry atattribute_name_index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the name of the attribute. The value of theattribute_length item indicates the length of the subsequent information in bytes. The length does not include the initial six bytes that contain theattribute_name_index andattribute_length items.

Certain attributes are predefined as part of theclass file 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 theattributes tables of theclass file structures in which they appear, the names of these predefined attributes are reserved.

Of the predefined attributes, theCode,ConstantValue, andExceptions attributes must be recognized and correctly read by aclass file reader for correct interpretation of theclass file by a Java virtual machine implementation. TheInnerClasses andSynthetic attributes must be recognized and correctly read by aclass file reader in order to properly implement the Java and Java 2 platform class libraries(§3.12). Use of the remaining predefined attributes is optional; aclass file 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 emitclass files containing new attributes in theattributes tables ofclass file structures. Java virtual machine implementationsare permitted to recognize and use new attributes found in theattributes tables ofclass file 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,class files 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 theclass files contain.

Java virtual machine implementations are specifically prohibited from throwing an exception or otherwise refusing to useclass files simply because of the presence of some new attribute. Of course, tools operating onclass files may not run correctly if givenclass files 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 byThe 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 thisclass file specification.

4.7.2 TheConstantValue Attribute

TheConstantValue attribute is a fixed-length attribute used in theattributes table of thefield_info(§4.5) structures. AConstantValue attribute represents the value of a constant field that must be (explicitly or implicitly)static; that is, theACC_STATIC bit (Table 4.4) in theflags item of thefield_info structure must be set. There can be no more than oneConstantValue attribute in theattributes table of a givenfield_info structure. The constant field represented by thefield_info structure is assigned the value referenced by itsConstantValue attribute 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 interfaceinitialization method(§3.9) of that class or interface.

If afield_info structure representing a non-static field has aConstantValue attribute, then that attribute must silently be ignored. Every Java virtual machine implementation must recognizeConstantValue attributes.

TheConstantValue attribute has the following format:


    ConstantValue_attribute {    u2 attribute_name_index;    u4 attribute_length;    u2 constantvalue_index;    }
The items of theConstantValue_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"ConstantValue".

attribute_length
The value of theattribute_length item of aConstantValue_attribute structure must be2.

constantvalue_index
The value of theconstantvalue_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index gives the constant value represented by this attribute. Theconstant_pool entry must be of a type appropriate to the field, as shown byTable 4.6.

Field TypeEntry Type
longCONSTANT_Long
floatCONSTANT_Float
doubleCONSTANT_Double
int,short,char,byte,booleanCONSTANT_Integer
StringCONSTANT_String


4.7.3 TheCode Attribute

TheCode attribute is a variable-length attribute used in theattributes table ofmethod_info structures. ACode attribute 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 recognizeCode attributes. If the method is eithernative orabstract, itsmethod_info structure must not have aCode attribute. Otherwise, itsmethod_info structure must have exactly oneCode attribute.

TheCode attribute has the following format:


    Code_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];    }
The items of theCode_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"Code".

attribute_length
The value of theattribute_length item indicates the length of the attribute, excluding the initial six bytes.

max_stack
The value of themax_stack item 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 themax_locals item 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 typelong ordouble ismax_locals-2. The greatest local variable index for a value of any other type ismax_locals-1.

code_length
The value of thecode_length item gives the number of bytes in thecode array for this method. The value ofcode_length must be greater than zero; thecode array must not be empty.

code[]
Thecode array gives the actual bytes of Java virtual machine code that implement the method.

When thecode array is read into memory on a byte-addressable machine, if the first byte of the array is aligned on a 4-byte boundary, thetableswitch andlookupswitch 32-bit offsets will be 4-byte aligned. (Refer to the descriptions of those instructions for more information on the consequences ofcode array alignment.)

The detailed constraints on the contents of thecode array are extensive and are given in a separate section(§4.8).

exception_table_length
The value of theexception_table_length item gives the number of entries in theexception_table table.

exception_table[]
Each entry in theexception_table array describes one exception handler in thecode array. The order of the handlers in theexception_table array is significant. SeeSection 3.10 for more details.

Eachexception_table entry contains the following four items:

start_pc,end_pc
The values of the two itemsstart_pc andend_pc indicate the ranges in thecode array at which the exception handler is active. The value ofstart_pc must be a valid index into thecode array of the opcode of an instruction. The value ofend_pc either must be a valid index into thecode array of the opcode of an instruction or must be equal tocode_length, the length of thecode array. The value ofstart_pc must be less than the value ofend_pc.

Thestart_pc is inclusive andend_pc is 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 thehandler_pc item indicates the start of the exception handler. The value of the item must be a valid index into thecode array and must be the index of the opcode of an instruction.

catch_type
If the value of thecatch_type item is nonzero, it must be a valid index into theconstant_pool table. Theconstant_pool entry 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 classThrowable or 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_type item is zero, this exception handler is called for all exceptions. This is used to implementfinally (seeSection 7.13, "Compilingfinally").

attributes_count
The value of theattributes_count item indicates the number of attributes of theCode attribute.

attributes[]
Each value of theattributes table must be an attribute structure(§4.7). ACode attribute 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 theCode attribute.

A Java virtual machine implementation is permitted to silently ignore any or all attributes in theattributes table of aCode attribute. Attributes not defined in this specification are not allowed to affect the semantics of theclass file, but only to provide additional descriptive information(§4.7.1).

4.7.4 TheExceptions Attribute

TheExceptions attribute is a variable-length attribute used in theattributes table of amethod_info(§4.6) structure. TheExceptions attribute indicates which checked exceptions a method may throw. There may be at most oneExceptions attribute in eachmethod_info structure.

TheExceptions attribute has the following format:


    Exceptions_attribute {    u2 attribute_name_index;    u4 attribute_length;    u2 number_of_exceptions;    u2 exception_index_table[number_of_exceptions];    }
The items of theExceptions_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must betheCONSTANT_Utf8_info(§4.4.7) structure representing the string"Exceptions".

attribute_length
The value of theattribute_length item indicates the attribute length, excluding the initial six bytes.

number_of_exceptions
The value of thenumber_of_exceptions item indicates the number of entries in theexception_index_table.

exception_index_table[]
Each value in theexception_index_table array must be a valid index into theconstant_pool table. Theconstant_pool entry 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.

A method should throw an exception only if at least one of the following three criteria is met:

These requirements are not enforced in the Java virtual machine; they are enforced only at compile time.

4.7.5 TheInnerClasses Attribute

TheInnerClasses attribute5 is a variable-length attribute in theattributes table 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, itsClassFile structure must have exactly oneInnerClasses attribute in itsattributes table.

TheInnerClasses attribute has the following format:


    InnerClasses_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];    }
The items of theInnerClasses_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"InnerClasses".

attribute_length
The value of theattribute_length item indicates the length of the attribute, excluding the initial six bytes.

number_of_classes
The value of thenumber_of_classes item indicates the number of entries in theclasses array.

classes[]
EveryCONSTANT_Class_info entry in theconstant_pool table which represents a class or interface C that is not a package member must have exactly one corresponding entry in theclasses array.

If a class has members that are classes or interfaces, itsconstant_pool table (and hence itsInnerClasses attribute) 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 haveInnerClasses information for each enclosing class and for each immediate member.

Eachclasses array entry contains the following four items:

inner_class_info_index
The value of theinner_class_info_index item must be zero or a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Class_info(§4.4.1) structure representing C. The remaining items in theclasses array entry give information about C.

outer_class_info_index
If C is not a member, the value of theouter_class_info_index item must be zero. Otherwise, the value of theouter_class_info_index item must be a valid index into theconstant_pool table, 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 theinner_name_index item must be zero. Otherwise, the value of theinner_name_index item must be a valid index into theconstant_pool table, 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 thisclass file was compiled.

inner_class_access_flags
The value of theinner_class_access_flags item 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 thisclass file was compiled. It is used by compilers to recover the original information when source code is not available. The flags are shown inTable 4.7.

Flag NameValueMeaning
ACC_PUBLIC0x0001Marked or implicitlypublic in source.
ACC_PRIVATE0x0002Markedprivate in source.
ACC_PROTECTED0x0004Markedprotected in source.
ACC_STATIC0x0008Marked or implicitlystatic in source.
ACC_FINAL0x0010Markedfinal in source.
ACC_INTERFACE0x0200Was aninterface in source.
ACC_ABSTRACT0x0400Marked or implicitlyabstract in source.


All bits of theinner_class_access_flags item not assigned inTable 4.7 are reserved for future use. They should be set to zero in generatedclass files and should be ignored by Java virtual machine implementations.

The Java virtual machine does not currently check the consistency of theInnerClasses attribute with anyclass file actually representing a class or interface referenced by the attribute.

4.7.6 TheSynthetic Attribute

TheSynthetic attribute6 is a fixed-length attribute in theattributes table 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 aSynthetic attribute.

TheSynthetic attribute has the following format:


    Synthetic_attribute {    u2 attribute_name_index;    u4 attribute_length;    }
The items of theSynthetic_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"Synthetic".

attribute_length
The value of theattribute_length item is zero.

4.7.7 TheSourceFile Attribute

TheSourceFile attribute is an optional fixed-length attribute in theattributes table of theClassFile(§4.1) structure. There can be no more than oneSourceFile attribute in theattributes table of a givenClassFile structure.

TheSourceFile attribute has the following format:


    SourceFile_attribute {    u2 attribute_name_index;    u4 attribute_length;    u2 sourcefile_index;    }
The items of theSourceFile_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"SourceFile".

attribute_length
The value of theattribute_length item of aSourceFile_attribute structure must be2.

sourcefile_index
The value of thesourcefile_index item must be a valid index into theconstant_pool table. The constant pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing a string.

The string referenced by thesourcefile_index item will be interpreted as indicating the name of the source file from which thisclass file 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 TheLineNumberTable Attribute

TheLineNumberTable attribute is an optional variable-length attribute in theattributes table of aCode(§4.7.3) attribute. It may be used by debuggers to determine which part of the Java virtual machinecode array corresponds to a given line number in the original source file. IfLineNumberTable attributes are present in theattributes table of a givenCode attribute, then they may appear in any order. Furthermore, multipleLineNumberTable attributes may together represent a given line of a source file; that is,LineNumberTable attributes need not be one-to-one with source lines.

TheLineNumberTable attribute has the following format:


    LineNumberTable_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];    }
The items of theLineNumberTable_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"LineNumberTable".

attribute_length
The value of theattribute_length item indicates the length of the attribute, excluding the initial six bytes.

line_number_table_length
The value of theline_number_table_length item indicates the number of entries in theline_number_table array.

line_number_table[]
Each entry in theline_number_table array indicates that the line number in the original source file changes at a given point in thecode array. Eachline_number_table entry must contain the following two items:

start_pc
The value of thestart_pc item must indicate the index into thecode array at which the code for a new line in the original source file begins. The value ofstart_pc must be less than the value of thecode_length item of theCode attribute of which thisLineNumberTable is an attribute.

line_number
The value of theline_number item must give the corresponding line number in the original source file.

4.7.9 TheLocalVariableTable Attribute

TheLocalVariableTable attribute 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. IfLocalVariableTable attributes are present in theattributes table of a givenCode attribute, then they may appear in any order. There may be no more than oneLocalVariableTable attribute per local variable in theCode attribute.

TheLocalVariableTable attribute has the following format:


    LocalVariableTable_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];    }
The items of theLocalVariableTable_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"LocalVariableTable".

attribute_length
The value of theattribute_length item indicates the length of the attribute, excluding the initial six bytes.

local_variable_table_length
The value of thelocal_variable_table_length item indicates the number of entries in thelocal_variable_table array.

local_variable_table[]
Each entry in thelocal_variable_table array indicates a range ofcode array 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 thecode array in the interval [start_pc,start_pc+length], that is, betweenstart_pc andstart_pc+length inclusive. The value ofstart_pc must be a valid index into thecode array of thisCode attribute and must be the index of the opcode of an instruction. Either the value ofstart_pc+length must be a valid index into thecode array of thisCode attribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of thatcode array.

name_index,descriptor_index
The value of thename_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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_index item must be a valid index into theconstant_pool table. Theconstant_pool entry 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 atindex in the local variable array of the current frame. If the local variable atindex is of typedouble orlong, it occupies bothindex andindex+1.

4.7.10 TheDeprecated Attribute

TheDeprecated attribute7 is an optional fixed-length attribute in theattributes table ofClassFile(§4.1),field_info(§4.5), andmethod_info(§4.6) structures.A class, interface, method, or field may be marked using aDeprecated attribute to indicate that the class, interface, method, or field has been superseded. A runtime interpreter or tool that reads theclass file 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 aDeprecated attribute does not alter the semantics of a class or interface.

TheDeprecated attribute has the following format:


    Deprecated_attribute {    u2 attribute_name_index;    u4 attribute_length;    }
The items of theDeprecated_attribute structure are as follows:

attribute_name_index
The value of theattribute_name_index item must be a valid index into theconstant_pool table. Theconstant_pool entry at that index must be aCONSTANT_Utf8_info(§4.4.7) structure representing the string"Deprecated".

attribute_length
The value of theattribute_length item 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 thecode array of theCode attribute of amethod_info structure of aclass file. This section describes the constraints associated with the contents of theCode_attribute structure.

4.8.1 Static Constraints

Thestatic constraints on aclass file are those defining the well-formedness of the file. With the exception of the static constraints on the Java virtual machine code of theclass file, these constraints have been given in the previous section. The static constraints on the Java virtual machine code in aclass file specify how Java virtual machine instructions must be laid out in thecode array and what the operands of individual instructions must be.

The static constraints on the instructions in thecode array are as follows:

The static constraints on the operands of instructions in thecode array are as follows:

4.8.2 Structural Constraints

The structural constraints on thecode array specify constraints on relationships between Java virtual machine instructions. The structural constraints are as follows:


4.9 Verification of Files

Even though Sun's compiler for the Java programming language attempts to produceonly 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-compiledclass files. The HotJava browser needs to determine whether theclass file 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 ofTradingClass might 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 frompublic toprivate. For a discussion of these issues, see Chapter 13, "Binary Compatibility," in the first edition ofTheJava 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 theclass files it attempts to incorporate. A Java virtual machine implementation verifies that eachclass file 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:

Sun'sclass file 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. Anyclass file that satisfies the structural criteria and static constraints will be certified by the verifier.

Theclass file verifier is also independent of the Java programming language. Programs written in other languages can be compiled into theclass file format, but will pass verification only if all the same constraints are satisfied.

4.9.1 The Verification Process

Theclass file verifier operates in four passes:

Pass 1:
When a prospectiveclass file is loaded(§2.17.2) by the Java virtual machine, the Java virtual machine first ensures that the file has the basic format of aclass file. The first four bytes must contain the right magic number. All recognized attributes must be of the proper length. Theclass file must not be truncated or have extra bytes at the end. The constant pool must not contain any superficially unrecognizable information.

Whileclass file verification properly occurs during class linking(§2.17.3), this check for basicclass file integrity is necessary for any interpretation of theclass file contents and can be considered to be logically part of the verification process.

Pass 2:
When theclass file is linked, the verifier performs all additional verification that can be done without looking at thecode array of theCode attribute (§4.7.3). The checks performed by this pass include the following:

Note that when it looks at field and method references, this pass does not check to make sure that the given field or method actually exists in the given class, nor does it check that the type descriptors given refer to real classes. It checks only that these items are well formed. More detailed checking is delayed until passes 3 and 4.

Pass 3:
During linking, the verifier checks thecode array of theCode attribute for each method of theclass file 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:

For further information on this pass, seeSection 4.9.2, "The Bytecode Verifier."

Pass 4:
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 loadingclass files 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 classA actually exists. However, if it is assigned to a field of the typeB, the definitions of bothA andB must be loaded in to ensure thatA is 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:

The first time an instruction invokes a method, or accesses or modifies a field, the executing instruction does the following:

The Java virtual machine does not have to check the type of the object on the operand stack. That check has already been done by Pass 3. Errors that are detected in Pass 4 cause instances of subclasses ofLinkageError to be thrown.

A Java virtual machine implementation is allowed to perform any or all of the Pass 4 steps as part of Pass 3; see2.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 inclass files, 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 ofclass file 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 thecode array 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:

For each instruction of the method, the verifier records the contents of the operand stack and the contents of the local variable array prior to the execution of that instruction. For the operand stack, it needs to know the stack height and the type of each value on it. For each local variable, it needs to know either the type of the contents of that local variable or that the local variable contains an unusable or unknown value (it might be uninitialized). The bytecode verifier does not need to distinguish between the integral types (e.g.,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:

  1. 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.

  2. Model the effect of the instruction on the operand stack and local variable array by doing the following:

  3. Determine the instructions that can follow the current instruction. Successor instructions can be one of the following:

  4. 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.

  5. Continue at step 1.
To merge two operand stacks, the number of values on each stack must be identical. The types of values on the stacks must also be identical, except that differently typedreference values may appear at corresponding places on the two stacks. In this case, the merged operand stack contains areference to an instance of the first common superclass of the two types. Such a reference type always exists because the typeObject is 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 containreference values, the verifier records that the local variable contains an unusable value. If both of the pair of local variables containreference values, the merged state contains areference to 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 theclass file 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 Typeslong anddouble

Values of thelong anddouble types are treated specially by the verification process.

Whenever a value of typelong ordouble is moved into a local variable at indexn, indexn + 1 is specially marked to indicate that it has been reserved by the value at indexn and may not be used as a local variable index. Any value previously at indexn + 1 becomes unusable.

Whenever a value is moved to a local variable at indexn, the indexn - 1 is examined to see if it is the index of a value of typelong ordouble. If so, the local variable at indexn - 1 is changed to indicate that it now contains an unusable value. Since the local variable at indexn has been overwritten, the local variable at indexn - 1 cannot represent a value of typelong ordouble.

Dealing with values of typeslong ordouble on the operand stack is simpler; the verifier treats them as single values on the stack. For example, the verification code for thedadd opcode (add twodouble values) checks that the top two items on the stack are both of typedouble. When calculating operand stack length, values of typelong anddouble have length two.

Untyped instructions that manipulate the operand stack must treat values of typedouble andlong as atomic (indivisible). For example, the verifier reports a failure if the top value on the stack is adouble and it encounters an instruction such aspop ordup. The instructionspop2 ordup2 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


    ...    new myClass(i, j, k);    ...
can be implemented by the following:


    ...    new#1// Allocate uninitialized space formyClass    dup// Duplicate object on the operand stack    iload_1// Push i    iload_2// Push j    iload_3// Push k    invokespecial #5 // InvokemyClass.<init>    ...
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 inChapter 7,"Compiling for the Java Virtual Machine.")

The instance initialization method(§3.9) for classmyClass sees the new uninitialized object as itsthis argument in local variable0. Before that method invokes another instance initialization method ofmyClass or its direct superclass onthis, the only operation the method can perform onthis is assigning fields declared withinmyClass.

When doing dataflow analysis on instance methods, the verifier initializes local variable0 to contain an object of the current class, or, for instance initialization methods, local variable0 contains 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 instructionnew. 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

new InputStream(new Foo(), new InputStream("foo"))

may have two uninitialized instances ofInputStream on 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 thesame 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 afinally clause. 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:

These restrictions are not enforced by theclass file 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 andfinally

Given the code fragment


    ...    try {     startFaucet();     waterLawn();    } finally {     stopFaucet();    }    ...
the Java programming language guarantees thatstopFaucet is invoked (the faucetis turned off) whether we finish watering the lawn or whether an exception occurs while starting the faucet or watering the lawn. That is, thefinally clause is guaranteed to be executed whether itstry clause completes normally or completesabruptly by throwing an exception.

To implement thetry-finally construct, Sun's compiler for the Java programming language uses the exception-handling facilities together with two special instructions:jsr ("jump to subroutine") andret ("return from subroutine"). Thefinally clause is compiled as a subroutine within the Java virtual machine code for its method, much like the code for an exception handler. When ajsr instruction that invokes the subroutine is executed, it pushes its return address, the address of the instruction after thejsr 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, aret instruction fetches the return address from the local variable and transfers control to the instruction at the return address.

Control can be transferred to thefinally clause (thefinally subroutine can be invoked) in several different ways. If thetry clause completes normally, thefinally subroutine is invoked via ajsr instruction before evaluating the next expression. Abreak orcontinue inside thetry clause that transfers control outside thetry clause executes ajsr to the code for thefinally clause first. If thetry clause executes areturn, the compiled code does the following:

  1. Saves the return value (if any) in a local variable.
  2. Executes ajsr to the code for thefinally clause.
  3. Upon return from thefinally clause, returns the value saved in the local variable.
The compiler sets up a special exception handler, which catches any exception thrown by thetry clause. If an exception is thrown in thetry clause, this exception handler does the following:

  1. Saves the exception in a local variable.
  2. Executes ajsr to thefinally clause.
  3. Upon return from thefinally clause, rethrows the exception.
For more information about the implementation of thetry-finally construct, seeSection 7.13, "Compilingfinally."

The code for thefinally clause 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, afinally clause might be called from several different places, yielding several different circumstances:

The code for thefinally clause itself might pass verification, but after completing the updating all the successors of theret 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 afinally clause is complicated. The basic idea is the following:


4.10 Limitations of the Java Virtual Machine

The following limitations of the Java virtual machine are implicit in theclass file format:


1 The Java virtual machine implementation of Sun's JDK release 1.0.2 supportsclass file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can supportclass file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can supportclass file 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 ofThe 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_pc is 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 anycode array) to 65534 bytes.

5 TheInnerClasses attribute was introduced in JDK release 1.1 to support nested classes and interfaces.

6 TheSynthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces.

7 TheDeprecated attribute was introduced in JDK release 1.1 to support the@deprecated tag in documentation comments.

Contents |Prev |Next |Index


[8]ページ先頭

©2009-2025 Movatter.jp