In-depth understanding of the class file analysis of the Java virtual machine

Introduction

This article is a source code analysis compiled by the reference book "In-depth Understanding of the Java Virtual Machine (2nd Edition)".
Since the compilation of the class file of the book is very different from the one I compiled, I have referred to a blog post to analyze it, and wrote relevant notes, I hope it will be helpful to you.
Finally, I also put a link to the original blog post, the source code and the Class file involved, to avoid that you use jdk to compile it differently from mine.

Class file analysis

CA FE BA BE // 魔数
00 00 00 34 // 版本52(对应jdk1.8)
00 13 // 对应19,即18个常量(1-18)
// 常量池
// 第1个常量开始
0A // 第#1类型:CONSTANT_Methodref_info
00 04 // name_index: #4
00 0F // name_index: #15
09 // 第#2类型:CONSTANT_Fieldref_info
00 03 // name_index: #3
00 10 // name_index: #16
07 // 第#3类型:CONSTANT_Class_info
00 11 // name_index: #17
07 // 第#4类型:CONSTANT_Class_info
00 12 // name_index: #18
01 // 第#5类型:CONSTANT_Utf8_info
00 01 // length: 1
6D // 对应字符串:m
01 // 第#6类型:CONSTANT_Utf8_info
00 01 // length: 1
49 // 对应字符串:I
01 // 第#7类型:CONSTANT_Utf8_info
00 06 // length: 6
3C 69 6E 69 74 3E // 字符串:<init>
01 // 第#8类型:CONSTANT_Utf8_info
00 03 // length: 3
28 29 56 // 字符串:()V
01 // 第#9类型:CONSTANT_Utf8_info
00 04 // length: 4
43 6F 64 65 // 字符串:Code
01 // 第#10类型:CONSTANT_Utf8_info
00 0F // length: 15
4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65 // 字符串:LineNumberTable
01 // 第#11类型:CONSTANT_Utf8_info
00 03 // length: 3
69 6E 63 // 字符串:inc
01 // 第#12类型:CONSTANT_Utf8_info
00 03 // length: 3
28 29 49 // 字符串:()I
01 // 第#13类型:CONSTANT_Utf8_info
00 0A // length: 10
53 6F 75 72 63 65 46 69 6C 65 // SourceFile
01 // 第#14类型:CONSTANT_Utf8_info
00 0E // length: 14
54 65 73 74 43 6C 61 73 73 2E 6A 61 76 61 // 字符串:TestClass.java
0C // 第#15类型:CONSTANT_NameAndType_info
00 07 // name_index: #7
00 08 // name_index: #8
0C // 第#16类型:CONSTANT_NameAndType_info
00 05 // name_index: #5
00 06 // name_index: #6
01 // 第#17类型:CONSTANT_Utf8_info
00 1D // length: 29
6F 72 67 2F 66 65 6E 69 78 73 6F 66 74 2F 63 6C 61 7A 7A 2F 54 65 73 74 43 6C 61 73 73 // 字符串:org/fenixsoft/clazz/TestClass
01 // 第#18类型:CONSTANT_Utf8_info
00 10 // length: 16
6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 // 字符串:java/lang/Object
00 21 // 访问标志。0x0021 == 0x0001|0x0020,因此是 public class 类
// 类索引、父类索引与接口索引集合
00 03 // 类索引#3
00 04 // 父类索引#4
00 00 // 接口集合大小:0
// 字段表集合 field_info
00 01 // 字段的数量:1
00 02 // 访问控制:private
00 05 // name_index: #5
00 06 // descriptor_index: #6 (代表基本类型int)
00 00 // attribute_info
// 方法表集合 method_info
00 02 // 方法数量:2
// 第一个方法:<init>()
00 01 // 访问控制:public
00 07 // name_index: #7 (<init>方法)
00 08 // descriptor_index: #8
00 01 // 属性数量:1
// 属性表集合 attribute_info
00 09 // name_index: #9 (Code)
00 00 00 1D // attribute_length: 29
00 01 // max_stack
00 01 // max_locals
00 00 00 05 // code_length
2A B7 00 01 B1 // code
00 00 // exception_table_length
00 01 // attributes_count
// 属性表集合 attribute_info
00 0A // name_index: #10 (LineNumberTable)
00 00 00 06 00 01 00 00 00 03 
// 第二个方法:inc()
00 01 // 访问控制:public
00 0B // name_index: #11
00 0C // descriptor_index: #12
00 01 // 属性数量:1
// 属性表集合 attribute_info
00 09 // name_index: #9 (Code)
00 00 00 1F // attribute_length: 31
00 02 // max_stack
00 01 // max_locals
00 00 00 07 // code_length
2A B4 00 02 04 60 AC // code
00 00 // exception_table_length
00 01 // attributes_count
00 0A // name_index: #10 (LineNumberTable)
00 00 00 06 00 01 00 00 00 08 00 01 00 0D 00 00 00 02 00 0E

[Involved tools]
WinHex
JDK1.8

[Reference code]
Source code and compiled files

[References] In-
depth understanding of Java virtual machine (2nd edition)
ASCII code and hexadecimal conversion comparison table In-
depth understanding of Java virtual machine (3)-Class file structure

Guess you like

Origin blog.csdn.net/fomeiherz/article/details/104055799
Recommended