Java class file structure

Source: Internet
Author: User
Tags field table

Java class file structure
After a week of in-depth understanding of the Java Virtual Machine, I can see Chapter 6 today. Recently, I have been enjoying the pleasure of accepting new knowledge. I can't help but feel the importance of basic knowledge, and I have learned a lot! This part of class file structure is very practical and has a very important help group for analyzing java code. Therefore, it is necessary to take notes. On the basis of reference books, the bytecode of a piece of code is completely parsed.
The structure of the class file is as follows:

ClassFile {u4 magic;u2 minor_version;u2 major_version;u2 constant_pool_count;cp_info constant_pool[constant_pool_count-1];u2 access_flags;u2 this_class;u2 super_class;u2 interfaces_count;u2 interfaces[interfaces_count];u2 fields_count;field_info fields[fields_count];u2 methods_count;method_info methods[methods_count];u2 attributes_count;attribute_info attributes[attributes_count];}
Compile the following code to generate a class file.
package org.fenixsoft.clazz;public class Test {private int m;public int inc(){return m+1;}}

The content of the corresponding class file is as follows:

 

cafe babe 0000 0033 0016 0700 0201 00186f72 672f 6665 6e69 7873 6f66 742f 636c617a 7a2f 5465 7374 0700 0401 0010 6a617661 2f6c 616e 672f 4f62 6a65 6374 0100016d 0100 0149 0100 063c 696e 6974 3e010003 2829 5601 0004 436f 6465 0a00 03000b0c 0007 0008 0100 0f4c 696e 654e 756d6265 7254 6162 6c65 0100 124c 6f63 616c5661 7269 6162 6c65 5461 626c 6501 00047468 6973 0100 1a4c 6f72 672f 6665 6e697873 6f66 742f 636c 617a 7a2f 5465 73743b01 0003 696e 6301 0003 2829 4909 00010013 0c00 0500 0601 000a 536f 7572 63654669 6c65 0100 0954 6573 742e 6a61 76610021 0001 0003 0000 0001 0002 0005 00060000 0002 0001 0007 0008 0001 0009 0000002f 0001 0001 0000 0005 2ab7 000a b1000000 0200 0c00 0000 0600 0100 0000 03000d00 0000 0c00 0100 0000 0500 0e00 0f000000 0100 1000 1100 0100 0900 0000 31000200 0100 0000 072a b400 1204 60ac 00000002 000c 0000 0006 0001 0000 0006 000d0000 000c 0001 0000 0007 000e 000f 00000001 0014 0000 0002 0015 

 

Resolution:

 

/* Magic Number */cafe babe // magic number/* times */0000/times/* master version */0033/Master version/* constant pool number * /0016 // Number of constants, 21 (starting from 1) /* cp_info array * // 21 constants 0700 0201 109672f 6665 6e69 7873 6f66 742f 636c617a 7a2f 5465 7374 0700 0401 0010 4172f6c 616e 672f 4f62 6a65 6374 0100016d 0100 0149 0100 063c 696e 6974 5E 2829 5601 0004 436f 6465 0a00 5E 0007 0008 0100 0f4c 696e 654e 5E 7254 6162 6c65 0100 124c 6f63 5E 7269 6162 6c65 5461 626c 6501 00047468 6973 1a4c 6f72 672f 0100 4176f66 742f 636c 617a 7a2f 5465 73743b01 0003 696e 6301 0003 2829 4909 0c00 00010013 0500 0601 000a 536f 7572 63654669 6c65 0100 0954 6573 7661 742e 6a61 0021/* access flag */// access flag (Use flag space) /* category index */0001/category index/* parent Index */0003/parent index/* number of interfaces */0000 // interface index: u2 interface counter + each interface/* u2 interface array * // * field table quantity */0001 // field table quantity/* field_info type array */0002 // field access flag 0005 // simple name 0006 // descriptor 0000 // Number of attribute tables for the field --- // set of attribute tables for the field (0) /* Number of method tables */0002 // Number of method tables/* method_info array * // The first method 0001 // method access flag 0007 // simple name 0008 // descriptor 0001 // Number of attribute tables // The Attribute Table of the method (the constant is Code, methods used in the table, representing the Code compiled into bytecode instructions) 0009 // Code Attribute Table u2 type, 0000 002f // u4 type in the constant pool, specify the number of digits occupied by the attribute value: 0001 // max_stack. the maximum depth of the operand stack. Allocate the stack frame: 0001 // max_locals. The storage space required for the local variable table, in the unit of Slot, no more than 32-bit data type, occupies a Slot, 64-Bit Data occupies 2 Slot0000 0005 // code length, // code, A total of five 2a // aload_0 (push local variables of the refrence type in the 0th slots to the top of the operand stack, which is actually this) b7 // invokespecial (call a method of the stack top object: instance constructor, private method, or its parent class method) 000a // invokespecial parameter, which specifies the method to call, point to CONSTANT_Methodref_info type constant b1 // return value is void0000 // exception table length // exception information, attribute Table with a length of 0----0002 // Number of attribute tables for the code // method. In the Code Attribute Table, 000c // represents LineNumberTable, relationship between java source code and bytecode offset 0000 0006 // Attribute Table length 0001 // line number table length 0000 // line number information // start_pc0003 // line_number // attribute of the Method table, 000d // indicates LocalVariableTable in the Code Attribute Table, describes the relationship between the variables in the local variable table in the stack frame and the variables defined in the java source code: 0000, 0001, and, the length is 1 and consists of five u2 types: 0000 // start_pc. the bytecode offset starting from the lifecycle of this local variable is 0005 // length, the range covers the length of 000e // name_index, pointing to the index in the constant pool, the local variable name 000f // descriptor_index, same as above, the local variable descriptor 0000 // index, slot position of the local variable in the stack frame local variable table // The second method 0001 // access flag 0010 // simple name, inc0011 // Descriptor () i0001 // Number of attribute tables: 0009 // Code Attribute Table: 0000 0031 // u4 type, specify the number of digits occupied by the attribute value: 0002 // max_stack0001 // max_locals0000 0007 // code_length2a // aload_0 (push local variables of refrence type in 0th slots to the top of the operand stack, which is actually this) b4 // getfield, get the instance of the specified class, and press it into the stack frame 0012 // parameter, m Member of the Test class 04 // iconst_1, push int type 1 to stack top 60 // iadd, add the two int type values at the top of stack, and push the result to stack top ac // ireturn, returns int0000 // The length of the exception table ---- // the error message from the current method, length: 00002 // Number of attribute tables // LineNumberTable000c 0000 0006 0001 0000 0006 // LocalVariableTable000d 0000 000c 0001 0000 0007 0000 e 000f 0001/* Number of attribute tables */// attribute table quantity/* attribute_info array of the class */0014 // SourceFile, record the original file name 0000 0002 // The Attribute Table length 0015 // source file, Test. java

 

Attribute tables can be contained in Class files, field tables, and method tables.

In just a few lines of code, we seem so hard to use the class file bytecode tool javap provided to us by Oracle. The content of the bytecode viewed by the javap-v Test. java command is as follows:

 

Last modified 2015-12-11; size 378 bytes  MD5 checksum 0e2626e6fdb11bccf73564affa0b63e2  Compiled from "Test.java"public class org.fenixsoft.clazz.Test  SourceFile: "Test.java"  minor version: 0  major version: 51  flags: ACC_PUBLIC, ACC_SUPERConstant pool:   #1 = Class              #2             //  org/fenixsoft/clazz/Test   #2 = Utf8               org/fenixsoft/clazz/Test   #3 = Class              #4             //  java/lang/Object   #4 = Utf8               java/lang/Object   #5 = Utf8               m   #6 = Utf8               I   #7 = Utf8               
 
     #8 = Utf8               ()V   #9 = Utf8               Code  #10 = Methodref          #3.#11         //  java/lang/Object."
  
   ":()V  #11 = NameAndType        #7:#8          //  "
   
    ":()V  #12 = Utf8               LineNumberTable  #13 = Utf8               LocalVariableTable  #14 = Utf8               this  #15 = Utf8               Lorg/fenixsoft/clazz/Test;  #16 = Utf8               inc  #17 = Utf8               ()I  #18 = Fieldref           #1.#19         //  org/fenixsoft/clazz/Test.m:I  #19 = NameAndType        #5:#6          //  m:I  #20 = Utf8               SourceFile  #21 = Utf8               Test.java{  public org.fenixsoft.clazz.Test();    flags: ACC_PUBLIC    Code:      stack=1, locals=1, args_size=1         0: aload_0         1: invokespecial #10                 // Method java/lang/Object."
    
     ":()V         4: return      LineNumberTable:        line 3: 0      LocalVariableTable:        Start  Length  Slot  Name   Signature               0       5     0  this   Lorg/fenixsoft/clazz/Test;  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   Lorg/fenixsoft/clazz/Test;}
    
   
  
 

 

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.