Java字节码文件结构剖析

Java字节码文件结构剖析

javap

javap MyTest1

image-20220605211957751

javap -c MyTest1

image-20220605212012796

javap -verbose MyTest1

结果如下

1
2
3
4
5
6
7
8
9
10
11
12
➜  bytecode git:(master) ✗ javap -c MyTest1
警告: 文件 ./MyTest1.class 不包含类 MyTest1
Compiled from "MyTest1.java"
public class com.terwergreen.jvm.bytecode.MyTest1 {
public com.terwergreen.jvm.bytecode.MyTest1();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field a:I
9: return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    public int getA();
Code:
0: aload_0
1: getfield #2 // Field a:I
4: ireturn

public void setA(int);
Code:
0: aload_0
1: iload_1
2: putfield #2 // Field a:I
5: return
}
➜ bytecode git:(master) ✗ javap -verbose MyTest1
警告: 文件 ./MyTest1.class 不包含类 MyTest1
Classfile /Users/terwer/Documents/workspace/senior-java-engineer-road/p7-skill/jvm/jvm-demo/target/classes/com/terwergreen/jvm/bytecode/MyTest1.class
Last modified 2022年3月2日; size 503 bytes
MD5 checksum 9bb025ac2880ca6faf9ee236d4d947ae
Compiled from "MyTest1.java"
public class com.terwergreen.jvm.bytecode.MyTest1
minor version: 0
major version: 51
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #3 // com/terwergreen/jvm/bytecode/MyTest1
super_class: #4 // java/lang/Object
interfaces: 0, fields: 1, methods: 3, attributes: 1
Constant pool:
#1 = Methodref #4.#20 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#21 // com/terwergreen/jvm/bytecode/MyTest1.a:I
#3 = Class #22 // com/terwergreen/jvm/bytecode/MyTest1
#4 = Class #23 // java/lang/Object
#5 = Utf8 a
#6 = Utf8 I
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/terwergreen/jvm/bytecode/MyTest1;
#14 = Utf8 getA
#15 = Utf8 ()I
#16 = Utf8 setA
#17 = Utf8 (I)V
#18 = Utf8 SourceFile
#19 = Utf8 MyTest1.java
#20 = NameAndType #7:#8 // "<init>":()V
#21 = NameAndType #5:#6 // a:I
#22 = Utf8 com/terwergreen/jvm/bytecode/MyTest1
#23 = Utf8 java/lang/Object
{
public com.terwergreen.jvm.bytecode.MyTest1();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field a:I
9: return
LineNumberTable:
line 8: 0
line 9: 4
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this Lcom/terwergreen/jvm/bytecode/MyTest1;

public int getA();
descriptor: ()I
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #2 // Field a:I
4: ireturn
LineNumberTable:
line 12: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/terwergreen/jvm/bytecode/MyTest1;

public void setA(int);
descriptor: (I)V
flags: (0x0001) ACC_PUBLIC
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: iload_1
2: putfield #2 // Field a:I
5: return
LineNumberTable:
line 16: 0
line 17: 5
LocalVariableTable:
Start Length Slot Name Signature
0 6 0 this Lcom/terwergreen/jvm/bytecode/MyTest1;
0 6 1 a I
}
SourceFile: "MyTest1.java"
➜ bytecode git:(master) ✗
作者

Terwer

发布于

2022-06-05

更新于

2022-06-05

许可协议

评论