本人学习过程中所整理的代码,源码地址
public class MyTest2 {
String str = "Welcome";
private int x = 5;
public static Integer in = 10;
public static void main(String[] args) {
MyTest2 myTest2 = new MyTest2();
myTest2.setX(0);
in = 20;
}
private void setX(int x) {
this.x = x;
}
}
Classfile /G:/GitHub/jvm-study/build/classes/java/main/bytecode/MyTest2.class
Last modified 2020-3-19; size 814 bytes
MD5 checksum baffcdf4a49b347cecf3122ddf8455e2
Compiled from "MyTest2.java"
public class bytecode.MyTest2
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #10.#34 // java/lang/Object."":()V
#2 = String #35 // Welcome
#3 = Fieldref #5.#36 // bytecode/MyTest2.str:Ljava/lang/String;
#4 = Fieldref #5.#37 // bytecode/MyTest2.x:I
#5 = Class #38 // bytecode/MyTest2
#6 = Methodref #5.#34 // bytecode/MyTest2."":()V
#7 = Methodref #5.#39 // bytecode/MyTest2.setX:(I)V
#8 = Methodref #40.#41 // java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
#9 = Fieldref #5.#42 // bytecode/MyTest2.in:Ljava/lang/Integer;
#10 = Class #43 // java/lang/Object
#11 = Utf8 str
#12 = Utf8 Ljava/lang/String;
#13 = Utf8 x
#14 = Utf8 I
#15 = Utf8 in
#16 = Utf8 Ljava/lang/Integer;
#17 = Utf8 <init>
#18 = Utf8 ()V
#19 = Utf8 Code
#20 = Utf8 LineNumberTable
#21 = Utf8 LocalVariableTable
#22 = Utf8 this
#23 = Utf8 Lbytecode/MyTest2;
#24 = Utf8 main
#25 = Utf8 ([Ljava/lang/String;)V
#26 = Utf8 args
#27 = Utf8 [Ljava/lang/String;
#28 = Utf8 myTest2
#29 = Utf8 setX
#30 = Utf8 (I)V
#31 = Utf8 <clinit>
#32 = Utf8 SourceFile
#33 = Utf8 MyTest2.java
#34 = NameAndType #17:#18 // "":()V
#35 = Utf8 Welcome
#36 = NameAndType #11:#12 // str:Ljava/lang/String;
#37 = NameAndType #13:#14 // x:I
#38 = Utf8 bytecode/MyTest2
#39 = NameAndType #29:#30 // setX:(I)V
#40 = Class #44 // java/lang/Integer
#41 = NameAndType #45:#46 // valueOf:(I)Ljava/lang/Integer;
#42 = NameAndType #15:#16 // in:Ljava/lang/Integer;
#43 = Utf8 java/lang/Object
#44 = Utf8 java/lang/Integer
#45 = Utf8 valueOf
#46 = Utf8 (I)Ljava/lang/Integer;
{
java.lang.String str;
descriptor: Ljava/lang/String;
flags:
private int x;
descriptor: I
flags: ACC_PRIVATE
public static java.lang.Integer in;
descriptor: Ljava/lang/Integer;
flags: ACC_PUBLIC, ACC_STATIC
public bytecode.MyTest2();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: aload_0
5: ldc #2 // String Welcome
7: putfield #3 // Field str:Ljava/lang/String;
10: aload_0
11: iconst_5
12: putfield #4 // Field x:I
15: return
LineNumberTable:
line 7: 0
line 9: 4
line 11: 10
LocalVariableTable:
Start Length Slot Name Signature
0 16 0 this Lbytecode/MyTest2;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: new #5 // class bytecode/MyTest2
3: dup
4: invokespecial #6 // Method "":()V
7: astore_1
8: aload_1
9: iconst_0
10: invokespecial #7 // Method setX:(I)V
13: bipush 20
15: invokestatic #8 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
18: putstatic #9 // Field in:Ljava/lang/Integer;
21: return
LineNumberTable:
line 16: 0
line 18: 8
line 20: 13
line 21: 21
LocalVariableTable:
Start Length Slot Name Signature
0 22 0 args [Ljava/lang/String;
8 14 1 myTest2 Lbytecode/MyTest2;
private void setX(int);
descriptor: (I)V
flags: ACC_PRIVATE
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: iload_1
2: putfield #4 // Field x:I
5: return
LineNumberTable:
line 24: 0
line 25: 5
LocalVariableTable:
Start Length Slot Name Signature
0 6 0 this Lbytecode/MyTest2;
0 6 1 x I
static {
};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: bipush 10
2: invokestatic #8 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: putstatic #9 // Field in:Ljava/lang/Integer;
8: return
LineNumberTable:
line 13: 0
}
SourceFile: "MyTest2.java"
访问标志信息包括了该class文件是类还是接口,是否被定义成public,是否是abstract,如果是类,是否被定义成final
方法的属性结构:
方法中的每个属性都是一个attribute_info结构:
//attribute_info格式:
attribute_info{
//attribute_name_index值为code,则为Code结构Code的作用是保存该方法的结构,所对应的的字节码
u2 attribute_name_index;
//表示attribute所包含的字节数,不包含attribute_name_index和attribute_length
u4 attribute_length;
u1 info[attribute_length]
}
public class MyTest3 {
public void test() {
try {
FileInputStream fis = new FileInputStream("test.txt");
ServerSocket serverSocket = new ServerSocket(9999);
serverSocket.accept();
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (Exception e) {
} finally {
System.out.println("finally");
}
}
}
0 new #2 <java/io/FileInputStream>
3 dup
4 ldc #3 <test.txt>
6 invokespecial #4 <java/io/FileInputStream.<init>>
9 astore_1
10 new #5 <java/net/ServerSocket>
13 dup
14 sipush 9999
17 invokespecial #6 <java/net/ServerSocket.<init>>
20 astore_2
21 aload_2
22 invokevirtual #7 <java/net/ServerSocket.accept>
25 pop
26 getstatic #8 <java/lang/System.out>
29 ldc #9 <finally>
31 invokevirtual #10 <java/io/PrintStream.println>
34 goto 92 (+58)
37 astore_1
38 aload_1
39 invokevirtual #12 <java/io/FileNotFoundException.printStackTrace>
42 getstatic #8 <java/lang/System.out>
45 ldc #9 <finally>
47 invokevirtual #10 <java/io/PrintStream.println>
50 goto 92 (+42)
53 astore_1
54 aload_1
55 invokevirtual #14 <java/io/IOException.printStackTrace>
58 getstatic #8 <java/lang/System.out>
61 ldc #9 <finally>
63 invokevirtual #10 <java/io/PrintStream.println> //第一次出现
66 goto 92 (+26)
69 astore_1
70 getstatic #8 <java/lang/System.out>
73 ldc #9 <finally>
75 invokevirtual #10 <java/io/PrintStream.println> //第二次出现
78 goto 92 (+14)
81 astore_3
82 getstatic #8 <java/lang/System.out>
85 ldc #9 <finally>
87 invokevirtual #10 <java/io/PrintStream.println> //第三次出现
90 aload_3
91 athrow
92 return