1、javap
通过javac
和javap
查看:先通过javac
将.java
文件编译成.class
字节码文件,然后通过javap -verbose
分析字节码。
(base) tinytongtongdeMacBook-Pro% javac JVMRunTimeStorageTest.java
(base) tinytongtongdeMacBook-Pro% javap -verbose JVMRunTimeStorageTest
这样你就能看到你的字节码信息了。
示例如下:
Classfile /Users/wangjianzhou/Documents/workspace/java/JavaStudy/src/com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest.class
Last modified Mar 16, 2022; size 657 bytes
MD5 checksum a13778df0b853b554f2a5da237c53672
Compiled from "JVMRunTimeStorageTest.java"
public class com.tinytongtong.javastudy.jvm.JVMRunTimeStorageTest
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #5.#21 // java/lang/Object."<init>":()V
#2 = Double 2.0d
#4 = Class #22 // com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest
#5 = Class #23 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 main
#11 = Utf8 ([Ljava/lang/String;)V
#12 = Utf8 add
#13 = Utf8 (I)I
#14 = Utf8 add1
#15 = Utf8 add2
#16 = Utf8 ()I
#17 = Utf8 foreach
#18 = Utf8 StackMapTable
#19 = Utf8 SourceFile
#20 = Utf8 JVMRunTimeStorageTest.java
#21 = NameAndType #6:#7 // "<init>":()V
#22 = Utf8 com/tinytongtong/javastudy/jvm/JVMRunTimeStorageTest
#23 = Utf8 java/lang/Object
{
public com.tinytongtong.javastudy.jvm.JVMRunTimeStorageTest();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 8: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=0, locals=1, args_size=1
0: return
LineNumberTable:
line 12: 0
public static int add(int);
descriptor: (I)I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: iload_0
8: iadd
9: ireturn
LineNumberTable:
line 28: 0
line 29: 2
line 30: 4
public static int add1(int);
descriptor: (I)I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=5, args_size=1
0: lconst_1
1: lstore_1
2: ldc2_w #2 // double 2.0d
5: dstore_3
6: lload_1
7: l2d
8: dload_3
9: dadd
10: iload_0
11: i2d
12: dadd
13: d2i
14: ireturn
LineNumberTable:
line 42: 0
line 43: 2
line 44: 6
public static int add2();
descriptor: ()I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=0
0: iconst_1
1: istore_0
2: iconst_2
3: istore_1
4: iload_0
5: iload_1
6: iadd
7: istore_2
8: iload_2
9: bipush 10
11: iadd
12: ireturn
LineNumberTable:
line 55: 0
line 56: 2
line 57: 4
line 58: 8
public static int foreach(int);
descriptor: (I)I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_2
5: iload_0
6: if_icmpge 19
9: iload_1
10: iload_2
11: iadd
12: istore_1
13: iinc 2, 1
16: goto 4
19: iload_1
20: ireturn
LineNumberTable:
line 70: 0
line 71: 2
line 72: 9
line 71: 13
line 74: 19
StackMapTable: number_of_entries = 2
frame_type = 253 /* append */
offset_delta = 4
locals = [ int, int ]
frame_type = 250 /* chop */
offset_delta = 14
}
SourceFile: "JVMRunTimeStorageTest.java"
2、ASM Bytecode Outline 插件
给IDE安装ASM Bytecode Outline插件。
安装好之后,重启IDE。
接着选择对应的java文件,右键
--> Show Bytecode outline
,可以看到ASM的输出,如下图所示
3、jclasslib Bytecode Viewer
我们可以通过这个插件,以可视化的方式查看字节码中的信息,比如方法Code
表中的局部变量表
信息。如下图所示:
打开对应的java文件,View
--> Show Bytecode With Jclasslib
我们可以看到局部变量表
的长度。