📄 chap06.html
字号:
<!-- All material contained herein is copyright (c) McGraw-Hill Professional Books
All Rights Reserved. No use of this material may be made without express written
permission of the copyright holder. HTML conversions by Mega Space [barry@megaspace.com] -->
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Understanding Digital Signatures: Inside the Java Virtual Machine
by Bill Venners - Beta Version</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap05.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap05.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap07.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap07.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
<TR><TD COLSPAN="2"><FONT FACE="ARIEL,HELVETICA" SIZE="-1"><I>© 1997 The McGraw-Hill Companies, Inc. All rights reserved. <BR>Any use of this Beta Book is subject to the rules stated in the <A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" target="_top">Terms of Use</A>.</I></FONT><br>
<script language="javascript">
document.write("<a href='http://banners.linkbuddies.com/click.php?id=237296'><img src='http://banners.linkbuddies.com/image.php?id=237296&ref=" + document.referrer + "' width=468 height=60 alt='Click Here' border=0></a>");
</script></TD></TR>
</TABLE>
<HR>
<P><H1>Chapter Six</H1></P>
<P><H2>The Java Class File</H2></P>
<P>The previous chapter, the first of Part II, "Java Internals," gave an overview of the Java Virtual Machine. The next four chapters will focus on different aspects of the Java Virtual Machine. This chapter takes a look at the Java class file. It describes the contents of the class file, including the structure and format of the constant pool. This chapter serves as a complete reference of the Java class file format.</P>
<P>Accompanying this chapter on the CD-ROM is an applet that interactively illustrates the material presented in the chapter. The applet, named <I>Getting Loaded</I>, simulates the Java Virtual Machine loading a Java class file. At the end of this chapter, you will find a description of this applet and an instructions on how to use it.</P>
<H3><EM><P>What is a Java Class File?</P>
</EM></H3><P>The Java class file is a precisely defined binary file format for Java programs. Each Java class file represents a complete description of one Java class or interface. There is no way to put more than one class or interface into a single class file. The precise definition of the class file format ensures that any Java class file can be loaded and correctly interpreted by any Java Virtual Machine, no matter what system produced the class file or what system hosts the virtual machine.</P>
<P>Although the class file is related to the Java language architecturally, it is not inextricably linked to the Java language. As shown in Figure 6-1, you could write programs in other languages and compile them to class files, or you could compile your Java programs to a different binary file format. Nevertheless, most Java programmers will likely use the class file as the primary vehicle for delivering their programs to Java Virtual Machines.</P>
<P><IMG SRC="fig6-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig6-1.gif" ALT="Figure 6-1"></P>
<P>As mentioned in earlier chapters, the Java class file is a binary stream of 8-bit bytes. Data items are stored sequentially in the class file with no padding between adjacent items. The lack of padding helps keep class files compact. Items that occupy more than one byte are split up into several consecutive bytes that appear in <I>big-endian</I> (higher bytes first) order.</P>
<P>Just as your Java classes can contain varying numbers of fields, methods, method parameters, local variables, and so on, the Java class file can contain many items that vary in size or number from one class file to another. In the class file, the size or length of a variable-length item precedes the actual data for the item. This allows class file streams to be parsed from beginning to end, reading in the size of an item first followed by the item data.</P>
<H3><EM><P>What's in a Class File?</P>
</EM></H3><P>The Java class file contains everything a Java Virtual Machine needs to know about one Java class or interface. The remainder of this chapter describes the class file format using tables. Each table has a name and shows an ordered list of items that can appear in a class file. Items appear in the table in the order in which they appear in the class file. Each item has a type, a name, and a count. The type is either a table name or one of the "primitive types" shown in Table 6-1. All values stored in items of type <FONT FACE="Courier New">u2</FONT>, <FONT FACE="Courier New">u4</FONT>, and <FONT FACE="Courier New">u8</FONT> appear in the class file in big-endian order.</P>
<P>Table 6-1. Class file "primitive types"</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u1</FONT></TD><TD VALIGN="TOP">a single unsigned byte</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP">two unsigned bytes</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u4</FONT></TD><TD VALIGN="TOP">four unsigned bytes</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u8</FONT></TD><TD VALIGN="TOP">eight unsigned bytes</TD></TR>
</TABLE>
<P>The major components of the class file, in their order of appearance in the class file, are shown in Table 6-2. Each of these components is described in more detail below.</P>
<P>Table 6-2. Format of a <FONT FACE="Courier New">ClassFile</FONT> Table</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Type&</STRONG></TD><TD VALIGN="TOP"><STRONG>#9;Name</STRONG></TD><TD VALIGN="TOP"><STRONG>Count</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u4</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">magic</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">minor_version</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">major_version</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">constant_pool_count</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">cp_info</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">constant_pool</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">constant_pool_count</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">- 1</FONT></TD><TD VALIGN="TOP"></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">access_flags</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">this_class</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">super_class</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">interfaces_count</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">interfaces</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">interfaces_count</FONT></TD><TD VALIGN="TOP"></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">fields_count</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">field_info</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">fields</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">fields_count</FONT></TD><TD VALIGN="TOP"></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">methods_count</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">method_info</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">methods</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">methods_count</FONT></TD><TD VALIGN="TOP"></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">u2</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">attributes_count</FONT></TD><TD VALIGN="TOP">1</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">attribute_info</FONT></TD><TD VALIGN="TOP"><FONT FACE="Courier New">attributes</FONT></TD><TD VALIGN="TOP"> <FONT FACE="Courier New">attributes_count</FONT></TD><TD VALIGN="TOP"></TD></TR>
</TABLE>
<H3><P>magic</P>
</H3><P>The first four bytes of every Java class file are its <I>magic number</I>, 0xCAFEBABE. The magic number makes non-Java class files easier to identify. If a file doesn韙 start with 0xCAFEBABE, it definitely isn韙 a Java class file. A magic number can be chosen by a file format韘 designers to be any arbitrary number that isn韙 already in widespread use. The magic number for the Java class file was chosen back in the days when "Java" was called "Oak." According to Patrick Naughton, a key member of the original Java team, the magic number was chosen "long before the name Java was ever uttered in reference to this language. We were looking for something fun, unique, and easy to remember. It is only a coincidence that 0xCAFEBABE, an oblique reference to the cute baristas at Peet's Coffee, was foreshadowing for the name Java."</P>
<H3><P>minor_version and major_version</P>
</H3><P>The second four bytes of the class file contain the minor and major version numbers. As Java technology evolves, new features may occasionally be added to the Java class file format. Each time the class file format changes, the version numbers will change as well. To the Java Virtual Machine, the version numbers identify the format to which a particular class file adheres. Java Virtual Machines will generally be able to load class files with a given major version number and a range of minor version numbers. Java Virtual Machines must reject class files with version numbers outside their valid range.</P>
<P>For class files generated by 1.0 or 1.1 compilers, the major version number is 45. The minor version number is 3.</P>
<H3><P>constant_pool_count and constant_pool</P>
</H3><P>Following the magic and version numbers in the class file is the <I>constant pool</I>. As mentioned in Chapter 5, "The Java Virtual Machine," the constant pool contains the constants associated with the class or interface defined by the file. Constants such as literal strings, final variable values, class names, and method names are stored in the constant pool. The constant pool is organized as a list of entries. A count of the number of entries in the list, <FONT FACE="Courier New">constant_pool_count</FONT>, precedes the actual list, <FONT FACE="Courier New">constant_pool</FONT>.</P>
<P>Many entries in the constant pool refer to other entries in the constant pool, and many items that follow the constant pool in the class file refer back to entries in the constant pool. Throughout the class file, constant pool entries are referred to by the integer index that indicates their position in the <FONT FACE="Courier New">constant_pool</FONT> list. The first entry in the list has an index of one, the second has an index of two, and so on. Although there is no entry in the <FONT FACE="Courier New">constant_pool</FONT> list that has an index of zero, the missing zeroeth entry is included in the <FONT FACE="Courier New">constant_pool_count</FONT>. For example, if a <FONT FACE="Courier New">constant_pool</FONT> list includes fourteen entries (with indexes one through fourteen), the <FONT FACE="Courier New">constant_pool_count</FONT> would be fifteen.</P>
<P>Each constant pool entry starts with a one-byte tag that indicates the type of constant making its home at that position in the list. Once a Java Virtual Machine grabs and interprets this tag, it knows what to expect after the tag. Table 6-3 shows the names and values of the constant pool tags.</P>
<P>Table 6-3. Constant pool tags</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Entry Type</STRONG></TD><TD VALIGN="TOP"><STRONG>Tag Value</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Utf8</FONT></TD><TD VALIGN="TOP">1</TD><TD VALIGN="TOP">A UTF-8 encoded Unicode string</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Integer</FONT></TD><TD VALIGN="TOP">3</TD><TD VALIGN="TOP">An <FONT FACE="Courier New">int</FONT> literal value</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Float</FONT></TD><TD VALIGN="TOP">4</TD><TD VALIGN="TOP">A <FONT FACE="Courier New">float</FONT> literal value</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Long</FONT></TD><TD VALIGN="TOP">5</TD><TD VALIGN="TOP">A <FONT FACE="Courier New">long</FONT> literal value</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">CONSTANT_Double</FONT></TD><TD VALIGN="TOP">6</TD><TD VALIGN="TOP">A <FONT FACE="Courier New">double</FONT> literal value</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -