Javap 命令参数解析
示例
将下面的demo经过反编译之后的效果。
1 package com.meituan.mtlearner.cheetah.service.test;
2
3 public class CommonTest {
4 private String name;
5
6 public static void main(String[] args) throws Exception{
7 String a = "222";
8 System.out.println(a);
9 throw new Exception();
10 }
11 }
① test javap -v CommonTest结果:
test javap -v CommonTest //和javap -verbose一样,输出额外信息
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Classfile /Users/shichangmin/workspace/cheetah/target/classes/com/meituan/mtlearner/cheetah/service/test/CommonTest.class
Last modified May 21, 2018; size 727 bytes
MD5 checksum 7110663fa12a58f7d74917d709a67c66
Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #8.#26 // java/lang/Object."<init>":()V
#2 = String #27 // 222
#3 = Fieldref #28.#29 // java/lang/System.out:Ljava/io/PrintStream;
#4 = Methodref #30.#31 // java/io/PrintStream.println:(Ljava/lang/String;)V
#5 = Class #32 // java/lang/Exception
#6 = Methodref #5.#26 // java/lang/Exception."<init>":()V
#7 = Class #33 // com/meituan/mtlearner/cheetah/service/test/CommonTest
#8 = Class #34 // java/lang/Object
#9 = Utf8 name
#10 = Utf8 Ljava/lang/String;
#11 = Utf8 <init>
#12 = Utf8 ()V
#13 = Utf8 Code
#14 = Utf8 LineNumberTable
#15 = Utf8 LocalVariableTable
#16 = Utf8 this
#17 = Utf8 Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;
#18 = Utf8 main
#19 = Utf8 ([Ljava/lang/String;)V
#20 = Utf8 args
#21 = Utf8 [Ljava/lang/String;
#22 = Utf8 a
#23 = Utf8 Exceptions
#24 = Utf8 SourceFile
#25 = Utf8 CommonTest.java
#26 = NameAndType #11:#12 // "<init>":()V
#27 = Utf8 222
#28 = Class #35 // java/lang/System
#29 = NameAndType #36:#37 // out:Ljava/io/PrintStream;
#30 = Class #38 // java/io/PrintStream
#31 = NameAndType #39:#40 // println:(Ljava/lang/String;)V
#32 = Utf8 java/lang/Exception
#33 = Utf8 com/meituan/mtlearner/cheetah/service/test/CommonTest
#34 = Utf8 java/lang/Object
#35 = Utf8 java/lang/System
#36 = Utf8 out
#37 = Utf8 Ljava/io/PrintStream;
#38 = Utf8 java/io/PrintStream
#39 = Utf8 println
#40 = Utf8 (Ljava/lang/String;)V
{
public com.meituan.mtlearner.cheetah.service.test.CommonTest();
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 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;
public static void main(java.lang.String[]) throws java.lang.Exception;
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: ldc #2 // String 222
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
10: new #5 // class java/lang/Exception
13: dup
14: invokespecial #6 // Method java/lang/Exception."<init>":()V
17: athrow
LineNumberTable: //代码中的行数对应code中的序号,例如示例代码的第7行(String a = "222";)对应Code块中的0号(idc #2 //String 222)
line 7: 0
line 8: 3
line 9: 10
LocalVariableTable: //局部变量表,列出了所有栈帧中的局部变量
Start Length Slot Name Signature
0 18 0 args [Ljava/lang/String;
3 15 1 a Ljava/lang/String;
Exceptions:
throws java.lang.Exception
}
SourceFile: "CommonTest.java"
②test javap -l CommonTest结果:
test javap -l CommonTest
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest {
public com.meituan.mtlearner.cheetah.service.test.CommonTest();
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/meituan/mtlearner/cheetah/service/test/CommonTest;
public static void main(java.lang.String[]) throws java.lang.Exception;
LineNumberTable:
line 7: 0
line 8: 3
line 9: 10
LocalVariableTable:
Start Length Slot Name Signature
0 18 0 args [Ljava/lang/String;
3 15 1 a Ljava/lang/String;
③test javap -c CommonTest结果:
test javap -c CommonTest
警告: 二进制文件CommonTest包含com.meituan.mtlearner.cheetah.service.test.CommonTest
Compiled from "CommonTest.java"
public class com.meituan.mtlearner.cheetah.service.test.CommonTest {
public com.meituan.mtlearner.cheetah.service.test.CommonTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.Exception;
Code:
0: ldc #2 // String 222
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
10: new #5 // class java/lang/Exception
13: dup
14: invokespecial #6 // Method java/lang/Exception."<init>":()V
17: athrow
通过反编译我们能够看到代码真实运行时的具体步骤是什么,结合字节码指令集的含义能够更清晰的感受这种背后的故事,很有意思