forked from helios-decompiler/transformer-api
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecompiler.java
More file actions
136 lines (116 loc) · 5.04 KB
/
Decompiler.java
File metadata and controls
136 lines (116 loc) · 5.04 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright 2017 Sam Sun <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.heliosdecompiler.transformerapi.decompilers;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InnerClassNode;
import org.vineflower.java.decompiler.main.extern.IContextSource;
import org.vineflower.java.decompiler.main.extern.IResultSaver;
import com.heliosdecompiler.transformerapi.TransformationException;
import com.heliosdecompiler.transformerapi.common.Loader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jd.core.DecompilationResult;
/**
* Represents a particular Decompiler. Note that decompiler implementations
* should be stateless, and thus can be reused (and are thread safe)
*/
public interface Decompiler<S> {
/**
* Decompile the given class with a loader
*
* @param loader The loader implementation used to load the class
* @param internalName The internal name of the class to decompile
* @param settings The settings to use with this decompiler
* @return The decompilation result
* @throws TransformationException
* @throws IOException
*/
DecompilationResult decompile(Loader loader, String internalName, S settings) throws TransformationException, IOException;
S defaultSettings();
default DecompilationResult decompile(Loader loader, String internalName) throws TransformationException, IOException {
return decompile(loader, internalName, defaultSettings());
}
default ClassStruct readClassAndInnerClasses(Loader loader, String internalName) throws IOException {
return readClassAndInnerClasses(new HashMap<>(), loader, internalName);
}
default ClassStruct readClassAndInnerClasses(Map<String, byte[]> importantData, Loader loader, String internalName) throws IOException {
String fullClassName = internalName.replace('/', '.');
if (!importantData.containsKey(internalName) && loader.canLoad(internalName)) {
byte[] data = loader.load(internalName);
importantData.put(internalName, data);
ClassReader reader = new ClassReader(data);
ClassNode classNode = new ClassNode();
reader.accept(classNode, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
fullClassName = classNode.name;
if (classNode.innerClasses != null) {
for (InnerClassNode icn : classNode.innerClasses) {
if (icn.name.startsWith(internalName + '$')) {
importantData.putAll(readClassAndInnerClasses(importantData, loader, icn.name).importantData);
}
}
}
}
return new ClassStruct(fullClassName, importantData);
}
record ClassStruct(String fullClassName, Map<String, byte[]> importantData) implements IContextSource {
@Override
public String getName() {
return "TransformerAPI ClassStruct";
}
@Override
public Entries getEntries() {
List<Entry> classes = new ArrayList<>();
for (String key : importantData.keySet()) {
classes.add(Entry.parse(key));
}
return new Entries(classes, Collections.emptyList(), Collections.emptyList());
}
@Override
public InputStream getInputStream(String resource) throws IOException {
return new ByteArrayInputStream(importantData.get(resource.replaceFirst("\\.class$", "")));
}
@Override
public IOutputSink createOutputSink(IResultSaver saver) {
return new IOutputSink() {
@Override
public void close() throws IOException {
}
@Override
public void begin() {
}
@Override
public void acceptOther(String path) {
// not used
}
@Override
public void acceptDirectory(String directory) {
// not used
}
@Override
public void acceptClass(String qualifiedName, String fileName, String content, int[] mapping) {
saver.saveClassFile("", qualifiedName, fileName, content, mapping);
}
};
}
}
}