⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classfile.doc.html

📁 Jvm 规范说明。The Java Virtual Machine was designed to support the Java programming language. Some concep
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>VM Spec  The class File Format</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<table width=100%><tr>
<td><a href="VMSpecTOC.doc.html">Contents</a> | <a href="Overview.doc.html">Prev</a> | <a href="ConstantPool.doc.html">Next</a> | <a href="Lindholm.INDEX.html">Index</a></td><td align=right><i><i>The Java<sup><font size=-2>TM</font></sup> Virtual Machine Specification</i></i></td>
</tr></table>


<hr><br>
 
<a name="3660"></a>
<p><strong>CHAPTER 4 </strong></p>
<a name="4639"></a>
<h1>The class File Format</h1>
<hr><p>
<a name="3661"></a>
This chapter describes the Java Virtual Machine <code>class</code> file format. Each <code>class</code> file 
contains one Java type, either a class or an interface. Compliant Java Virtual 
Machine implementations must be capable of dealing with all <code>class</code> files that conform to the specification provided by this book. 
<p><a name="14580"></a>
A <code>class</code> 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 Java, this format is supported by inter-faces <code>java.io.DataInput</code> and <code>java.io.DataOutput</code> and classes such as <code>java.io.DataInputStream</code> and <code>java.io.DataOutputStream</code>.<p>
<a name="6289"></a>
This chapter defines its own set of data types representing Java <code>class</code> file data: The types <code>u1</code>, <code>u2</code>, and <code>u4</code> represent an unsigned one-, two-, or four-byte quantity, respectively. In Java, these types may be read by methods such as <code>readUnsignedByte</code>, <code>readUnsignedShort</code>, and <code>readInt</code> of the interface <code>java.io.DataInput</code>.<p>
<a name="7076"></a>
The Java <code>class</code> file format is presented using pseudostructures written in a C-like structure notation. To avoid confusion with the fields of Java Virtual Machine classes and class instances, the contents of the structures describing the Java <code>class</code> file format are referred to as <em>items</em>. Unlike the fields of a C structure, successive items are stored in the Java <code>class</code> file sequentially, without padding or alignment. <p>
<a name="13303"></a>
Variable-sized <i>tables</i>, consisting of variable-sized items, are used in several <code>class</code> file structures. Although we will 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 directly translate a table index into a byte offset into the table. <p>
<a name="15336"></a>
Where we refer to a data structure as an array, it is literally an array.<p>
<a name="21045"></a>
<hr><h2>4.1	 <em>ClassFile</em></h2>
<a name="21068"></a>
A <code>class</code> file contains a single <code>ClassFile</code> structure:
<p><pre><br><a name="19050"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>ClassFile {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u4 magic;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 minor_version;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 major_version;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 constant_pool_count;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	cp_info constant_pool[constant_pool_count-1];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 access_flags;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 this_class;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 super_class;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 interfaces_count;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 interfaces[interfaces_count];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 fields_count;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	field_info fields[fields_count];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 methods_count;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	method_info methods[methods_count];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	u2 attributes_count;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	attribute_info attributes[attributes_count];
</code><a name="19084"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="7456"></a>
The items in the <code>ClassFile</code> structure are as follows:
<p><a name="19719"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;magic</b><br><blockquote><a name="21106"></a>
The <code>magic</code> item supplies the magic number identifying the <code>class</code> file format; it has the value <code>0xCAFEBABE</code>.<p>
</blockquote><a name="13448"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;minor_version, major_version</b><br><blockquote><a name="2929"></a>
The values of the <code>minor_version</code> and <code>major_version</code> items are the minor and major version numbers of the compiler that produced this <code>class</code> file. An implementation of the Java Virtual Machine normally supports <code>class</code> files having a given major version number and minor version numbers <code>0</code> through some particular <code>minor_version</code>.<p>
<a name="4983"></a>
If an implementation of the Java Virtual Machine supports some range of minor version numbers and a <code>class</code> file of the same major version but a higher minor version is encountered, the Java Virtual Machine must not attempt to run the newer code. However, unless the major version number differs, it will be feasible to implement a new Java Virtual Machine that can run code of minor versions up to and including that of the newer code.<p>
<a name="2930"></a>
A Java Virtual Machine must not attempt to run code with a different major version. A change of the major version number indicates a major incompatible change, one that requires a fundamentally different Java Virtual Machine. <p>
<a name="6539"></a>
In Sun's Java Developer's Kit (JDK) 1.0.2 release, documented by this book, the value of <code>major_version</code> is <code>45</code>. The value of <code>minor_version</code> is <code>3</code>. Only Sun may define the meaning of new <code>class</code> file version numbers.<p>
</blockquote><a name="13480"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;constant_pool_count</b><br><blockquote><a name="19092"></a>
The value of the <code>constant_pool_count</code> item must be greater than zero. It gives the number of entries in the <code>constant_pool</code> table of the <code>class</code> file, where the <code>constant_pool</code> entry at index zero is included in the count but is not present in the <code>constant_pool</code> table of the class file. A <code>constant_pool</code> index is considered valid if it is greater than zero and less than <code>constant_pool_count</code>.<p>
</blockquote><a name="19093"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;constant_pool[]</b><br><blockquote><a name="19096"></a>
The <code>constant_pool</code> is a table of variable-length structures <a href="ClassFile.doc.html#20080">(&#167;4.4)</a> representing various string constants, class names, field names, and other constants that are referred to within the <code>ClassFile</code> structure and its substructures.<p>
<a name="13295"></a>
The first entry of the <code>constant_pool</code> table, <code>constant_pool[0]</code>, is reserved for internal use by a Java Virtual Machine implementation. That entry is <i>not</i> present in the <code>class</code> file. The first entry in the <code>class</code> file is <code>constant_pool[1]</code>. <p>
<a name="11856"></a>
Each of the <code>constant_pool</code> table entries at indices <code>1</code> through <code>constant_pool_count-1</code> is a variable-length structure <a href="ClassFile.doc.html#20080">(&#167;4.4)</a> whose format is indicated by its first "tag" byte.<p>
</blockquote><a name="13484"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;access_flags</b><br><blockquote><a name="23242"></a>
The value of the <code>access_flags</code> item is a mask of modifiers used with class and interface declarations. The <code>access_flags</code> modifiers are shown in Table 4.1.<p><Table Border="0">
<tr><td><a name="23254"></a>
Flag Name
<td><a name="23256"></a>
Value
<td><a name="23258"></a>
Meaning
<td><a name="23260"></a>
Used By

<tr><td><a name="23262"></a>
<code>ACC_PUBLIC</code>
<td><a name="23264"></a>
<code>0x0001</code>
<td><a name="23266"></a>
Is public; may be accessed from outside its package. 
<td><a name="23268"></a>
Class, interface

<tr><td><a name="23270"></a>
<code>ACC_FINAL</code>
<td><a name="23272"></a>
<code>0x0010</code>
<td><a name="23274"></a>
Is final; no subclasses allowed.
<td><a name="23276"></a>
Class

<tr><td><a name="23278"></a>
<code>ACC_SUPER</code>
<td><a name="23280"></a>
0x0020
<td><a name="23282"></a>
Treat superclass methods specially in <i>invokespecial</i>.
<td><a name="23284"></a>
Class, interface

<tr><td><a name="23286"></a>
<code>ACC_INTERFACE</code>
<td><a name="23288"></a>
<code>0x0200</code>
<td><a name="23290"></a>
Is an interface. 
<td><a name="23292"></a>
Interface

<tr><td><a name="23294"></a>
<code>ACC_ABSTRACT</code>
<td><a name="23296"></a>
<code>0x0400</code>
<td><a name="23298"></a>
Is abstract; may not be instantiated.
<td><a name="23300"></a>
Class, interface

</Table><br><br><p>
<a name="23301"></a>
An interface is distinguished by its <code>ACC_INTERFACE</code> flag being set. If <code>ACC_INTERFACE</code> is not set, this class file defines a class, not an interface.<p>
<a name="14189"></a>
Interfaces may only use flags indicated in <a href="ClassFile.doc.html#23242">Table 4.1</a> as used by interfaces. Classes may only use flags indicated in <a href="ClassFile.doc.html#23242">Table 4.1</a> as used by classes. An interface is implicitly <code>abstract</code> <a href="Concepts.doc.html#18339">(&#167;2.13.1)</a>; its <code>ACC_ABSTRACT</code> flag must be set. An interface cannot be <code>final</code>; its implementation could never be completed <a href="Concepts.doc.html#18339">(&#167;2.13.1)</a> if it were, so it could not have its <code>ACC_FINAL</code> flag set.<p>
<a name="22069"></a>
The flags <code>ACC_FINAL</code> and <code>ACC_ABSTRACT</code> cannot both be set for a class; the implementation of such a class could never be completed <a href="Concepts.doc.html#20340">(&#167;2.8.2)</a>.<p>
<a name="22165"></a>
The setting of the <code>ACC_SUPER</code> flag directs the Java Virtual Machine which of two alternative semantics for its <i>invokespecial</i> instruction to express; it exists for backward compatibility for code compiled by Sun's older Java compilers. All new implementations of the Java Virtual Machine should implement the semantics for <i>invokespecial</i> documented in Chapter <a href="Instructions.doc.html#13311">6</a>, <a href="Instructions.doc.html#13312">"Java Virtual Machine Instruction Set."</a> All new compilers to the Java Virtual Machine's instruction set should set the <code>ACC_SUPER</code> flag. Sun's older Java compilers generate <code>ClassFile</code> flags with <code>ACC_SUPER</code> unset. Sun's older Java Virtual Machine implementations ignore the flag if it is set.<p>
<a name="22118"></a>
All unused bits of the <code>access_flags</code> item, including those not assigned in <a href="ClassFile.doc.html#23242">Table 4.1</a>, are reserved for future use. They should be set to zero in generated <code>class</code> files and should be ignored by Java Virtual Machine implementations.<p>
</blockquote><a name="13052"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;this_class</b><br><blockquote><a name="2115"></a>
The value of the <code>this_class</code> item must be a valid index into the <code>constant_pool</code> table. The <code>constant_pool</code> entry at that index must be a <code>CONSTANT_Class_info</code> <a href="ClassFile.doc.html#1221">(&#167;4.4.1)</a> structure representing the class or interface defined by this <code>class</code> file.<p>
</blockquote><a name="18651"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;super_class</b><br><blockquote><a name="19632"></a>
For a class, the value of the <code>super_class</code> item either must be zero or must be a valid index into the <code>constant_pool</code> table. If the value of the <code>super_class</code> item is nonzero, the <code>constant_pool</code> entry at that index must be a <code>CONSTANT_Class_info</code> <a href="ClassFile.doc.html#1221">(&#167;4.4.1)</a> structure representing the superclass of the class defined by this <code>class</code> file. Neither the superclass nor any of its superclasses may be a <code>final</code> class.<p>
<a name="21130"></a>
If the value of <code>super_class</code> is zero, then this <code>class</code> file must represent the class <code>java.lang.Object</code>, the only class or interface without a superclass.<p>
<a name="11369"></a>
For an interface, the value of <code>super_class</code> must always be a valid index into the <code>constant_pool</code> table. The <code>constant_pool</code> entry at that index must be a <code>CONSTANT_Class_info</code> structure representing the class <code>java.lang.Object</code>.<p>
</blockquote><a name="18662"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;interfaces_count</b><br><blockquote><a name="19639"></a>
The value of the <code>interfaces_count</code> item gives the number of direct superinterfaces of this class or interface type.<p>
</blockquote><a name="18665"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;interfaces[]</b><br><blockquote><a name="6757"></a>
Each value in the <code>interfaces</code> array must be a valid index into the <code>constant_pool</code> table. The <code>constant_pool</code> entry at each value of <code>interfaces[</code><i>i</i><code>]</code>, where <code>0</code> &#163; i < <code>interfaces_count</code>, must be a <code>CONSTANT_Class_info</code> <a href="ClassFile.doc.html#1221">(&#167;4.4.1)</a> structure representing an interface which is a direct superinterface of this class or interface type, in the left-to-right order given in the source for the type.<p>
</blockquote><a name="6758"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;fields_count</b><br><blockquote><a name="23377"></a>
The value of the <code>fields_count</code> item gives the number of <code>field_info</code> structures in the <code>fields</code> table. The <code>field_info </code><a href="ClassFile.doc.html#2877">(&#167;4.5)</a> structures represent all fields, both class variables and instance variables, declared by this class or interface type.<p>
</blockquote><a name="19653"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;fields[]</b><br><blockquote><a name="19655"></a>
Each value in the <code>fields</code> table must be a variable-length <code>field_info</code> <a href="ClassFile.doc.html#2877">(&#167;4.5)</a> structure giving a complete description of a field in the class or interface type. The <code>fields</code> 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.<p>
</blockquote><a name="19656"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;methods_count</b><br><blockquote><a name="19657"></a>
The value of the <code>methods_count</code> item gives the number of <code>method_info</code> structures in the <code>methods</code> table. <p>
</blockquote><a name="19659"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;methods[]</b><br><blockquote><a name="7189"></a>
Each value in the <code>methods</code> table must be a variable-length <code>method_info</code> <a href="ClassFile.doc.html#1513">(&#167;4.6)</a> structure giving a complete description of and Java Virtual Machine code for a method in the class or interface. <p>
<a name="18125"></a>
The <code>method_info</code> structures represent all methods, both instance methods and, for classes, class (<code>static</code>) methods, declared by this class or interface type. The <code>methods</code> table only includes those methods that are explicitly declared by this class. Interfaces have only the single method <code>&lt;clinit&gt;</code>, the interface initialization method <a href="Overview.doc.html#12174">(&#167;3.8)</a>. The <code>methods</code> table does not include items representing methods that are inherited from superclasses or superinterfaces.<p>
</blockquote><a name="7193"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;attributes_count</b><br><blockquote><a name="18668"></a>
The value of the <code>attributes_count</code> item gives the number of attributes <a href="ClassFile.doc.html#1442">(&#167;4.7)</a> in the <code>attributes</code> table of this class.<p>
</blockquote><a name="19684"></a>
<b>&nbsp; &nbsp; &nbsp; &nbsp;attributes[]</b><br><blockquote><a name="19685"></a>
Each value of the <code>attributes</code> table must be a variable-length attribute structure. A <code>ClassFile</code> structure can have any number of attributes <a href="ClassFile.doc.html#1442">(&#167;4.7)</a> associated with it. <p>
<a name="14273"></a>
The only attribute defined by this specification for the <code>attributes</code> table of a <code>ClassFile</code> structure is the <code>SourceFile</code> attribute <a href="ClassFile.doc.html#1365">(&#167;4.7.2)</a>.<p>
<a name="14249"></a>
A Java Virtual Machine implementation is required to silently ignore any or all attributes in the <code>attributes</code> table of a <code>ClassFile</code> structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the <code>class</code> file, but only to provide additional descriptive information <a href="ClassFile.doc.html#16733">(&#167;4.7.1)</a>.<p>
</blockquote><a name="14757"></a>
<hr><h2>4.2	 Internal Form of Fully Qualified Class Names</h2>
<a name="14894"></a>
Class names that appear in <code>class</code> file structures are always represented in a fully 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -