javap定义
javap是 Java class文件分解器,可以反编译(即对javac编译的文件进行反编译),也可以查看java编译器生成的字节码。用于分解class文件。
实例类
class synchronizedTest {
private static int i = 100;
public static void main(String[] args) {
test2();
}
public synchronized void test1() {
}
public static void test2() {
System.out.println(i);
}
}
javap命令参数列表
-
javap -version:显示java版本
-
javap -p: 显示所有成员和类
-
javap -public:显示所有修饰符范围>=public的所有成员
-
javap -protected:类似于javap -public
-
javap -l: 输出行号和本地变量表
- 最重要/最强大的命令:javap -c和Java -v(java 字节码文件执行的指令,具体jvm虚拟机中的指令,可在其他地方搜索或者翻阅《深入理解java虚拟机的附录 》)
Classfile /Users/leefox/Desktop/synchronizedTest.class
Last modified 2019-1-9; size 592 bytes
MD5 checksum 00565fcec7dbd63149b0c2b40ca64e1a
Compiled from "synchronized.java"
class synchronizedTest
minor version: 0
major version: 52
flags: ACC_SUPER
Constant pool:
#1 = Methodref #7.#21 // java/lang/Object."<init>":()V
#2 = Methodref #6.#22 // synchronizedTest.test2:()V
#3 = Fieldref #23.#24 // java/lang/System.out:Ljava/io/PrintStream;
#4 = Fieldref #6.#25 // synchronizedTest.i:I
#5 = Methodref #26.#27 // java/io/PrintStream.println:(I)V
#6 = Class #28 // synchronizedTest
#7 = Class #29 // java/lang/Object
#8 = Utf8 i
#9 = Utf8 I
#10 = Utf8 <init>
#11 = Utf8 ()V
#12 = Utf8 Code
#13 = Utf8 LineNumberTable
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 test1
#17 = Utf8 test2
#18 = Utf8 <clinit>
#19 = Utf8 SourceFile
#20 = Utf8 synchronized.java
#21 = NameAndType #10:#11 // "<init>":()V
#22 = NameAndType #17:#11 // test2:()V
#23 = Class #30 // java/lang/System
#24 = NameAndType #31:#32 // out:Ljava/io/PrintStream;
#25 = NameAndType #8:#9 // i:I
#26 = Class #33 // java/io/PrintStream
#27 = NameAndType #34:#35 // println:(I)V
#28 = Utf8 synchronizedTest
#29 = Utf8 java/lang/Object
#30 = Utf8 java/lang/System
#31 = Utf8 out
#32 = Utf8 Ljava/io/PrintStream;
#33 = Utf8 java/io/PrintStream
#34 = Utf8 println
#35 = Utf8 (I)V
{
synchronizedTest();
descriptor: ()V
flags:
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 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: invokestatic #2 // Method test2:()V
3: return
LineNumberTable:
line 5: 0
line 6: 3
public synchronized void test1();
descriptor: ()V
flags: ACC_PUBLIC, ACC_SYNCHRONIZED
Code:
stack=0, locals=1, args_size=1
0: return
LineNumberTable:
line 9: 0
public static void test2();
descriptor: ()V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=0, args_size=0
0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #4 // Field i:I
6: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
9: return
LineNumberTable:
line 12: 0
line 13: 9
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: bipush 100
2: putstatic #4 // Field i:I
5: return
LineNumberTable:
line 3: 0
}
SourceFile: "synchronized.java"
输出的内容包含:
class文件的路径、最后修改时间、文件大小等
类的全路径、源(java)文件等
常量池
常量定义、值
构造方法
程序调用及执行逻辑
- javap -c 反编译的内容
class synchronizedTest {
synchronizedTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: invokestatic #2 // Method test2:()V
3: return
public synchronized void test1();
Code:
0: return
public static void test2();
Code:
0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #4 // Field i:I
6: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
9: return
static {};
Code:
0: bipush 100
2: putstatic #4 // Field i:I
5: return
}
- javap -s:内部签名
Compiled from "synchronized.java"
class synchronizedTest {
synchronizedTest();
descriptor: ()V
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
public synchronized void test1();
descriptor: ()V
public static void test2();
descriptor: ()V
static {};
descriptor: ()V
}
- 剩余命令:-constants、-sysinfo等作用见-help中描述。