Analyze a Java class file

Source: Internet
Author: User
Tags field table



Java source file Testclass.java:
Package Jvm.chapter6;//p166public class TestClass {private int m;public int Inc () {return m+1;}}


Show the 16 binary contents of this class file:




Analyze from the beginning
CA fe ba be:magic number;
00 00 00 34: The version number is 1.8.0;
00 16: Description constant pool has 21 constants, 1-21, index left to do his use; The following is a description of the 21 constants:
07/00 02:constant_class_info constant, the class name index is the 2nd item of the constant pool;
01/00 16/6a 6d 2f-------------------------------61 73 73: This is the 2nd item of the constant pool, constant_utf8_info constant, length is (0x0016) , it is the string constant "Jvm/chapter6/testclass";
07/00 04:constant_class_info constant, the class name index is the 4th item of the constant pool;
01/00 10/6a, 2f 6c, 6e, 2f 4f, 6a 65 63 74: This is the 4th item of the constant pool, constant_utf8_info constant, length is (0x0010), exactly the string constant "java/lang/o Bject ";
01/00 01/6d:constant_utf8_info constant, is the string constant "M";
01/00 01/49:constant_utf8_info constant, is the string constant "M";
01/00 06/3c 6e 3e:constant_utf8_info constant, is the string constant "<init>";
01/00 03/28 56:constant_utf8_info constant, is the string constant "() V";
01/00 04/43 6f 65:constant_utf8_info constant, is the string constant "Code";
0a/00 03/00 0b:tag=10, which represents the Constant_methodref_info constant, declares that the class name of the method is in the 3rd item of the constant pool, that is, Java/lang/object, method name, and type nameandtype= "< Init> ":() V;
0c/00 07/00 08: Next tag=12 is the Constant_nameandtype_info type mentioned above, and the next two indexes point to the method name and the descriptor "<init>" and "() V", respectively.
01/00 0f/4c 6e 4e 6d, the 65:constant_utf8_info constant, is the string constant "linenumbertable";
01/00 12/4c 6f, 6c, and the 65:constant_utf8_info constant, is the string constant "localvariabletable";
01/00 04/74 73:constant_utf8_info constant, is the string constant "this";
01/00 18/4C 6a 6d 2f-------------------------- /testclass; ";
01/00 6e 63:constant_utf8_info constant, is the string Constant "Inc";
01/00 49:constant_utf8_info constant, is the string constant "() I";
09/00 01/00 13: Represents the FieldRef constant, then the next two U2 represent the index of the class and nameandtype that declares the field;
0c/00 05/00 06: The Nameandtype constant that was just used above;
01/00 0a/53 6f 6c 65:constant_utf8_info constant, is the string constant "sourcefile";
0e (6c) 61::constant_utf8_info constant, which is the string constant "Testclass.java";


The fields in the constant pool are analyzed above.
XX 21:access_flags= Acc_public | Acc_super = 0x0021;
00 01 00 03 00 00: Class Index parent class index and interface index, constant pool The 3rd item represents an object, which indicates that the default parent class is object and no interface is implemented, so interfaces_count=0;
00 01/00 02/00 05/00 06/00 00: Next is the Field table collection (Fields_info), Fields_count=1,access_flags=0x0002=acc_private, which represents the field name Name_ Index=5 (the 5th item of the constant pool is the string m of the Constant_utf8_info type), the field descriptor descriptor_index=0x0006 (the 5th item of the constant pool is the string I of the Constant_utf8_info type), Finally atributes_count=0, the description does not contain additional information. This information is always in order to illustrate the defined field "private int M";


XX 02/00 01/00 07/00 08/00 01/00 09/00 xx 2f/00 01/00 01/00 xx 05/2a B7 0a B1: Next is the method set (Methods_info), there are two methods, the first The access flag for a method is 0x0001=acc_public, the name index is 0x0007, which points to <init&gt in the constant pool, the descriptor index value is 0x0008, pointing to "() V" in the constant pool, and the next property sheet counter Attributes_count =1, the property name index is 0x0009 corresponding to "code", followed by the structure of the Code attribute table: The length of the property value attribute_length=0x2f; The maximum depth of the operand stack and the capacity of the local variable table are 0x0001; bytecode instruction length is 5, translation The process of "2a B7 0a B1" is:
1) read in 2a, corresponding to the ALOAD_0, the first reference type of local variables pushed to the top of the operand stack;
2) read into the B7, corresponding to the invokespecial, the function is to call the reference type of the top of the constructor method of the object to initialize the method instance of a private method, followed by a U2 type parameter description which method is called, it points to a constant pool in a constant A constant of _methodref_info, that is, the symbolic reference of that method;
3) 0a is the parameter of the above invokespecial, pointing to a constant pool of a MethodRef constant, corresponding to the Object.init () method;
4) b1, the corresponding instruction is return, and the return type is OID, the current method corresponds to the end of the instruction.


Continue with the above analysis,
00/00 02/00 0c/00 (xx) 06/00 01/00 00/00 03|00 0d/00 xx 0c/00 01|00 00/00 05/00 0e/00 0f/00 00:exception_table_leng Th=0, the method does not throw an exception, and then attributes_count=2, the Code property has two properties: 0x000c points to "linenumbertable" in the constant pool, attribute_length=6,line_number_ Table_length=1, is a pair of Java source code line number and byte code line number corresponding relationship (3:0); 0x000d points to "localvariabletable" in the constant pool, Attribute_length=12,local_ Variable_table_length=1 (represents a stack frame and a local variable association in the program), start_pc=0x0000,length=0x0005 the scope of the local variable in the bytecode (that is, within this method), name_ index=0x000e, descriptor_index=0x000f represents the name of the local variable and its descriptor, which is "this", "Ljvm/chapter6/testclass", and the output through JAVAP can be at a glance. index=0x0000 represents the position of the local variable in the stack frame local variable table slot.


Next look at the second method,
01/00 10/00 11/00 01/00 09/00 xx 31/00 02/00 01/00 xx 07/2a b4: The access flag of the method is ac/, the name index is 16, Pointing to "Inc" in the constant pool, the descriptor index value is 0x0011, pointing to "() I" in the constant pool, the next attribute table counter Attributes_count=1, the property name index is 0x0009 corresponding to "Code", Next is the structure of the Code attribute table: The length of the property value attribute_length=0x31, the maximum depth of the operand stack is 2, the capacity of the local variable table is 1, the byte code instruction length is 7, and the process of translating "2a B4 to AC" is:
1) read in 2a, corresponding to the ALOAD_0, the first reference type of local variables pushed to the top of the operand stack;
2) read into the B4, the corresponding instruction is GetField, get the instance domain, and press into the top of the stack, he needs parameters to explain which variable into the stack, that is, below;
3) Read in 00 12, which indicates GetField which field, points to a constant pool of fieldref constant, "jvm/chapter6/testclass.m:i";
4) Read in 04, the corresponding instruction is iconst_1, the int type of 1 is pushed to the top of the stack;
5) Read in 60, the corresponding instruction is Iadd, the stack top two int type of value added and the result is pressed into the top of the stack;
6) Read into AC, the corresponding instruction is ireturn, return int from the current method, end.
The latter two properties are similar to the first method of analysis.


At this point, the analysis of a simple class file structure, a lot clearer.


Report:

Results of JAVAP Testclass.class: $ javap-verbose Testclass.class

Classfile/home/vonzhou/github/javaproject/learning-java/bin/jvm/chapter6/testclass.class
Last modified APR 29, 2015; Size 379 bytes
MD5 Checksum 70a67e773b621619d03f9d1b5ac91af6
Compiled from "Testclass.java"
public class Jvm.chapter6.TestClass
SourceFile: "Testclass.java"
Minor version:0
Major version:52
Flags:acc_public, Acc_super
Constant Pool:
#1 = Class #2; Jvm/chapter6/testclass
#2 = Utf8 Jvm/chapter6/testclass;
#3 = Class #4; Java/lang/object
#4 = Utf8 Java/lang/object;
#5 = Utf8 m;
#6 = Utf8 I;
#7 = Utf8 <init>;
#8 = Utf8 () V;
#9 = Utf8 Code;
#10 = MethodRef #3. #11; Java/lang/object. " <init> ":() V
#11 = Nameandtype #7: #8; "<init>":() V
#12 = Utf8 linenumbertable;
#13 = Utf8 localvariabletable;
#14 = Utf8 this;
#15 = Utf8 ljvm/chapter6/testclass;;
#16 = Utf8 Inc;
#17 = Utf8 () I;
#18 = Fieldref #1. #19; Jvm/chapter6/testclass.m:i
#19 = Nameandtype #5: #6; M:i
#20 = Utf8 sourcefile;
#21 = Utf8 Testclass.java;
{
public Jvm.chapter6.TestClass ();
Flags:acc_public
Code:
Stack=1, Locals=1, args_size=1
0:aload_0
1:invokespecial #10; Method java/lang/object. " <init> ":() V
4:return
Linenumbertable:
Line 3:0
Localvariabletable:
Start Length Slot Name Signature
0 5 0 This ljvm/chapter6/testclass;


public int Inc ();
Flags:acc_public
Code:
stack=2, Locals=1, args_size=1
0:aload_0
1:getfield #18; Field m:i
4:iconst_1
5:iadd
6:ireturn
Linenumbertable:
Line 6:0
Localvariabletable:
Start Length Slot Name Signature
0 7 0 This ljvm/chapter6/testclass;
}



Analyze a Java class file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.