📄 chap06.html
字号:
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Class</FONT></TD><TD VALIGN="TOP">7</TD><TD VALIGN="TOP">A symbolic reference to a class or interface</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_String</FONT></TD><TD VALIGN="TOP">8</TD><TD VALIGN="TOP">A <FONT FACE="Courier New">String</FONT> literal value</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Fieldref</FONT></TD><TD VALIGN="TOP">9</TD><TD VALIGN="TOP">A symbolic reference to a field</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Methodref</FONT></TD><TD VALIGN="TOP">10</TD><TD VALIGN="TOP">A symbolic reference to a method declared in a class</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_InterfaceMethodref</FONT></TD><TD VALIGN="TOP">11</TD><TD VALIGN="TOP">A symbolic reference to a method declared in an interface</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_NameAndType</FONT></TD><TD VALIGN="TOP">12</TD><TD VALIGN="TOP">Part of a symbolic reference to a field or method</TD></TR>
</TABLE>
<P>For each tag shown in Table 6-3, there is a corresponding table. The name of the table is formed by appending "<FONT FACE="Courier New">_info</FONT>" to the tag name. For example, the table that corresponds to the <FONT FACE="Courier New">CONSTANT_Class</FONT> tag is called <FONT FACE="Courier New">CONSTANT_Class_info</FONT>. The <FONT FACE="Courier New">CONSTANT_Utf8_info</FONT> table stores a compressed form of Unicode strings. The tables for the various kinds of constant pool entries are described in detail later in this chapter.</P>
<P>The constant pool plays an important role in the dynamic linking of Java programs. In addition to literal constant values, the constant pool contains the following kinds of symbolic references:</P>
<UL>
<LI> fully qualified names of classes and interfaces
<LI> field names and descriptors
<LI> method names and descriptors
</UL>
<P>A <I>field</I> is an instance or class variable of the class or interface. A <I>field descriptor</I> is a string that indicates the field韘 type. A <I>method descriptor</I> is a string that indicates the method韘 return type and the number, order, and types of its parameters. The constant pool韘 fully qualified names and method and field descriptors are used at run time to link code in this class or interface with code and data in other classes and interfaces. The class file contains no information about the eventual memory layout of its components, so classes, fields, and methods cannot be referenced directly by the bytecodes in the class file. The Java Virtual Machine resolves the actual address of any referenced item at run time given a symbolic reference from the constant pool. For example, bytecode instructions that invoke a method give constant pool index of a symbolic reference to the method to invoke. This process of using the symbolic references in the constant pool is described in more detail in Chapter 8, "The Linking Model."</P>
<H3><P>access_flags</P>
</H3><P>The first two bytes after the constant pool, the <I>access flags</I>, reveal several pieces of information about the class or interface defined in the file. To start with, the access flags indicate whether the file defines a class or an interface. The access flags also indicate what modifiers were used in the declaration of the class or interface. Classes and interfaces can be public or abstract. Classes can be final, though final classes cannot be abstract. Interfaces can韙 be final. The bits used for the various flags are shown in Table 6-4.</P>
<P>Table 6-4. Flag bits in the <FONT FACE="Courier New">access_flags</FONT> item of <FONT FACE="Courier New">ClassFile</FONT> tables</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Flag Name</STRONG></TD><TD VALIGN="TOP"><STRONG>Value</STRONG></TD><TD VALIGN="TOP"><STRONG>Meaning if Set</STRONG></TD><TD VALIGN="TOP"><STRONG>Set By</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ACC_PUBLIC</FONT></TD><TD VALIGN="TOP">0x0001</TD><TD VALIGN="TOP">Type is public</TD><TD VALIGN="TOP">Classes and interfaces</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ACC_FINAL</FONT></TD><TD VALIGN="TOP">0x0010</TD><TD VALIGN="TOP">Class is final</TD><TD VALIGN="TOP">Classes only</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ACC_SUPER</FONT></TD><TD VALIGN="TOP">0x0020</TD><TD VALIGN="TOP">Use new </TD><TD VALIGN="TOP"><FONT FACE="Courier New">invokespecial</FONT> semanticsClasses and interfaces</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ACC_INTERFACE</FONT></TD><TD VALIGN="TOP">0x0200</TD><TD VALIGN="TOP">Type is an interface, not a class</TD><TD VALIGN="TOP">All interfaces, no classes</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ACC_ABSTRACT</FONT></TD><TD VALIGN="TOP">0x0400</TD><TD VALIGN="TOP">Type is abstract</TD><TD VALIGN="TOP">All interfaces, some classes</TD></TR>
</TABLE>
<P>The <FONT FACE="Courier New">ACC_SUPER</FONT> flag exists for backwards compatibility with Sun韘 older Java compilers. In Sun韘 older Java Virtual Machines, the <FONT FACE="Courier New">invokespecial</FONT> instruction had more relaxed semantics. All new compilers should set the <FONT FACE="Courier New">ACC_SUPER</FONT> flag. All new implementations of the Java Virtual Machine should implement the newer, stricter <FONT FACE="Courier New">invokespecial</FONT> semantics. (See the <FONT FACE="Courier New">invokespecial</FONT> instruction in Appendix A for a description of these semantics.) Sun韘 older compilers generate class files with the <FONT FACE="Courier New">ACC_SUPER</FONT> flag set to zero. Sun韘 older Java Virtual Machines ignore the flag if it is set.</P>
<P>All unused bits in <FONT FACE="Courier New">access_flags</FONT> must be set to zero and ignored by Java Virtual Machine implementations.</P>
<H3><P>this_class</P>
</H3><P>The next two bytes are the <FONT FACE="Courier New">this_class</FONT> item, an index into the constant pool. The constant pool entry at position <FONT FACE="Courier New">this_class</FONT> must be a <FONT FACE="Courier New">CONSTANT_Class_info</FONT> table, which has two parts: a <FONT FACE="Courier New">tag</FONT> and a <FONT FACE="Courier New">name_index</FONT>. The tag will have the value <FONT FACE="Courier New">CONSTANT_Class</FONT>. The constant pool entry at position <FONT FACE="Courier New">name_index</FONT> will be a <FONT FACE="Courier New">CONSTANT_Utf8_info</FONT> table containing the fully qualified name of the class or interface.</P>
<P>The <FONT FACE="Courier New">this_class</FONT> item provides a glimpse of how the constant pool is used. By itself, the <FONT FACE="Courier New">this_class</FONT> item is just an index into the constant pool. When a Java Virtual Machine looks up the constant pool entry at position <FONT FACE="Courier New">this_class</FONT>, it will find an entry that identifies itself via its <FONT FACE="Courier New">tag</FONT> as a <FONT FACE="Courier New">CONSTANT_Class_info</FONT>. The Java Virtual Machine knows <FONT FACE="Courier New">CONSTANT_Class_info</FONT> entries always have an index into the constant pool, called <FONT FACE="Courier New">name_index</FONT>, following their <FONT FACE="Courier New">tag</FONT>. So the virtual machine looks up the constant pool entry at position <FONT FACE="Courier New">name_index</FONT>, where it should find a <FONT FACE="Courier New">CONSTANT_Utf8_info</FONT> entry that contains the fully qualified name of the class or interface. See Figure 6-2 for a graphical depiction of this process.</P>
<P><IMG SRC="fig6-2.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig6-2.gif" ALT="Figure 6-2"></P>
<H3><P>super_class</P>
</H3><P>Following <FONT FACE="Courier New">this_class</FONT> in the class file is the <FONT FACE="Courier New">super_class</FONT> item, another two-byte index into the constant pool. The constant pool entry at position <FONT FACE="Courier New">super_class</FONT> will be a <FONT FACE="Courier New">CONSTANT_Class_info</FONT> entry that refers to the fully qualified name of this class韘 superclass. Because the base class of every object in Java programs is the <FONT FACE="Courier New">java.lang.Object</FONT> class, the <FONT FACE="Courier New">super_class</FONT> constant pool index will be valid for every class except <FONT FACE="Courier New">Object</FONT>. For <FONT FACE="Courier New">Object</FONT>, <FONT FACE="Courier New">super_class</FONT> is a zero. For interfaces, the constant pool entry at position <FONT FACE="Courier New">super_class</FONT> is <FONT FACE="Courier New">java.lang.Object</FONT>.</P>
<H3><P>interfaces_count and interfaces</P>
</H3><P>The component that follows <FONT FACE="Courier New">super_class</FONT> starts with <FONT FACE="Courier New">interfaces_count</FONT>, a count of the number of superinterfaces directly implemented by the class or interface defined in this file. Immediately following the count is <FONT FACE="Courier New">interfaces</FONT>, an array that contains one index into the constant pool for each superinterface directly implemented by this class or interface. Each superinterface is represented by a <FONT FACE="Courier New">CONSTANT_Class_info</FONT> entry in the constant pool that refers to the fully qualified name of the interface. Only direct superinterfaces, those that appear in the <FONT FACE="Courier New">implements</FONT> clause of the class or the <FONT FACE="Courier New">extends</FONT> clause of the interface declaration, appear in this array. The superinterfaces appear in the array in the order in which they appear (left to right) in the <FONT FACE="Courier New">implements</FONT> or <FONT FACE="Courier New">extends</FONT> clause.</P>
<H3><P>fields_count and fields</P>
</H3><P>Following the interfaces component in the class file is a description of the fields declared by this class or interface. This component starts with <FONT FACE="Courier New">fields_count</FONT>, a count of the number of fields, including both class and instance variables. Following the count is a list of variable-length <FONT FACE="Courier New">field_info</FONT> tables, one for each field. (The <FONT FACE="Courier New">fields_count</FONT> indicates the number of <FONT FACE="Courier New">field_info</FONT> tables in the list.) The only fields that appear in the <FONT FACE="Courier New">fields</FONT> list are those that were declared by the class or interface defined in the file. No fields inherited from superclasses or superinterfaces appear in the <FONT FACE="Courier New">fields</FONT> list.</P>
<P>Each <FONT FACE="Courier New">field_info</FONT> table reveals information about one field. The table contains the field's name, descriptor, and modifiers. If the field is declared as final, the <FONT FACE="Courier New">field_info</FONT> table also reveals the field韘 constant value. Some of this information is contained in the <FONT FACE="Courier New">field_info</FONT> table itself, and some is contained in constant pool locations referred to by the table. The <FONT FACE="Courier New">field_info</FONT> table is described in more detail later in this chapter.</P>
<H3><P>methods_count and methods</P>
</H3><P>Following the fields in the class file is a description of the methods declared by the class or interface. This component starts with <FONT FACE="Courier New">methods_count</FONT>, a two-byte count of the number of methods in the class or interface. The count includes only those methods that are explicitly defined by this class or interface. (It does not include any methods inherited from superclasses or superinterfaces.) Following the method count are the methods themselves, described in a list of <FONT FACE="Courier New">method_info</FONT> tables. (The <FONT FACE="Courier New">methods_count</FONT> indicates the number of <FONT FACE="Courier New">method_info</FONT> tables in the list.)</P>
<P>The <FONT FACE="Courier New">method_info</FONT> table contains several pieces of information about the method, including the method韘 name and descriptor (its return type and argument types). If the method is not abstract, the <FONT FACE="Courier New">method_info</FONT> table includes the number of stack words required for the method's local variables, the maximum number of stack words required for the method's operand stack, a table of exceptions caught by the method, the bytecode sequence, and optional line number and local variable tables. If the method can throw any checked exceptions, the <FONT FACE="Courier New">method_info</FONT> table includes a list of those checked exceptions. The <FONT FACE="Courier New">method_info</FONT> table is described in detail later in this chapter.</P>
<H3><P>attributes_count and attributes</P>
</H3><P>The last component in the class file are the attributes, which give general information about the particular class or interface defined by the file. The attributes component starts with <FONT FACE="Courier New">attributes_count</FONT>, a count of the number of <FONT FACE="Courier New">attribute_info</FONT> tables appearing in the subsequent <FONT FACE="Courier New">attributes</FONT> list. The first item in each <FONT FACE="Courier New">attribute_info</FONT> table is an index into the constant pool of a <FONT FACE="Courier New">CONSTANT_Utf8_info</FONT> table that gives the attribute韘 name.</P>
<P>Attributes come in many varieties. Several varieties are defined by the Java Virtual Machine specification, but anyone can create their own varieties of attributes (following certain rules) and place them into class files. Java Virtual Machine implementations must silently ignore any attributes they don韙 recognize. The rules surrounding the creation of new varieties of attributes are described later in this chapter.</P>
<P>Attributes appear in several places in the class file, not just in the <FONT FACE="Courier New">attributes</FONT> item of the top-level <FONT FACE="Courier New">ClassFile</FONT> table. The attributes that appear in the <FONT FACE="Courier New">ClassFile</FONT> table give more information about the class or interface define by the file. Attributes that give more information about a field may be included as part of <FONT FACE="Courier New">field_info</FONT> table. Attributes that give more information about a method may be included as part of a <FONT FACE="Courier New">method_info</FONT> table.</P>
<P>The Java Virtual Machine specification defines two kinds of attributes that may appear in the attributes list of the <FONT FACE="Courier New">ClassFile</FONT> table: <FONT FACE="Courier New">SourceCode</FONT> and <FONT FACE="Courier New">InnerClasses</FONT>. These two attributes are described in detail later in this chapter.</P>
<H3><EM><P>Special Strings</P>
</EM></H3><P>The symbolic references contained in the constant pool involve three special kinds of strings: fully qualified names, simple names, and descriptors. All symbolic references include the fully qualified name of a class or interface. Symbolic references to fields include a simple field name and field descriptor in addition to a fully qualified type name. Symbolic references to methods include a simple method name and method descriptor in addition to a fully qualified name.</P>
<P>The same special strings are used to describe the class or interface that is defined by the class file. The class or interface name, the superclass name (if any), and the names of any superinterfaces are all given as fully qualified names. For each field declared by the class or interface, the constant pool contains a simple name and field descriptor. For each method declared by the class or interface, the constant pool contains a simple name and method descriptor.</P>
<H3><P>Fully Qualified Names</P>
</H3><P>Whenever constant pool entries refer to classes and interfaces, they give the fully qualified name of the class or interface. In the class file, fully qualified names have their dots replaced with slashes. For example, the representation of the fully qualified name of <FONT FACE="Courier New">java.lang.Object</FONT> in the class file is <FONT FACE="Courier New">java/lang/Object</FONT>. The fully qualified name of <FONT FACE="Courier New">java.util.Hashtable</FONT> in the class file is <FONT FACE="Courier New">java/util/Hashtable</FONT>.</P>
<H3><P>Simple Names</P>
</H3><P>The names of fields and methods appear in constant pool entries as simple (not fully qualified) names. For example, a constant pool entry that refers to the <FONT FACE="Courier New">String</FONT> <FONT FACE="Courier New">toString()</FONT> method of class <FONT FACE="Courier New">java.lang.Object</FONT> would give its method name as <FONT FACE="Courier New">"toString"</FONT>. A constant pool entry that refers to the <FONT FACE="Courier New">java.io.PrintStream</FONT> <FONT FACE="Courier New">out</FONT> field of class <FONT FACE="Courier New">java.lang.System</FONT> would specify the field name simply as <FONT FACE="Courier New">"out"</FONT>.</P>
<H3><P>Descriptors</P>
</H3><P>Symbolic references to fields and methods include a descriptor string in addition to a fully qualified class or interface name and a simple field or method name. A field descriptor gives the field韘 type. A method descriptor gives the method韘 return type and the number and types of the method韘 parameters.</P>
<P>Field and method descriptors are defined by the context free grammar shown below. Nonterminals of this grammar, such as <I>FieldType</I>, are shown in italic font. Terminals, such as <FONT FACE="Courier New">B</FONT> or <FONT FACE="Courier New">V</FONT>, are shown in fixed width font. The asterisk character (*) stands for zero or more occurrences of the item that precedes it placed side by side (with no intervening white space).</P>
<P><H4><EM>FieldDescriptor:</P></EM></H4>
<P><I>FieldType</P></EM></H4>
<P><H4><EM>ComponentType:</P></EM></H4>
<P><I>FieldType</P></EM></H4>
<P><H4><EM>FieldType:</P></EM></H4>
<P><I>BaseType</P></EM></H4>
<P><I>ObjectType</P></EM></H4>
<P><I>ArrayType</P></EM></H4>
<P><H4><EM>BaseType:</P></EM></H4>
<P><FONT FACE="Courier New">B</FONT></P>
<P><FONT FACE="Courier New">C</FONT></P>
<P><FONT FACE="Courier New">D</FONT></P>
<P><FONT FACE="Courier New">F</FONT></P>
<P><FONT FACE="Courier New">I</FONT></P>
<P><FONT FACE="Courier New">J</FONT></P>
<P><FONT FACE="Courier New">S</FONT></P>
<P><FONT FACE="Courier New">Z</FONT></P>
<P><H4><EM>ObjectType:</P></EM></H4>
<P><FONT FACE="Courier New">L<classname</FONT>>;</P>
<P><H4><EM>ArrayType:</P></EM></H4>
<P><FONT FACE="Courier New">[</FONT> <I>ComponentType</P></EM></H4>
<P><H4><EM>ParameterDescriptor:</P></EM></H4>
<P><I>FieldType</P></EM></H4>
<P><H4><EM>MethodDescriptor:</P></EM></H4>
<P><FONT FACE="Courier New">(</FONT> <I>ParameterDescriptor*</I> <FONT FACE="Courier New">)</FONT> <I>ReturnDescriptor</P></EM></H4>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -