forked from sfPlayer1/Matcher
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jadx.java
82 lines (69 loc) · 2.42 KB
/
Jadx.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
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
package matcher.srcprocess;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import jadx.api.CommentsLevel;
import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler;
import jadx.api.impl.NoOpCodeCache;
import jadx.api.plugins.input.data.IClassData;
import jadx.api.plugins.input.data.ILoadResult;
import jadx.api.plugins.input.data.IResourceData;
import jadx.core.utils.Utils;
import jadx.plugins.input.java.JavaClassReader;
import jadx.plugins.input.java.data.JavaClassData;
import matcher.NameType;
import matcher.type.ClassFeatureExtractor;
import matcher.type.ClassInstance;
public class Jadx implements Decompiler {
@Override
public synchronized String decompile(ClassInstance cls, ClassFeatureExtractor env, NameType nameType) {
String errorMessage = null;
final String fullClassName = cls.getName(NameType.PLAIN, true);
if (fullClassName.contains("$")) {
errorMessage = "JADX doesn't support decompiling inner classes!";
} else {
try (JadxDecompiler jadx = new JadxDecompiler(jadxArgs)) {
jadx.addCustomLoad(new ILoadResult() {
@Override
public void close() throws IOException {
return;
}
@Override
public void visitClasses(Consumer<IClassData> consumer) {
consumer.accept(new JavaClassData(new JavaClassReader(idGenerator.getAndIncrement(),
fullClassName + ".class", cls.serialize(nameType))));
}
@Override
public void visitResources(Consumer<IResourceData> consumer) {
return;
}
@Override
public boolean isEmpty() {
return false;
}
});
jadx.load();
assert jadx.getClassesWithInners().size() == 1;
return jadx.getClassesWithInners().get(0).getCode();
} catch (Exception e) {
errorMessage = Utils.getStackTrace(e);
}
}
throw new RuntimeException(errorMessage != null ? errorMessage : "JADX couldn't find the requested class");
}
private static final JadxArgs jadxArgs;
private static final AtomicInteger idGenerator = new AtomicInteger();
static {
jadxArgs = new JadxArgs();
jadxArgs.setCodeCache(NoOpCodeCache.INSTANCE);
jadxArgs.setShowInconsistentCode(true);
jadxArgs.setInlineAnonymousClasses(false);
jadxArgs.setInlineMethods(false);
jadxArgs.setSkipResources(true);
jadxArgs.setRespectBytecodeAccModifiers(true);
jadxArgs.setCommentsLevel(CommentsLevel.WARN);
}
}