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

📄 classfile.doc.html

📁 A Java virtual machine instruction consists of an opcode specifying the operation to be performed, f
💻 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="VMSpecIX.fm.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="80959"></a><p><strong>CHAPTER 4 </strong></p><a name="80961"></a><h1>The <code><font size=+4>class</font></code> File Format</h1><hr><p><a name="80963"></a>This chapter describes the Java virtual machine <strong></strong><code>class</code> file format. Each <code>class</code> file contains the definition of a single class or interface. Although a class or interface need not have an external representation literally contained in a file (for instance, because the class is generated by a class loader), we will colloquially refer to any valid representation of a class or interface as being in the <code>class</code> file format.<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 the Java and Java 2 platforms, this format is supported by interfaces <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 <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 the Java and Java 2 platforms, 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="41600"></a>This chapter presents the <code>class</code> file format using pseudostructures written in a C-like structure notation. To avoid confusion with the fields of classes and class instances, etc., the contents of the structures describing the <code>class</code> file format are referred to as <em>items</em>. Successive items are stored in the <code>class</code> file sequentially, without padding or alignment. <p><a name="74328"></a><em>Tables</em>, consisting of zero or more variable-sized items, are used in several <code>class</code> 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. <p><a name="74331"></a>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.<p><a name="74353"></a><hr><h2>4.1    The <code>ClassFile</code>&#32;Structure</h2><a name="44115"></a>A <code>class</code> file consists of a single <code>ClassFile</code> structure:<p><pre><br><a name="19050"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>ClassFile {</code>&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><dl><dt><code>magic</code><a name="21106"></a><dd> The <code>magic</code> item supplies the magic number identifying the <code>class</code> file format; it has the value <code>0xCAFEBABE</code>.<p><a name="13448"></a><dt><code>minor_version</code>, <code>major_version</code><a name="70425"></a><dd> The values of the <code>minor_version</code> and <code>major_version</code> items are the minor and major version numbers of this <code>class</code> file.Together, a major and a minor version number determine the version of the <code>class</code> file format. If a <code>class</code> file has major version number M and minor version number m, we denote the version of its <code>class</code> file format as M.m. Thus, <code>class</code> file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.<p>A Java virtual machine implementation can support a <code>class</code> file format of version v if and only if v lies in some contiguous range Mi.0 <img src="chars/lt_equal.gif"> v <img src="chars/lt_equal.gif"> 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.<a href="#75883"><sup>1</sup></a><p><a name="13480"></a><dt><code>constant_pool_count</code><a name="31689"></a><dd> The value of the <code>constant_pool_count</code> item is equal to the number of entries in the <code>constant_pool</code> table plus one. A <code>constant_pool</code> index is considered valid if it is greater than zero and less than <code>constant_pool_count</code>, with the exception for constants of type <code>long</code> and <code>double</code> noted in <a href="ClassFile.doc.html#1348">&#167;4.4.5</a>.<p><a name="29202"></a><dt><code>constant_pool[]</code><a name="19096"></a><dd> The <code>constant_pool</code> is a table of structures <a href="ClassFile.doc.html#20080">(&#167;4.4)</a> representing various string constants, class and interface names, field names, and other constants that are referred to within the <code>ClassFile</code> structure and its substructures. The format of each <code>constant_pool</code> table entry is indicated by its first "tag" byte.<p>The <code>constant_pool</code> table is indexed from <code>1</code> to <code>constant_pool_count</code>-<code>1</code>. <p><a name="13484"></a><dt><code>access_flags</code><a name="23242"></a><dd> The value of the <code>access_flags</code> 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 in <a href="ClassFile.doc.html#75734">Table 4.1</a>.</dl><a name="75734"></a><p><Table Border="1"><tr><td><a name="75687"></a><strong>Flag Name</strong><td><a name="75689"></a><strong>Value</strong><td><a name="75691"></a><strong>Interpretation</strong><tr><td><a name="75695"></a><code>ACC_PUBLIC</code><td><a name="75697"></a><code>0x0001</code><td><a name="75699"></a>Declared <code>public</code>; may be accessed from outside its package. <tr><td><a name="75703"></a><code>ACC_FINAL</code><td><a name="75705"></a><code>0x0010</code><td><a name="75707"></a>Declared <code>final</code>; no subclasses allowed.<tr><td><a name="75711"></a><code>ACC_SUPER</code><td><a name="75713"></a><code>0x0020</code><td><a name="75715"></a>Treat superclass methods specially when invoked by the <i>invokespecial</i> instruction.<tr><td><a name="75719"></a><code>ACC_INTERFACE</code><td><a name="75721"></a><code>0x0200</code><td><a name="75723"></a>Is an interface, not a class.<tr><td><a name="75727"></a><code>ACC_ABSTRACT</code><td><a name="75729"></a><code>0x0400</code><td><a name="75731"></a>Declared <code>abstract</code>; may not be instantiated.</Table><br><br><p><a name="23301"></a>An interface is distinguished by its <code>ACC_INTERFACE</code> flag being set. If its <code>ACC_INTERFACE</code> flag is not set, this <code>class</code> file defines a class, not an interface.<p><a name="75587"></a>If the <code>ACC_INTERFACE</code> flag of this <code>class</code> file is set, its <code>ACC_ABSTRACT</code> flag must also be set <a href="Concepts.doc.html#18339">(&#167;2.13.1)</a> and its <code>ACC_PUBLIC</code> flag may be set. Such a <code>class</code> file may not have any of the other flags in <a href="ClassFile.doc.html#75734">Table 4.1</a> set.<p><a name="74404"></a>If the <code>ACC_INTERFACE</code> flag of this <code>class</code> file is not set, it may have any of the other flags in <a href="ClassFile.doc.html#75734">Table 4.1</a> set. However, such a <code>class</code> file cannot have both its <code>ACC_FINAL</code> and <code>ACC_ABSTRACT</code> flags set <a href="Concepts.doc.html#20340">(&#167;2.8.2)</a>. <p><a name="75639"></a>The setting of the <code>ACC_SUPER</code> flag indicates which of two alternative semantics for its <i>invokespecial</i> instruction the Java virtual machine is to express; the <code>ACC_SUPER</code> flag exists for backward compatibility for code compiled by Sun's older compilers for the Java programming language. All new implementations of the Java virtual machine should implement the semantics for <i>invokespecial</i> documented in this specification. All new compilers to the instruction set of the Java virtual machine should set the <code>ACC_SUPER</code> flag. Sun's older compilers generated <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="75646"></a>All bits of the <code>access_flags</code> item not assigned in <a href="ClassFile.doc.html#75734">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><a name="13052"></a><dl><dt><code>this_class</code><a name="2115"></a><dd> 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><a name="18651"></a><dt><code>super_class</code><dd> 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 direct superclass of the class defined by this <code>class</code> file. Neither the direct superclass nor any of its superclasses may be a <code>final</code> class.<p>If the value of the <code>super_class</code> item is zero, then this <code>class</code> file must represent the class <code>Object</code>, the only class or interface without a direct superclass.<p>For an interface, the value of the <code>super_class</code> item 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>Object</code>.<p><a name="18662"></a><dt><code>interfaces_count</code><dd> The value of the <code>interfaces_count</code> item gives the number of direct superinterfaces of this class or interface type.<p><a name="18665"></a><dt><code>interfaces[]</code><dd> 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> <img src="chars/lt_equal.gif"> 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 that is a direct superinterface of this class or interface type, in the left-to-right order given in the source for the type.<p><a name="6758"></a><dt>fields_count</code><dd> 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><a name="19653"></a><dt><code>fields[]</code><dd> Each value in the <code>fields</code> table must be a <code>field_info</code> <a href="ClassFile.doc.html#2877">(&#167;4.5)</a> structure giving a complete description of a field in this class or interface. 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><a name="19656"></a><dt><code>methods_count</code><dd> 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><a name="19659"></a><dt><code>methods[]</code><dd> Each value in the <code>methods</code> table must be a <code>method_info</code> <a href="ClassFile.doc.html#1513">(&#167;4.6)</a> structure giving a complete description of a method in this class or interface. If the method is not <code>native</code> or <code>abstract</code>, the Java virtual machine instructions implementing the method are also supplied. <p>The <code>method_info</code> structures represent all methods declared by this class or interface type, including instance methods, class (<code>static</code>) methods, instance initialization methods <a href="Overview.doc.html#12174">(&#167;3.9)</a>, and any class or interface initialization method <a href="Overview.doc.html#12174">(&#167;3.9)</a>. The <code>methods</code> table does not include items representing methods that are inherited from superclasses or superinterfaces.<p><a name="7193"></a><dt><code>attributes_count</code><dd> The value of the <code>attributes_count</code> item gives the number of attributes <a href="ClassFile.doc.html#43817">(&#167;4.7)</a> in the <code>attributes</code> table of this class.<p><a name="19684"></a><dt><code>attributes[]</code><dd> Each value of the <code>attributes</code> table must be an attribute structure <a href="ClassFile.doc.html#43817">(&#167;4.7)</a>.<p>The only attributes defined by this specification as appearing in the <code>attributes</code> table of a <code>ClassFile</code> structure are the <code>SourceFile</code> attribute <a href="ClassFile.doc.html#79868">(&#167;4.7.7)</a> and the <code>Deprecated</code> <a href="ClassFile.doc.html#78232">(&#167;4.7.10)</a> attribute.<p>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><a name="14757"></a></dl><hr><h2>4.2    The Internal Form of Fully Qualified Class  and Interface Names</h2><a name="14894"></a>Class and interface names that appear in <code>class</code> file structures are always representedin a fully qualified form <a href="Concepts.doc.html#20207">(&#167;2.7.5)</a>. Such names are always represented as <code>CONSTANT_Utf8_info</code> <a href="ClassFile.doc.html#7963">(&#167;4.4.7)</a> structures and thus may be drawn, where not furtherconstrained, from the entire Unicode character set. Class names and interfacesare referenced both from those <code>CONSTANT_NameAndType_info</code> <a href="ClassFile.doc.html#1327">(&#167;4.4.6)</a> structures that have such names as part of their descriptor <a href="ClassFile.doc.html#1169">(&#167;4.3)</a> and from all <code>CONSTANT_Class_info</code> <a href="ClassFile.doc.html#1221">(&#167;4.4.1)</a> structures.<p><a name="14922"></a>For historical reasons the syntax of fully qualified class and interface names that appear in <code>class</code> file structures differs from the familiar syntax of fully qualified names documented in <a href="Concepts.doc.html#20207">&#167;2.7.5</a>. In this internal form, the ASCII periods (<code>'</code>.<code>'</code>) that normally separate the identifiers that make up the fully qualified name are replaced by ASCII forward slashes (<code>'/'</code>). For example, the normal fully qualified name of class <code>Thread</code> is <code>java.lang.Thread</code>. In the form used in descriptors in the <code>class</code> file format, a reference to the name of class <code>Thread</code> is implemented using a <code>CONSTANT_Utf8_info</code> structure representing the string <code>"java/lang/Thread"</code>.<p><a name="1169"></a><hr><h2>4.3    Descriptors</h2><a name="1170"></a>A <em>descriptor</em> is a string representing the type of a field or method. Descriptors are represented in the <code>class</code> file format using UTF-8 strings <a href="ClassFile.doc.html#7963">(&#167;4.4.7)</a> and thus may be drawn, where not further constrained, from the entire Unicode character set.<p><a name="13972"></a><h3>4.3.1    Grammar Notation</h3><a name="14150"></a>Descriptors are specified using a grammar. This grammar is a set of productions that describe how sequences of characters can form syntactically correct descriptors of various types. Terminal symbols of the grammar are shown in bold fixed-width font. Nonterminal symbols are shown in <i>italic</i> 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:<p><a name="81070"></a><i>FieldType:</i><br>

⌨️ 快捷键说明

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