📄 appa.htm
字号:
super class: Class: java/lang/Object<BR> interfaces: none<BR> fields: [1] st_one<BR> methods: [4] <init> <init>do_countdown do_countdown<BR> attributes: SourceFile(test.java)<BR></TT></BLOCKQUOTE><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The above output was generated with a Java tool I wrote called ClassView. The full source code is available on this book's CD-ROM. ClassView parses and outputs the various features and levels of a given class file. It also provides a good basis for other class file parsers, such as disassemblers.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The magic number of a class is 0xcafebabe. This number must appearin a class file, or the file is assumed to be invalid. The currentmajor version is 45; the current minor version is 3.<H4>Access Flags</H4><P>Access flags are used throughout the class file to convey theaccess characteristics of various items. The flag itself is acollection of 11 individual bits. Table A.1 lays out the masks.<BR><P><CENTER><B>Table A.1. Access flag bit values.</B></CENTER><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=193><I>Flag Value</I></TD><TD WIDTH=265><I>Indication</I></TD></TR><TR><TD WIDTH=193><TT>ACC_PUBLIC = 0x0001</TT></TD><TD WIDTH=265>Global visibility</TD></TR><TR><TD WIDTH=193><TT>ACC_PRIVATE = 0x0002</TT></TD><TD WIDTH=265>Local class visibility</TD></TR><TR><TD WIDTH=193><TT>ACC_PROTECTED = 0x0004</TT></TD><TD WIDTH=265>Subclass visibility</TD></TR><TR><TD WIDTH=193><TT>ACC_STATIC = 0x0008</TT></TD><TD WIDTH=265>One occurrence in system (not per class)</TD></TR><TR><TD WIDTH=193><TT>ACC_FINAL = 0x0010</TT></TD><TD WIDTH=265>No changes allowed</TD></TR><TR><TD WIDTH=193><TT>ACC_SYNCHRONIZED = 0x0020</TT></TD><TD WIDTH=265>Access with a monitor</TD></TR><TR><TD WIDTH=193><TT>ACC_VOLATILE = 0x0040</TT></TD><TD WIDTH=265>No local caching</TD></TR><TR><TD WIDTH=193><TT>ACC_TRANSIENT = 0x0080</TT></TD><TD WIDTH=265>Not a persistent value</TD></TR><TR><TD WIDTH=193><TT>ACC_NATIVE = 0x0100</TT></TD><TD WIDTH=265>Native method implementation</TD></TR><TR><TD WIDTH=193><TT>ACC_INTERFACE = 0x0200</TT></TD><TD WIDTH=265>Class is an interface</TD></TR><TR><TD WIDTH=193><TT>ACC_ABSTRACT = 0x0400</TT></TD><TD WIDTH=265>Class or method is abstract</TD></TR></TABLE></CENTER><P><P>Access flags are present for a class and its fields and methods.Only a subset of values appears in any given item. Some bits applyonly to fields (VOLATILE and TRANSIENT); others apply only tomethods (SYNCHRONIZED and NATIVE).<H4>Attributes</H4><P>Attributes, like access flags, appear throughout a class file.They have the following form:<BLOCKQUOTE><TT>GenericAttribute_info<BR>{<BR> u2 attribute_name;<BR> u4 attribute_length;<BR> u1 info[attribute_length];<BR>}</TT></BLOCKQUOTE><P>A generic structure exists to enable loaders to skip over attributesthey don't understand. The actual attribute has a unique structurethat can be read if the loader understands the format. As an example,the following structure specifies the format of a source fileattribute:<BLOCKQUOTE><TT>SourceFile_attribute<BR>{<BR> u2 attribute_name;<BR> u4 attribute_length;<BR> u2 sourcefile_index;<BR>}</TT></BLOCKQUOTE><P>The name of an attribute is an index into the constant pool. You'lllearn about constant pools momentarily. If a loader did not understandthe source file attribute structure, it could skip the data byreading the number of bytes specified in the length parameter.For the source file attribute, the length is 2.<H4>Constant Pool</H4><P>The constant pool forms the basis for all numbers and stringswithin a class file. Nowhere else do you ever find strings ornumbers in a class file. Any time there is a need to referencea string or number, an index into the constant pool is substituted.Consequently, the constant pool is the dominant feature of a class.The pool is even used directly within the virtual machine itself.<P>There are twelve different types of constants:<UL><LI><TT>CONSTANT_Utf8</TT> = 1<LI><TT>CONSTANT_Unicode</TT> = 2<LI><TT>CONSTANT_Integer</TT> = 3<LI><TT>CONSTANT_Float</TT> = 4<LI><TT>CONSTANT_Long</TT> = 5<LI><TT>CONSTANT_Double</TT> = 6<LI><TT>CONSTANT_Class</TT> = 7<LI><TT>CONSTANT_String</TT> = 8<LI><TT>CONSTANT_Fieldref</TT> = 9<LI><TT>CONSTANT_Methodref</TT> =10<LI><TT>CONSTANT_InterfaceMethodref</TT>= 11<LI><TT>CONSTANT_NameAndType</TT>= 12</UL><P>Each constant structure leads off with a tag identifying the structuretype. Following the type is data specific to each individual structure.The layout of each constant structure follows:<BLOCKQUOTE><TT>CONSTANT_Utf8_info<BR>{<BR> u1 tag;<BR> u2 length;<BR> u1 bytes[length];<BR>}<BR><BR>CONSTANT_Unicode_info<BR>{<BR> u1 tag;<BR> u2 length;<BR> u2 words[length];<BR>}<BR><BR>CONSTANT_Integer_info<BR>{<BR> u1 tag;<BR> u4 bytes;<BR>}<BR><BR>CONSTANT_Float_info<BR>{<BR> u1 tag;<BR> u4 bytes;<BR>}<BR><BR>CONSTANT_Long_info<BR>{<BR> u1 tag;<BR> u4 high_bytes;<BR> u4 low_bytes;<BR>}<BR><BR>CONSTANT_Double_info<BR>{<BR> u1 tag;<BR> u4 high_bytes;<BR> u4 low_bytes;<BR>}<BR><BR>CONSTANT_Class_info<BR>{<BR> u1 tag;<BR> u2 name_index;<BR>}<BR><BR>CONSTANT_String_info<BR>{<BR> u1 tag;<BR> u2 string_index;<BR>}<BR><BR>CONSTANT_Fieldref_info<BR>{<BR> u1 tag;<BR> u2 class_index;<BR> u2 name_and_type_index;<BR>}<BR><BR>CONSTANT_Methodref_info<BR>{<BR> u1 tag;<BR> u2 class_index;<BR> u2 name_and_type_index;<BR>}<BR><BR>CONSTANT_InterfaceMethodref_info<BR>{<BR> u1 tag;<BR> u2 class_index;<BR> u2 name_and_type_index;<BR>}<BR><BR>CONSTANT_NameAndType_info<BR>{<BR> u1 tag;<BR> u2 name_index;<BR> u2 signature_index;<BR>}</TT></BLOCKQUOTE><P>The <TT>CONSTANT_Utf8</TT> containsstandard ASCII text strings. These are not null-terminated becausethey use an explicit length parameter. Notice that most of theconstants reference other constants for information. Methods,for instance, specify a class and type by providing indexes toother constant pool members. Constant pool cross-references eliminaterepetition of data.<P>The constant pool for the test class appears as follows:<BLOCKQUOTE><TT>String #28 -> Performing countdown:<BR>Class: java/lang/System<BR>Class: java/lang/Object<BR>Class: test<BR>Class: java/io/PrintStream<BR>Field: java/lang/System.out Ljava/io/PrintStream;<BR>Field: test.st_one I<BR>Method: java/io/PrintStream.println(I)V<BR>Method: java/lang/Object.<init>()V<BR>Method: java/io/PrintStream.println(Ljava/lang/String;)V<BR>NameAndType: println (I)V<BR>NameAndType: println (Ljava/lang/String;)V<BR>NameAndType: out Ljava/io/PrintStream;<BR>NameAndType: <init> ()V<BR>NameAndType: st_one I<BR>Utf8: [7] println<BR>Utf8: [4] (I)V<BR>Utf8: [3] ()I<BR>Utf8: [13] ConstantValue<BR>Utf8: [4] (I)I<BR>Utf8: [19] java/io/PrintStream<BR>Utf8: [10] Exceptions<BR>Utf8: [15] LineNumberTable<BR>Utf8: [1] I<BR>Utf8: [10] SourceFile<BR>Utf8: [14] LocalVariables<BR>Utf8: [4] Code<BR>Utf8: [21] Performing countdown:<BR>Utf8: [3] out<BR>Utf8: [21] (Ljava/lang/String;)V<BR>Utf8: [16] java/lang/Object<BR>Utf8: [6] <init><BR>Utf8: [21] Ljava/io/PrintStream;<BR>Utf8: [16] java/lang/System<BR>Utf8: [12] do_countdown<BR>Utf8: [5] ([I)Z<BR>Utf8: [6] st_one<BR>Utf8: [7] getData<BR>Utf8: [9] test.java<BR>Utf8: [3] ()V<BR>Utf8: [4] test<BR></TT></BLOCKQUOTE><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The ClassView tool substitutes pool indexes with actual pool data whenever possible.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H4>Fields</H4><P>Field structures contain the individual data members of a class.Any class item that is not a method is placed into the fieldssection. The field structure looks like the following:<BLOCKQUOTE><TT>field_info<BR>{<BR> u2 access_flags;<BR> u2 name_index;<BR> u2 signature_index;<BR> u2 attribute_count;<BR> attribute_info attributes[attribute_count];<BR>}</TT></BLOCKQUOTE><P>The test class contains the following field:<BLOCKQUOTE><TT>st_one I<BR> PUBLIC STATIC<BR> No attributes</TT></BLOCKQUOTE><H4>Methods</H4><P>The method section contains all of the executable content of aclass. In addition to the method name and signature, the structurecontains a set of attributes. One of these attributes has theactual byte codes that the virtual machine will execute. The methodstructure follows:<BLOCKQUOTE><TT>method_info<BR>{<BR> u2 access_flags;<BR> u2 name_index;<BR> u2 signature_index;<BR> u2 attributes_count;<BR> attribute_info attributes[attribute_count];<BR>}</TT></BLOCKQUOTE><P>The test class produces the following method section:<BLOCKQUOTE><TT><init>()V<BR> PUBLIC<BR> Code(stack=1 locals=1 code=10 exceptions=none)<BR><init>(I)V<BR> PUBLIC<BR> Code(stack=1 locals=2 code=9 exceptions=none)<BR>getData([I)Z<BR> PUBLIC NATIVE<BR> No attributes<BR>do_countdown()I<BR> PUBLIC<BR> Code(stack=2 locals=2 code=33 exceptions=none)<BR>do_countdown(I)I<BR> PUBLIC<BR> Code(stack=2 locals=3 code=29 exceptions=none)</TT></BLOCKQUOTE><P>Each method has a name and signature. You learned about signaturesin <A HREF="ch10.htm" >Chapter 10</A>, "Native Methods andJava." Each non-native method contains a code attribute thathas the following format:<BLOCKQUOTE><TT>Code_attribute<BR>{<BR> u2 attribute_name;<BR> u4 attribute_length;<BR> u2 max_stack;<BR> u2 max_locals;<BR> u4 code_length;<BR> u1 code[code_length];<BR> u2 exception_table_length;<BR> ExceptionItem exceptions[exception_table_length];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -