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

Class file structure

2021-01-07 17:07:25 itread01


> Part of this paper is excerpted from 《 In depth understanding of Java Virtual machine Third Edition 》
## We know that ,Java It's cross platform , Its implementation is based on virtual machine and bit group code storage format .Java Virtual machines don't work with Java Language ties , 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 execution , With Class Archives as the delivery medium of their products .Class Files are a group of 8 A binary stream based on one byte , Each data project is arranged in the file in strict order , There is no new separator in the middle , This makes the whole Class The contents stored in the file are almost all necessary data for program execution . When it comes to occupying 8 When there are more than two bytes of data items , It will be divided into several high positions according to the way 8 Bytes .Class There are two types of data in the file , They are non-conforming numbers and tables :- The unsigned number belongs to the master data type , With u1、u2、u4、u8 To represent respectively 1 Bytes 、2 Bytes 、4 Bytes and 8 The 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 consisting of multiple unsigned numbers or other tables as data items , General with _info End . Tables are used to describe hierarchical composite structure data , The whole Class In essence, archives can also be regarded as a table, whether it is an unsigned number or a table , When it is necessary to describe multiple data of the same type but with an indefinite number , It is often used in the form of a front-end capacity counter plus several consecutive data items , In this case, we call this series of continuous data of a certain type a collection of a certain type . Here is Class File format :
Type Name Quantity
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 number sum Class File version Class The head of the file 4 Bytes are called magic numbers (Magic Number), Its only function is to determine the time Class Can files be accepted by virtual machines , Its value is 0xCAFEBABE( Coffee baby ). And then the magic number 4 Bytes store Class Version number of the file : The first 5 And the 6 Bytes are minor version numbers (Minor Version), The first 7 And the 8 Bytes are major version numbers (Major Version).Java Version number from 45 Start , After that, every JDK When a large version is released, the main version number is added with 1. The high version of the JDK Can be down compatible with previous versions of Class Archives , But you can't execute later versions of Class Archives .
## The constant pool follows the main 、 The next version number is the constant pool entry , Constant pool entry needs to put an item u2 Type data , Represents the constant pool capacity meter value (constant_pool_count), This capacity count is from 1 Not from 0 Start , The first 0 Item is used to express “ Do not reference any constant pool project ” The meaning of .Class In the file structure, only the capacity count of constant pool 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 literal amount is close to Java The concept of constant at the linguistic level , Like a string of words 、 Be declared to be final Constant value of . Symbolic reference is a concept of compiler principles , It mainly includes the following types of constants :- Packages exported or opened by modules (Package)- Fully qualified names for classes and interfaces (Fully Qualified Name)- The name and descriptor of the field (Descriptor)- The name and descriptor of the method - Method control code and method type (Method Handle、Method Type、Invoke Dynamic)- Dynamic call points and dynamic constants (Dynamically-Computed Call Site、Dynamically-Computed Constant)Java Will be loaded in the virtual machine Class The dynamic connection is made when the files are stored , Convert symbolic references to real memory entries . Every constant in a constant pool is a table , At first there was 11 There are three kinds of table structure data with different structures , Later, in order to better support dynamic language call , Added extra 4 A dynamic language related constant , Later, in order to support Java Modularity , Joined again 2 Constant , So the deadline JDK13, Constant table has 17 Different types of constants . This 17 Class tables all have one thing in common , The first place at the beginning of the table structure is u1 The flag bit of the type (tag)17 The specific meanings of these constant types are shown in table :
Type Logo 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 The number of characters of string type
CONSTANT_Fieldref_info 9 Field symbol reference
CONSTANT_Methodref_info 10 Symbolic references to methods in a class
CONSTANT_InterfaceMethodref_info 11 Symbolic references to methods in interfaces
CONSTANT_NameAndType_info 12 A partial symbolic reference to a field or method
CONSTANT_MethodHandle_info 15 Represents the method control code
CONSTANT_MethodType_info 16 Represents a method type
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

## After the access flag constant pool ends , And then 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 , Whether declared as final wait . The specific sign bit and the meaning of the sign are shown in the table :
Logo name Flag value Meaning
ACC_PUBLIC 0x0001 Is it Public Type
ACC_FINAL 0x0010 Whether declared as final, Only classes can set
ACC_SUPER 0x0020 Is it allowed to use invokespecial The new meaning of bit group code instruction
ACC_INTERFACE 0x0200 This is an interface
ACC_ABSTRACT 0x0400 Is it abstract Type , For interfaces or abstract classes , The secondary flag value is true , Other types are false
ACC_SYNTHETIC 0x1000 Flag this class is not generated by user code
ACC_ANNOTATION 0x2000 This is an annotation
ACC_ENUM 0x4000 This is an enumeration
access_flags There's a total of 16 A flag bit can be used , At present, only one of them is defined 9 One , All unused flag bits are required to be zero .
## Class index 、 Parent class index and interface index set class index (this_class)、 Parent index (super_class) And interface index (interfaces) All in order after the access flag , The class index and the parent class index refer to two u2 The index value of the 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 of Java Multiple inheritance is not allowed , So there is only one parent index ,Object The parent class index of the 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 The fully qualified string of names in a constant of type . For interface index set , 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 following interface no longer occupies 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 it doesn't include the regional variables declared inside the method . Field contains the scope of the field to be specified (public、private、protected)、 Is it an instance variable or a class variable (static)、 Variability (final) wait . All this information needs to be available , Or not , It's very suitable to use flag bits to represent , And what's the name of the field , Defined as a data type , These are not fixed , Can only be described by constants in the constant pool . The structure of the field table is as follows :| Type | Name | Quantity || -------------- | ---------------- | ---------------- || 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 a parent class or interface , But it's possible that the original Java Fields not in code , For example, in order to maintain the accessibility of internal classes to external classes , The compiler will automatically add a field 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

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210106212307822A.html

随机推荐