Skip to content

Commit a0cad77

Browse files
committed
Refactor getName() into JDANamespacedComponent
1 parent 4018075 commit a0cad77

File tree

16 files changed

+150
-82
lines changed

16 files changed

+150
-82
lines changed

mapleir/src/main/java/org/mapleir/jdaplugin/ILDecompiler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.mapleir.jdaplugin;
22

33
import club.bytecode.the.jda.FileContainer;
4+
import club.bytecode.the.jda.api.JDANamespace;
45
import club.bytecode.the.jda.decompilers.JDADecompiler;
56
import org.mapleir.ir.algorithms.BoissinotDestructor;
67
import org.mapleir.ir.cfg.ControlFlowGraph;
@@ -34,14 +35,14 @@ protected ControlFlowGraph getCfg(MethodNode mn) {
3435
cp.print(cn);
3536
return sw.toString();
3637
}
37-
38-
@Override
39-
public void decompileToZip(String zipName) {
40-
41-
}
4238

4339
@Override
4440
public String getName() {
4541
return "MapleIL";
4642
}
43+
44+
@Override
45+
public JDANamespace getNamespace() {
46+
return MaplePlugin.getInstance().getNamespace();
47+
}
4748
}

mapleir/src/main/java/org/mapleir/jdaplugin/IRDecompiler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mapleir.jdaplugin;
22

3+
import club.bytecode.the.jda.api.JDANamespace;
34
import club.bytecode.the.jda.decompilers.bytecode.*;
45
import org.mapleir.ir.algorithms.BoissinotDestructor;
56
import org.mapleir.ir.cfg.ControlFlowGraph;
@@ -17,14 +18,15 @@ protected MethodNodeDecompiler getMethodNodeDecompiler(PrefixedStringBuilder sb,
1718
return new IRMethodDecompiler(this, sb, it.next(), cn);
1819
}
1920

20-
@Override
21-
public void decompileToZip(String zipName) {
22-
}
23-
2421
@Override
2522
public String getName() {
2623
return "MapleIR";
2724
}
25+
26+
@Override
27+
public JDANamespace getNamespace() {
28+
return MaplePlugin.getInstance().getNamespace();
29+
}
2830
}
2931

3032
class IRMethodDecompiler extends MethodNodeDecompiler {

mapleir/src/main/java/org/mapleir/jdaplugin/MaplePlugin.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import club.bytecode.the.jda.FileContainer;
44
import club.bytecode.the.jda.api.JDAPlugin;
5+
import club.bytecode.the.jda.api.JDAPluginNamespace;
56
import club.bytecode.the.jda.decompilers.Decompilers;
67
import club.bytecode.the.jda.decompilers.JDADecompiler;
78
import org.mapleir.DefaultInvocationResolver;
@@ -18,21 +19,34 @@
1819
import java.util.Map;
1920

2021
public class MaplePlugin implements JDAPlugin {
21-
public static final Map<FileContainer, AnalysisContext> cxts = new HashMap<>();
22-
public static final JDADecompiler MAPLEIR = new IRDecompiler();
23-
public static final JDADecompiler MAPLEIL = new ILDecompiler();
22+
private static MaplePlugin instance;
23+
24+
public final Map<FileContainer, AnalysisContext> cxts = new HashMap<>();
25+
private final JDADecompiler MAPLEIR = new IRDecompiler();
26+
private final JDADecompiler MAPLEIL = new ILDecompiler();
27+
public final JDAPluginNamespace namespace = new JDAPluginNamespace(this);
2428

2529
public MaplePlugin() {
30+
instance = this;
2631
}
2732

2833
public static void main(String[] args) {
2934
throw new NotImplementedException();
3035
}
36+
37+
public static MaplePlugin getInstance() {
38+
return instance;
39+
}
3140

3241
@Override
3342
public String getName() {
3443
return "MapleIR";
3544
}
45+
46+
@Override
47+
public JDAPluginNamespace getNamespace() {
48+
return namespace;
49+
}
3650

3751
@Override
3852
public void onLoad() {

src/main/java/club/bytecode/the/jda/JDA.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package club.bytecode.the.jda;
22

33
import club.bytecode.the.jda.api.ExceptionUI;
4+
import club.bytecode.the.jda.api.JDANamespace;
45
import club.bytecode.the.jda.api.JDAPlugin;
56
import club.bytecode.the.jda.api.PluginLoader;
67
import club.bytecode.the.jda.gui.MainViewerGUI;
@@ -47,8 +48,9 @@ public class JDA {
4748
private static List<String> recentFiles = new ArrayList<>();
4849
public static String lastDirectory = "";
4950
public static List<Process> createdProcesses = new ArrayList<>();
50-
private static List<JDAPlugin> plugins = new ArrayList<>();
5151
private static final AtomicInteger jobCount = new AtomicInteger(0);
52+
public static final JDANamespace namespace = JDADefaultNamespace.INSTANCE;
53+
private static List<JDAPlugin> plugins = new ArrayList<>();
5254

5355
public static Supplier<JDAPlugin> injectedPlugin = null; // for testing purposes only.
5456

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package club.bytecode.the.jda;
2+
3+
import club.bytecode.the.jda.api.JDANamespace;
4+
5+
public class JDADefaultNamespace implements JDANamespace {
6+
public static final JDADefaultNamespace INSTANCE = new JDADefaultNamespace();
7+
8+
private JDADefaultNamespace() {
9+
}
10+
11+
@Override
12+
public String getName() {
13+
return "JDA";
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return getName();
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package club.bytecode.the.jda.api;
2+
3+
public interface JDANamespace {
4+
String getName();
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package club.bytecode.the.jda.api;
2+
3+
public interface JDANamespacedComponent {
4+
String getName();
5+
6+
JDANamespace getNamespace();
7+
8+
default String getFullName() {
9+
return getName() + ":" + getNamespace();
10+
}
11+
}

src/main/java/club/bytecode/the/jda/api/JDAPlugin.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import club.bytecode.the.jda.FileContainer;
44

5-
public interface JDAPlugin {
6-
/**
7-
* Should return the human-readable name of the plugin.
8-
*/
9-
String getName();
5+
public interface JDAPlugin extends JDANamespacedComponent{
106

117
/**
128
* Callback for when the plugin is loaded.
@@ -44,4 +40,10 @@ public interface JDAPlugin {
4440
* Called when the plugin's button in the Edit/Plugins menu is pressed.
4541
*/
4642
void onPluginButton();
43+
44+
/**
45+
* Should return the JDAPluginNamespace for this plugin
46+
*/
47+
@Override
48+
JDAPluginNamespace getNamespace();
4749
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package club.bytecode.the.jda.api;
2+
3+
public class JDAPluginNamespace implements JDANamespace {
4+
private final JDAPlugin plugin;
5+
6+
public JDAPluginNamespace(JDAPlugin plugin) {
7+
this.plugin = plugin;
8+
}
9+
10+
public JDAPlugin getPlugin() {
11+
return plugin;
12+
}
13+
14+
@Override
15+
public String getName() {
16+
return plugin.getName();
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return getName();
22+
}
23+
}

src/main/java/club/bytecode/the/jda/decompilers/CFRDecompiler.java

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import club.bytecode.the.jda.FileContainer;
44
import club.bytecode.the.jda.JDA;
5-
import club.bytecode.the.jda.JarUtils;
5+
import club.bytecode.the.jda.api.JDANamespace;
66
import club.bytecode.the.jda.settings.JDADecompilerSettings;
77
import club.bytecode.the.jda.settings.JDADecompilerSettings.SettingsEntry;
8-
import org.apache.commons.io.FileUtils;
98
import org.benf.cfr.reader.bytecode.analysis.types.JavaTypeInstance;
109
import org.benf.cfr.reader.entities.ClassFile;
1110
import org.benf.cfr.reader.entities.Method;
@@ -23,11 +22,7 @@
2322
import org.benf.cfr.reader.util.getopt.OptionsImpl;
2423
import org.benf.cfr.reader.util.output.*;
2524
import org.objectweb.asm.tree.ClassNode;
26-
import org.zeroturnaround.zip.ZipUtil;
2725

28-
import java.io.File;
29-
import java.io.IOException;
30-
import java.nio.file.Files;
3126
import java.nio.file.Path;
3227
import java.util.List;
3328
import java.util.Set;
@@ -94,6 +89,11 @@ public JDADecompilerSettings getSettings() {
9489
public String getName() {
9590
return "CFR";
9691
}
92+
93+
@Override
94+
public JDANamespace getNamespace() {
95+
return JDA.namespace;
96+
}
9797

9898
@Override
9999
public String decompileClassNode(FileContainer container, ClassNode cn) {
@@ -108,38 +108,6 @@ public String decompileClassNode(FileContainer container, ClassNode cn) {
108108
}
109109
}
110110

111-
@Override
112-
public void decompileToZip(String zipName) {
113-
try {
114-
Path outputDir = Files.createTempDirectory("cfr_output");
115-
Path tempJar = Files.createTempFile("cfr_input", ".jar");
116-
File output = new File(zipName);
117-
try {
118-
JarUtils.saveAsJar(JDA.getLoadedBytes(), tempJar.toAbsolutePath().toString());
119-
Options options = new GetOptParser().parse(generateMainMethod(), OptionsImpl.getFactory());
120-
ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options);
121-
DCCommonState dcCommonState = new DCCommonState(options, classFileSource);
122-
doJar(dcCommonState, tempJar.toAbsolutePath(), outputDir.toAbsolutePath());
123-
ZipUtil.pack(outputDir.toFile(), output);
124-
} catch (Exception e) {
125-
handleException(e);
126-
} finally {
127-
try {
128-
FileUtils.deleteDirectory(outputDir.toFile());
129-
} catch (IOException e) {
130-
handleException(e);
131-
}
132-
try {
133-
Files.delete(tempJar);
134-
} catch (IOException e) {
135-
handleException(e);
136-
}
137-
}
138-
} catch (Exception e) {
139-
handleException(e);
140-
}
141-
}
142-
143111
public String[] generateMainMethod() {
144112
Set<SettingsEntry> entries = settings.getEntries();
145113
String[] result = new String[entries.size() * 2 + 1];

0 commit comments

Comments
 (0)