当前位置:网站首页>Class file structure

Class file structure

2021-01-08 09:08:06 Murmur without saying a word


Part of this paper is excerpted from 《 In depth understanding of Java Virtual machine Third Edition 》


summary

We know ,Java Cross platform , Its implementation is based on virtual machine and bytecode storage format .Java Not with virtual machines Java Language binding , Only with Class The file is associated with .Java Virtual machine as a universal 、 Machine independent execution platform , Any language can translate Java Virtual machines are the basis of their operation , With Class Documents serve as the delivery medium for their products .

Class Documents are a set of 8 Binary stream based on bytes , The data items are arranged in strict order in the file , There's no separator in the middle , This makes the whole Class The content stored in the file is almost all the necessary data for the program to run . When it comes to occupying 8 When there are more than bytes of data items , Then it will be divided into a number of 8 Bytes for storage .

Class There are two types of data in the file , They are non-conforming numbers and tables :

  • Unsigned numbers are basic data types , With u1、u2、u4、u8 To represent respectively 1 Bytes 、2 Bytes 、4 Bytes and 8 An unsigned number of bytes , Unsigned numbers can be used to describe numbers 、 Index reference 、 Quantity value or UTF-8 The encoding constitutes a string value
  • A table is a composite data type composed of multiple unsigned numbers or other tables as data items , General with _info ending . Tables are used to describe data in a hierarchical composite structure , Whole Class A document can also be seen as a table in essence

Whether it's an unsigned number or a table , When it is necessary to describe multiple data of the same type but with variable quantity , Often use the form of a front-end capacity counter plus several consecutive data items , In this case, a series of continuous data of a certain type is called a collection of a certain type .

Here is Class File format :

type name Number
u4 magic 1
u2 minor_version 1
u2 major_version 1
u2 constant_pool_count 1
cp_info constant_pool constant_pool_count - 1
u2 access_flags 1
u2 this_class 1
u2 super_class 1
u2 interfaces_count 1
u2 interfaces interfaces_count
u2 fields_count 1
field_info fields fields_count
u2 methods_count 1
method_info methods methods_count
u2 attribute_count 1
attribute_info attributes attributes_count

Magic sum Class Document version

Class Header of file 4 Bytes are called magic numbers (Magic Number), Its only function is to make sure that Class Whether the file can be accepted by the virtual machine , Its value is 0xCAFEBABE( Coffee baby ).

Next to the magic count 4 One byte stores Class Version number of the document : The first 5 And the 6 Byte is the minor version number (Minor Version), The first 7 And the 8 Byte is the major version number (Major Version).Java Version number from 45 Start , After each JDK When a large version is released, the main version number is added with 1. The high version of the JDK Can be downward compatible with previous versions of Class file , But you can't run later versions of Class file .


Constant pool

And then the Lord 、 The next version number is the constant pool entry , Constant pool entry needs to put an item u2 Data of type , Represents the constant pool capacity meter value (constant_pool_count), This capacity count is from 1 Not from 0 Start , The first 0 Items are used to express “ Don't reference any constant pool project ” The meaning of .Class Only the capacity count of the constant pool in the file structure is from 1 Start , Everything else comes from 0 Start .

There are two main types of constants in the constant pool : Literal and symbolic references . The amount of words is close to Java The concept of constant at the linguistic level , Such as text string 、 Be declared final Constant value of . And symbolic reference belongs to the concept of compilation principle , It mainly includes the following types of constants :

  • Packages exported or opened by modules (Package)
  • Fully qualified names of classes and interfaces (Fully Qualified Name)
  • Name and descriptor of the field (Descriptor)
  • The name and descriptor of the method
  • Method handle and method type (Method Handle、Method Type、Invoke Dynamic)
  • Dynamic call points and dynamic constants (Dynamically-Computed Call Site、Dynamically-Computed Constant)

Java Will load on the virtual machine Class File dynamic connection , Convert symbolic references to real memory entries . Every constant in the constant pool is a table , At first there was 11 Different data structures , Later, in order to better support dynamic language calls , additional 4 Constants related to dynamic languages , Later, in order to support Java modularization , Joined again 2 Constant , So the deadline JDK13, Constant table has 17 There are different types of constants . this 17 Class tables all have one thing in common , The first bit of the table structure is a u1 Flags of type (tag)

17 The specific meanings of the constant types are shown in the table :

type sign describe
CONSTANT_Utf8_info 1 UTF-8 Encoded string
CONSTANT_Integer_info 3 Integer literal
CONSTANT_Float_info 4 Floating point literal
CONSTANT_Long_info 5 Long integer literal quantity
CONSTANT_Double_info 6 Double precision floating point literal
CONSTANT_Class_info 7 A symbolic reference to a class or interface
CONSTANT_String_info 8 String type literal
CONSTANT_Fieldref_info 9 Symbol reference for field
CONSTANT_Methodref_info 10 Symbolic references to methods in class
CONSTANT_InterfaceMethodref_info 11 Symbolic references to methods in interfaces
CONSTANT_NameAndType_info 12 A partial symbol reference to a field or method
CONSTANT_MethodHandle_info 15 Represents a method handle
CONSTANT_MethodType_info 16 Represents the type of method
CONSTANT_Dynamic_info 17 Represents a dynamically computed constant
CONSTANT_InvokeDynamic_info 18 Represents a dynamic method call point
CONSTANT_Moudle_info 19 Represents a module
CONSTANT_Package_info 20 Represents an open or exported package in a module

Access signs

After the constant pool ends , The next 2 Bytes represent access flags (access_flags), This flag is used to identify access information at some class or interface level , Including this Class Class or interface ; Is it defined as public type ; Is it defined as abstract type ; If it's a class , Is it declared as final wait . And the specific meaning of the tag :

Logo name Flag value meaning
ACC_PUBLIC 0x0001 Is it Public type
ACC_FINAL 0x0010 Is it declared as final, Only classes can set
ACC_SUPER 0x0020 Is it allowed to use invokespecial New meaning of bytecode instruction
ACC_INTERFACE 0x0200 Mark this is an interface
ACC_ABSTRACT 0x0400 Is it abstract type , For interfaces or abstract classes , Secondary flag value is true , Other types are false
ACC_SYNTHETIC 0x1000 Flag this class is not generated by user code
ACC_ANNOTATION 0x2000 Mark this is an annotation
ACC_ENUM 0x4000 Flag this is an enumeration

access_flags There's a total of 16 Flags can be used , At present, only one of them is defined 9 individual , All unused flag bits are required to be zero .


Class index 、 Parent index and interface index set

Class index (this_class)、 Parent index (super_class) And interface index (interfaces) All in order after the access flag , Class index and parent index refer to two u2 The index value of type represents , And the interface index is a set of u2 A collection of data of type .

A class reference is used to determine the fully qualified name of the class , The parent class index determines the fully qualified name of the parent class of the class , because Java Multiple inheritance not allowed , So there's only one parent index ,Object The parent class index of class is 0. The class index and the parent index each point to a CONSTANT_Class_info Class descriptor constant for , Through this index value, we can find the definition in CONSTANT_Utf8_info A fully qualified name string in a constant of type .

For interface index sets , The first entry u2 The data of type is interface counter (interfaces_count), Indicates the capacity of the index table , If the class does not implement any interfaces , Then the value of the counter is 0, The index table of the subsequent interface no longer takes up any bytes .


Field table set method table set

Field table set (field_info) Used to describe variables declared in an interface or class , Including class level variables and instance level variables , But not including local variables declared inside methods . The field contains the scope of the field to be specified (public、private、protected)、 Instance variable or class variable (static)、 variability (final) wait . This information either has , Or not , It's very suitable to use flag bits to represent , And the field is called , Defined why data types , These are not fixed , Can only be described by constants in the constant pool .

The field table structure is as follows :

type name Number
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

access_flags Used to identify field modifiers (public、static、final、volatile ...),name_index and descriptor_index All references to constant pools , Represents the simple name of the field and the descriptor of the field and method respectively . After that is the property sheet collection , Used to store some extra information .

The field table collection does not list fields inherited from the parent class or interface , But it's possible that the original Java Fields not in the code , For example, in order to maintain the accessibility of internal classes to external classes , The compiler automatically adds fields to the external class instance .

The method table set is almost exactly the same as the field table set , Only among the options available for the flag and property sheet collection . As for the code in the method , After compilation, it is stored in the method property table collection with a name of Code In the properties of .


版权声明
本文为[Murmur without saying a word]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210106162321395R.html

随机推荐