JVM Serial X (Class file structure).

A, Class file structure

JDK version number has reached 14, the change with respect to other aspects of language, the API and Java technology system, Class file structure has been in a relatively stable state, the number of the main structure and semantics of the Class file byte code instructions little there have been changes.

Class file is a set of 8-bit binary stream in units of bytes based on respective data items are arranged compactly in the exact order in the Class file, without adding any intermediate separators, which makes the contents of the entire file is stored in Class almost all data necessary to run the program, there is no gap.

Under the Java Virtual Machine Specification, Class file format structure of a C-like pseudo language structure to store data, such dummy structures are only two types of data: unsigned and tables:

Unsigned: In u1, u2, u4, u8 to unsigned number representing 1 byte, 2 bytes, 4 bytes and 8 bytes, unsigned number can be used to describe figures, reference index , the number of values ​​or string value configured according to UTF-8 encoding.

Table: Number of symbols to a plurality of non-table, or other data type as a composite composed of data items, all tables are habitual "_info" end, is used to describe a composite data structure of the hierarchy is essentially the entire file is Class a table.

The following is a Class File Format:

Types of name Quantity description
u4 magic 1 It indicates whether the file can be accepted as a virtual machine Class file
u2 minor_version 1 Minor version number
u2 major_version 1 The major version number, Java version number from 45 starts
u2 constant_pool_count 1 The constant pool count value Capacity
cp_info constant_pool constant_pool_count-1 The constant pool is a Class file resource warehouse, the main storage of two categories constants: literal and symbolic references (see Definition below)
u2 access_flags 1 Identify the class / interface hierarchy of access to information, such as: This Class is a class or an interface, whether the pubilc, such as whether the final
u2 this_class 1 Class index for determining the fully qualified name of the class
u2 super_class 1 Parent index for determining the fully qualified name of the parent class of the class
u2 interfaces_count 1 The number of interfaces implemented in the index set
u2 interfaces interfaces_count Used to describe this class implements interfaces which
u2 fields_count 1 Number of fields
field_info fields fields_count Description interface or variables declared in the class, including the class instance variables and class variables Level
u2 methods_count 1 Quantitative Methods
method_info methods methods_count The method described interface or declared in the class, instance, and method comprising a class-level scale method
u2 attributes_count 1 Number of Properties
attribute_info attributes attributes_count Description Field / method table additional information, such as specific code Code for storing, for storing constants ConstantValue like

Literal refers to the Java language text strings, declared as a constant value final and so on. The symbolic references belong to the concept of compiler theory aspects, including the following three constants:

  • The fully qualified name of the class and interface
  • Symbol name or description field
  • Name or description of the method identifier

Two, Class file byte code

public class TestClass {

    private int m;

    public int inc() {
        return m + 1;
    }
}

We TestClass.class file by outputting content -verbose parameter JAVA_HOME / bin / javap tool for analyzing Class file byte code.

javap -verbose TestClass
 Classfile /D:/JMCui/jvm-demo/demo/target/classes/org/jvm/demo/chapter6/TestClass.class
     Last modified 2020-4-1; size 397 bytes
     MD5 checksum 291f52e2b746bf6c338ece68fdf3dc08
     Compiled from "TestClass.java"
     public class org.jvm.demo.chapter6.TestClass
     minor version: 0
     major version: 52
     flags: ACC_PUBLIC, ACC_SUPER
     Constant                                                                          :
     #1 = Methodref          #4.#18         // java/lang/Object."<init>":()V
     #2 = Fieldref           #3.#19         // org/jvm/demo/chapter6/TestClass.m:I
     #3 = Class              #20            // org/jvm/demo/chapter6/TestClass
     #4 = Class              #21            // java/lang/Object
     #5 = Utf8               m
     #6 = Utf8               I
     #7 = Utf8               <init>
     #8 = Utf8               ()V
     #9 = Utf8               Code
     #10 = Utf8               LineNumberTable
     #11 = Utf8               LocalVariableTable
     #12 = Utf8               this
     #13 = Utf8               Lorg/jvm/demo/chapter6/TestClass;
     #14 = Utf8               inc
     #15 = Utf8               ()I
     #16 = Utf8               SourceFile
     #17 = Utf8               TestClass.java
     #18 = NameAndType        #7:#8          // "<init>":()V
     #19 = NameAndType        #5:#6          // m:I
     #20 = Utf8               org/jvm/demo/chapter6/TestClass
     #21 = Utf8               java/lang/Object
     {
     public org.jvm.demo.chapter6.TestClass();
     descriptor: ()V
     flags: ACC_PUBLIC
     Code:
     stack=1, locals=1, args_size=1
     0: aload_0
     1: invokespecial #1                  // Method java/lang/Object."<init>":()V
     4: return
     LineNumberTable:
     line 7: 0
     LocalVariableTable:
     Start  Length  Slot  Name   Signature
     0       5     0  this   Lorg/jvm/demo/chapter6/TestClass;

     public int inc();
     descriptor: ()I
     flags: ACC_PUBLIC
     Code:
     stack=2, locals=1, args_size=1
     0: aload_0
     1: getfield      #2                  // Field m:I
     4: iconst_1
     5: iadd
     6: ireturn
     LineNumberTable:
     line 12: 0
     LocalVariableTable:
     Start  Length  Slot  Name   Signature
     0       7     0  this   Lorg/jvm/demo/chapter6/TestClass;
     }
     SourceFile: "TestClass.java"

Above Minor Version , Major Version , flags , Constant these meanings are better understood.

Code property is the most important property of a Class file, if the information is a Java program into the code (Code, which method body Java code) and metadata (the Metadata, including classes, fields, methods and other information defined) two parts, the entire Class file, Code description Code attribute, all the other data items are used to describe metadata.

Stack , about locals content Code attribute, Stack stack operands; represents about locals local variable table storage space belongs, the unit is Slot, Slot machine virtual local variables allocated to a minimum unit of memory used.

args_size represents the number of parameters, the above reason is args_size 1, because there will be at least a pointer to the current instance of the object local variable local variable table example of the method, the local variable table is also set aside a first bit Slot to store a reference to the object instance.

LineNumberTable used to describe Java source code line numbers corresponding relationship between the line number (offset byte code) bytecode.

LocalVariableTable used to describe the relationship between variables in the stack frame of the local variable table variables defined in Java source code. LocalVariableTypeTable generic type for recording characteristic signature (the Signature) information.

SourceFile records generated source code file name of the Class file.

aload_0 , invokespecial , return the focus of these belongs to the bytecode instructions, is not described in this article, related to the bytecode instructions, and other attribute information can be read "Java Virtual Machine Specification."

Guess you like

Origin www.cnblogs.com/jmcui/p/12627180.html