Java class file

来源:互联网 发布:sql语句去重 distinct 编辑:程序博客网 时间:2024/05/16 15:47

源自:http://en.wikipedia.org/wiki/Java_class_file#File_layout_and_structure

In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension. Since Java is aplatform-independent language, source code is compiled into an output file known as bytecode, which it stores in a .class file. If a source file has more than one class, each class is compiled into a separate .class file. These .class files can be loaded by anyJava Virtual Machine (JVM).

JVMs are available for many platforms, and the .class file compiled in one platform will execute in a JVM of another platform. This makes Java platform-independent.

Contents

[hide]
  • 1History
  • 2File layout and structure
    • 2.1Sections
    • 2.2Magic Number
    • 2.3General layout
    • 2.4Representation in a C-like programming language
    • 2.5The constant pool
  • 3See also
  • 4References
  • 5Further reading

[edit]History

As of 2006[update], the modification of the class file format is being considered under Java Specification Request (JSR) 202.[1]

[edit]File layout and structure

[edit]Sections

There are 10 basic sections to the Java Class File structure:

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • Access Flags: for example whether the class is abstract, static, etc.
  • This Class: The name of the current class
  • Super Class: The name of the super class
  • Interfaces: Any interfaces in the class
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

There is a handy mnemonic for remembering these 10: My Very Cute Animal Turns SavageIn Full Moon Areas.

Magic, Version, Constant, Access, This, Super, Interfaces, Fields, Methods, Attributes (MVCATSIFMA)

[edit]Magic Number

Class files are identified by the following 4 byte header (in hexadecimal): CA FE BA BE (the first 4 entries in the below table). The history of thismagic number was explained by James Gosling:[2]

"We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, theGrateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. WhenJerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and ingrepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced byRMI."

[edit]General layout

Because the class file contains variable-sized items and does not also contain embedded file offsets (or pointers), it is typically parsed sequentially, from the first byte toward the end. At the lowest level the file format is described in terms of a few fundamental data types:

  • u1: an unsigned 8-bit integer
  • u2: an unsigned 16-bit integer in big-endian byte order
  • u4: an unsigned 32-bit integer in big-endian byte order
  • table: an array of variable-length items of some type. The number of items in the table is identified by a preceding count number, but the size in bytes of the table can only be determined by examining each of its items.

Some of these fundamental types are then re-interpreted as higher-level values (such as strings or floating-point numbers), depending on context. There is no enforcement of word alignment, and so no padding bytes are ever used. The overall layout of the class file is as shown in the following table.

byte offsetsizetype or valuedescription04 bytesu1 =
0xCA hexmagic number (CAFEBABE) used to identify file as conforming to the class file format1u1 =
0xFE hex2u1 =
0xBA hex3u1 =
0xBE hex42 bytesu2minor version number of the class file format being used562 bytesu2major version number of the class file format being used.
J2SE 7 = 51 (0x33 hex),
J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).
For details of earlier version numbers see footnote 1 at The JavaTM Virtual Machine Specification 2nd edition782 bytesu2constant pool count, number of entries in the following constant pool table. This count is at least one greater than the actual number of entries; see following discussion.910cpsize (variable)tableconstant pool table, an array of variable-sized constant pool entries, containing items such as literal numbers, strings, and references to classes or methods. Indexed starting at 1, containing (constant pool count - 1) number of entries in total (see note)..........10+cpsize2 bytesu2access flags, a bitmask11+cpsize12+cpsize2 bytesu2identifies this class, index into the constant pool to a "Class"-type entry13+cpsize14+cpsize2 bytesu2identifies super class, index into the constant pool to a "Class"-type entry15+cpsize16+cpsize2 bytesu2interface count, number of entries in the following interface table17+cpsize18+cpsizeisize (variable)tableinterface table, an array of variable-sized interfaces.........18+cpsize+isize2 bytesu2field count, number of entries in the following field table19+cpsize+isize20+cpsize+isizefsize (variable)tablefield table, variable length array of fields.........20+cpsize+isize+fsize2 bytesu2method count, number of entries in the following method table21+cpsize+isize+fsize22+cpsize+isize+fsizemsize (variable)tablemethod table, variable length array of methods.........22+cpsize+isize+fsize+msize2 bytesu2attribute count, number of entries in the following attribute table23+cpsize+isize+fsize+msize24+cpsize+isize+fsize+msizeasize (variable)tableattribute table, variable length array of attributes.........

[edit]Representation in a C-like programming language

Since C doesn't support multiple variable length arrays within a struct, the code below won't compile and only serves as a demonstration.

struct Class_File_Format {   u4 magic_number;       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];}

[edit]The constant pool

The constant pool table is where most of the literal constant values are stored. This includes values such as numbers of all sorts, strings, identifier names, references to classes and methods, and type descriptors. All indexes, or references, to specific constants in the constant pool table are given by 16-bit (type u2) numbers, where index value 1 refers to the first constant in the table (index value 0 is invalid).

Due to historic choices made during the file format development, the number of constants in the constant pool table is not actually the same as the constant pool count which precedes the table. First, the table is indexed starting at 1 (rather than 0), so the count should actually be interpreted as the maximum index. Additionally, two types of constants (longs and doubles) take up two consecutive slots in the table, although the second such slot is a phantom index that is never directly used.

The type of each item (constant) in the constant pool is identified by an initial bytetag. The number of bytes following this tag and their interpretation are then dependent upon the tag value. The valid constant types and their tag values are:

Tag byteAdditional bytesDescription of constant12+x bytes
(variable)UTF-8 (Unicode) string: a character string prefixed by a 16-bit number (type u2) indicating the number of bytes in the encoded string which immediately follows (which may be different than the number of characters). Note that the encoding used is not actuallyUTF-8, but involves a slight modification of the Unicode standard encoding form.34 bytesInteger: a signed 32-bit two's complement number in big-endian format44 bytesFloat: a 32-bit single-precision IEEE 754 floating-point number58 bytesLong: a signed 64-bit two's complement number in big-endian format (takes two slots in the constant pool table)68 bytesDouble: a 64-bit double-precision IEEE 754 floating-point number (takes two slots in the constant pool table)72 bytesClass reference: an index within the constant pool to a UTF-8 string containing the fully qualified class name (ininternal format)82 bytesString reference: an index within the constant pool to a UTF-8 string94 bytesField reference: two indexes within the constant pool, the first pointing to a Class reference, the second to a Name and Type descriptor.104 bytesMethod reference: two indexes within the constant pool, the first pointing to a Class reference, the second to a Name and Type descriptor.114 bytesInterface method reference: two indexes within the constant pool, the first pointing to a Class reference, the second to a Name and Type descriptor.124 bytesName and type descriptor: two indexes to UTF-8 strings within the constant pool, the first representing a name (identifier) and the second a specially encoded type descriptor.

There are only two integral constant types, integer and long. Other integral types appearing in the high-level language, such as boolean, byte, and short must be represented as an integer constant.

Class names in Java, when fully qualified, are traditionally dot-separated, such as "java.lang.Object". However within the low-level Class reference constants, an internal form appears which uses slashes instead, such as "java/lang/Object".

The Unicode strings, despite the moniker "UTF-8 string", are not actually encoded according to the Unicode standard, although it is similar. There are two differences (seeUTF-8 for a complete discussion). The first is that the codepoint U+0000 is encoded as the two-byte sequenceC0 80 (in hex) instead of the standard single-byte encoding 00. The second difference is that supplementary characters (those outside theBMP at U+10000 and above) are encoded using a surrogate-pair construction similar toUTF-16 rather than being directly encoded using UTF-8. In this case each of the two surrogates is encoded separately in UTF-8. For example U+1D11E is encoded as the 6-byte sequenceED A0 B4 ED B4 9E, rather than the correct 4-byte UTF-8 encoding of F0 9D 84 9E.