当前位置:网站首页>In depth study of JVM bottom layer (IV): class file structure
In depth study of JVM bottom layer (IV): class file structure
2022-07-02 06:48:00 【I think starfish_ ninety-eight】
List of articles
Class file structure
summary
Every line of code we write , To run on the machine , Eventually, it needs to be compiled into binary machine code CPU To recognize . But because of the virtual machine , Shield the operating system from CPU Differences in instruction sets , Be similar to Java This kind of programming language based on virtual machine usually Compiled into an intermediate format file for storage , For example, what we're going to talk about today Bytecode (ByteCode) file .
The cornerstone of irrelevance
Java The designer of virtual machine considered and implemented other languages in the beginning of design Java The possibility of running on a virtual machine . So it's not just Java Language can run in Java Virtual machine , today Such as Kotlin、Groovy、Jython、JRuby Wait for a lot of JVM Language can be in Java Running on a virtual machine . They and Java All languages are compiled into bytecode files by the compiler , And then it's done by the virtual machine . So class files ( Bytecode file ) Have language independence .
Class Class file structure
Class The document is a set of With 8 Bit byte is the base unit of binary stream , The data are arranged in strict order Class In file , There is no separator in between , This makes the whole Class Almost all the contents stored in the file are necessary data for the program to run , There is no gap . When you need to occupy 8 When the data item in the space above the bit byte , It will be divided into several in the way of high position first 8 Bit byte for storage .
Java Virtual machine specification Class The file format is similar to C The pseudo structure of a language structure to store data , There are only two data types in this pseudo structure : Unsigned numbers and tables :
- An unsigned number : It's a basic data type , 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 according to UTF-8 The string value formed by the encoding structure .
- surface : A composite data type consisting of multiple unsigned numbers or other tables as data items , All watches are used to 「_info」 ending . Tables are used to describe data in a hierarchical composite structure , Whole Class A document is a table , It consists of the data items shown in the table below .
Pictured ,Class The bytes stored in the file are arranged in strict accordance with the order in the table above . Which byte represents what meaning , What is the length , The sequence is strictly limited , No change is allowed .
Magic number and Class Version of a file
Every Class Header of file 4 Bytes are called magic numbers (Magic Number), Its only function is to determine whether the file can be received by the virtual machine Calss file . The reason why magic numbers are used instead of file suffixes is mainly based on security considerations , Because the file suffix can be changed at will .Class The magic value of the file is 「0xCAFEBABE」.
Next to the magic count 4 One byte stores Class Version number of the document : The first 5 And the 6 Two bytes are minor version numbers (Minor Version), The first 7 And the 8 Byte is the major version number (Major Version). The high version of the JDK Can be downward compatible with lower versions of Class file , The virtual machine refused to execute more than its version number Class file .
Constant pool
After the major version number is the constant pool entry , Constant pools can be understood as Class The repository of resources in the file , It is Class The most data types associated with other projects in the file structure , It's also occupation Class One of the largest data projects in file space , It's the same thing Class The first table type data item in the file .
There are two main types of constants in the constant pool : Literal and Symbol reference
- Literal : Close to Java The concept of constant at the linguistic level , Such as a string 、 Declare as final Constant value of .
- Symbol reference : The concept of compilation principle , The following three types of constants are included
- Fully qualified names of classes and interfaces
- Name and descriptor of the field
- The name and descriptor of the method
Access signs
The two bytes following the access pool represent (access_flag), This flag is used to identify some class or interface level access information , Including this Class Class or interface ; Is it defined as public type ; Is it defined as abstract type ; If it's a class , Whether it is declared as final etc. . See the table below for the specific sign position and the meaning of the sign :
access_flags There's a total of 16 Flags can be used , At present, only one of them is defined 8 individual , All unused flag bits are required to be 0.
Class index 、 Parent index and interface index set
Class index (this_class) And parent index (super_class) It's all one u2 Data of type , And the interface index set (interfaces) It's a group. u2 Type of data set ,Class These three data are used to determine the inheritance relationship of this class in the file .
- The class index is used to determine the fully qualified name of this class
- The parent index is used to determine the fully qualified name of the parent of this class
- The interface index collection is used to describe which interfaces the class implements
Field table set
Field table set (field_info) Used to describe variables declared in an interface or class . Field (field) Including class variables and instance variables , But excluding local variables declared inside methods . Let's look at the structure of the field table :
The field modifier is placed in access_flags in , It's related to access_flag Very similar , It's all one u2 Data type of :
Method table set
- Class The description of the method in the document is completely consistent with the description of the field , The structure of the method table is the same as that of the field table .
- because volatile Key words and transient Keywords cannot modify methods , So there is no access flag in the method table ACC_VOLATILE and ACC_TRANSIENT. In contrast ,synchronizes、native、strictfp and abstract Keywords can modify methods , So the access flag of the method table is added ACC_SYNCHRONIZED、ACC_NATIVE、ACC_STRICTFP and ACC_ABSTRACT sign .
Property sheet set
stay Class file 、 Field table 、 All method tables can carry their own attribute sheets (attribute_info) aggregate , Used to describe specific information about certain scenarios .
The property sheet collection doesn't look like Class Other data items in the file are so strict , The order of attribute sheets is not mandatory , And as long as it does not duplicate the existing property name , Any compiler implemented by anyone can write their own defined attribute information to the attribute table ,Java The virtual machine will omit the properties it doesn't know at runtime .
边栏推荐
猜你喜欢
Usage of map and foreach in JS
Fe - wechat applet - Bluetooth ble development research and use
web自动中利用win32上传附件
Latex error: the font size command \normalsize is not defined problem solved
Linux MySQL 5.6.51 community generic installation tutorial
The table component specifies the concatenation parallel method
Vscode installation, latex environment, parameter configuration, common problem solving
Huawei mindspire open source internship machine test questions
Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
uniapp引入本地字体
随机推荐
微信小程序基础
FE - Weex 使用简单封装数据加载插件为全局加载方法
Summary of advertisement business bug replay
js的防抖和节流
默认google浏览器打不开链接(点击超链接没有反应)
MySQL index
Solution to the black screen of win computer screenshot
Latex在VSCODE中编译中文,使用中文路径问题解决
工具种草福利帖
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Self study table Au
Thread hierarchy in CUDA
Sublime Text 配置php编译环境
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Underlying mechanism mvcc
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
ctf三计
Build learning tensorflow
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Sentry construction and use