How to disassemble Java code - java

How to disassemble Java code - java

八月 05, 2019

Introduction

In order to do some researchs about the internal structure of the Java code, we need to disassemble the Java code.

How

Create a Java file(Singleton.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Author: steven
* Email: steven.wang@deepnetsecurity.com
* Datetime: 12/06/19 17:58
**/
public class Singleton {
private volatile static Singleton uniqueInstance;

private Singleton() {
}

public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}

Java file to class file, we will get Singleton.class which will be similar with Java file.

1
javac Singleton.java
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

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public class Singleton {
private static volatile Singleton uniqueInstance;

private Singleton() {
}

public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
Class var0 = Singleton.class;
synchronized(Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}

return uniqueInstance;
}
}

Class file to assembly code

1
javap -c Singleton.class
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
Compiled from "Singleton.java"
public class Singleton {
public static Singleton getUniqueInstance();
Code:
0: getstatic #2 // Field uniqueInstance:LSingleton;
3: ifnonnull 37
6: ldc #3 // class Singleton
8: dup
9: astore_0
10: monitorenter
11: getstatic #2 // Field uniqueInstance:LSingleton;
14: ifnonnull 27
17: new #3 // class Singleton
20: dup
21: invokespecial #4 // Method "<init>":()V
24: putstatic #2 // Field uniqueInstance:LSingleton;
27: aload_0
28: monitorexit
29: goto 37
32: astore_1
33: aload_0
34: monitorexit
35: aload_1
36: athrow
37: getstatic #2 // Field uniqueInstance:LSingleton;
40: areturn
Exception table:
from to target type
11 29 32 any
32 35 32 any
}