-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecompiler.java
More file actions
101 lines (88 loc) · 2.79 KB
/
Copy pathDecompiler.java
File metadata and controls
101 lines (88 loc) · 2.79 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.lang.reflect.Modifier;
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Decompiler{
public static void main(String[] args){
if(args.length != 1){
System.err.println("usage: java Decompiler package-qualified-classname");
}else{
try{
decompileClass(Class.forName(args[0]), 0);
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
}
}
static void decompileClass(Class<?> clazz, int indentLevel){
indent(indentLevel * 3);
String modifierString = Modifier.toString(clazz.getModifiers());
System.out.print( modifierString.replace(" interface", "") + " ");
// Class.toString() handle the type string, otherwise the annotation type declaration will be weird.
if(clazz.isEnum()){
System.out.println("enum " + clazz.getName());
}else if(clazz.isInterface()){
if(clazz.isAnnotation()){
System.out.print("@"); // The annotation type source pattern can reference the Override.
}
System.out.println(clazz);
}else{
System.out.println(clazz);
}
indent(indentLevel * 3);
System.out.println("{");
Field[] fields = clazz.getDeclaredFields();
for(int i=0;i<fields.length;i++){
indent(indentLevel * 3);
System.out.println(" " + fields[i]);
}
Constructor[] constructors = clazz.getDeclaredConstructors();
if(constructors.length!=0 && fields.length!=0){
System.out.println();
}
for(int i=0;i<constructors.length;i++){
indent(indentLevel * 3);
System.out.println(" " + constructors[i]);
}
Method[] methods = clazz.getDeclaredMethods();
if(methods.length !=0 && (fields.length!=0 || constructors.length!=0)){
System.out.println();
}
for(int i=0;i<methods.length;i++){
indent(indentLevel*3);
System.out.println(" " + methods[i]);
}
Method[] allPublicMethods = clazz.getMethods();
if(allPublicMethods.length!=0 && (fields.length!=0 || constructors.length!=0 || methods.length!=0)){
System.out.println();
}
if(allPublicMethods.length!=0){
indent(indentLevel*3);
System.out.println(" ALL PUBLIC METHODS");
System.out.println();
}
for(int i=0;i<allPublicMethods.length;i++){
indent(indentLevel*3);
System.out.println(" " + allPublicMethods[i]);
}
Class<?>[] members = clazz.getDeclaredClasses();
if(members.length!=0 && (fields.length!=0|| constructors.length!=0 || methods.length!=0 || allPublicMethods.length!=0)){
System.out.println();
}
for(int i=0; i < members.length;i++){
if(clazz != members[i]){
decompileClass(members[i], indentLevel + 1);
if(i != members.length -1){
System.out.println();
}
}
}
indent(indentLevel*3);
System.out.println("}");
}
static void indent(int numSpaces){
for(int i=0;i<numSpaces;i++){
System.out.print(' ');
}
}
}