Skip to content

Commit bb50a5f

Browse files
committed
Refactor DecompilerSettings
1 parent 5529259 commit bb50a5f

File tree

7 files changed

+183
-106
lines changed

7 files changed

+183
-106
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public String[] generateMainMethod() {
103103
int index = 1;
104104
for (Settings setting : Settings.values()) {
105105
result[index++] = "--" + setting.getParam();
106-
result[index++] = String.valueOf(getSettings().isSelected(setting));
106+
result[index++] = String.valueOf(getSettings().getBoolean(setting));
107107
}
108108
return result;
109109
}
@@ -191,6 +191,7 @@ public static void doJar(DCCommonState dcCommonState, Path input, Path output) t
191191
}
192192
}
193193

194+
// TODO: Rewrite!
194195
public enum Settings implements DecompilerSettings.SettingsEntry {
195196
DECODE_ENUM_SWITCH("decodeenumswitch", "Decode Enum Switch", true),
196197
SUGAR_ENUMS("sugarenums", "SugarEnums", true),
@@ -254,12 +255,16 @@ public String getText() {
254255
return name;
255256
}
256257

257-
public boolean isDefaultOn() {
258-
return on;
259-
}
260-
261258
public String getParam() {
262259
return param;
263260
}
261+
262+
public String getDefaultValue() {
263+
return String.valueOf(on);
264+
}
265+
266+
public SettingType getType() {
267+
return SettingType.BOOLEAN;
268+
}
264269
}
265270
}

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

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getName() {
4141
@Override
4242
public String decompileClassNode(String containerName, final ClassNode cn) {
4343
try {
44-
Map<String, Object> options = main(generateMainMethod());
44+
Map<String, Object> options = generateFernflowerArgs();
4545

4646
final AtomicReference<String> result = new AtomicReference<>();
4747
result.set(null);
@@ -120,7 +120,7 @@ public void decompileToZip(String zipName) {
120120
Path tempJar = Files.createTempFile("fernflower_input", ".jar");
121121
File output = new File(zipName);
122122
JarUtils.saveAsJar(JDA.getLoadedBytes(), tempJar.toAbsolutePath().toString());
123-
ConsoleDecompiler decompiler = new ConsoleDecompiler(outputDir.toFile(), main(generateMainMethod()));
123+
ConsoleDecompiler decompiler = new ConsoleDecompiler(outputDir.toFile(), generateFernflowerArgs());
124124
decompiler.addSpace(tempJar.toFile(), true);
125125
decompiler.decompileContext();
126126
Files.move(outputDir.toFile().listFiles()[0].toPath(), output.toPath());
@@ -131,87 +131,72 @@ public void decompileToZip(String zipName) {
131131
}
132132
}
133133

134-
public Map<String, Object> main(String[] args) {
135-
HashMap mapOptions = new HashMap();
136-
boolean isOption = true;
137-
138-
for (int destination = 0; destination < args.length - 1; ++destination) {
139-
String logger = args[destination];
140-
if (isOption && logger.length() > 5 && logger.charAt(0) == 45 && logger.charAt(4) == 61) {
141-
String decompiler = logger.substring(5);
142-
if ("true".equalsIgnoreCase(decompiler)) {
143-
decompiler = "1";
144-
} else if ("false".equalsIgnoreCase(decompiler)) {
145-
decompiler = "0";
146-
}
147-
148-
mapOptions.put(logger.substring(1, 4), decompiler);
149-
} else {
150-
isOption = false;
151-
}
152-
}
153-
154-
return mapOptions;
155-
}
156-
157-
private String[] generateMainMethod() {
158-
String[] result = new String[getSettings().size()];
159-
int index = 0;
134+
private Map<String, Object> generateFernflowerArgs() {
135+
Map<String, Object> options = new HashMap<>();
160136
for (Settings setting : Settings.values()) {
161-
result[index++] = String.format("-%s=%s", setting.getParam(),
162-
getSettings().isSelected(setting) ? "1" : "0");
137+
options.put(setting.getParam(), getSettings().getValue(setting));
163138
}
164-
return result;
139+
return options;
165140
}
166141

167142
public enum Settings implements DecompilerSettings.SettingsEntry {
168-
HIDE_BRIDGE_METHODS("rbr", "Hide Bridge Methods", true),
169-
HIDE_SYNTHETIC_CLASS_MEMBERS("rsy", "Hide Synthetic Class Members"),
170-
DECOMPILE_INNER_CLASSES("din", "Decompile Inner Classes", true),
171-
COLLAPSE_14_CLASS_REFERENCES("dc4", "Collapse 1.4 Class References", true),
172-
DECOMPILE_ASSERTIONS("das", "Decompile Assertions", true),
173-
HIDE_EMPTY_SUPER_INVOCATION("hes", "Hide Empty Super Invocation", true),
174-
HIDE_EMPTY_DEFAULT_CONSTRUCTOR("hec", "Hide Empty Default Constructor", true),
175-
DECOMPILE_GENERIC_SIGNATURES("dgs", "Decompile Generic Signatures"),
176-
ASSUME_RETURN_NOT_THROWING_EXCEPTIONS("ner", "Assume return not throwing exceptions", true),
177-
DECOMPILE_ENUMS("den", "Decompile enumerations", true),
178-
REMOVE_GETCLASS("rgn", "Remove getClass()", true),
179-
OUTPUT_NUMBERIC_LITERALS("lit", "Output numeric literals 'as-is'"),
180-
ENCODE_UNICODE("asc", "Encode non-ASCII as unicode escapes"),
181-
INT_1_AS_BOOLEAN_TRUE("bto", "Assume int 1 is boolean true", true),
182-
ALLOW_NOT_SET_SYNTHETIC("nns", "Allow not set synthetic attribute", true),
183-
NAMELESS_TYPES_AS_OBJECT("uto", "Consider nameless types as java.lang.Object", true),
184-
RECOVER_VARIABLE_NAMES("udv", "Recover variable names", true),
185-
REMOVE_EMPTY_EXCEPTIONS("rer", "Remove empty exceptions", true),
186-
DEINLINE_FINALLY("fdi", "De-inline finally", true),
187-
RENAME_AMBIGIOUS_MEMBERS("ren", "Rename ambigious members"),
188-
REMOVE_INTELLIJ_NOTNULL("inn", "Remove IntelliJ @NotNull", true),
189-
DECOMPILE_LAMBDA_TO_ANONYMOUS("lac", "Decompile lambdas to anonymous classes");
190-
191-
private String name;
192-
private String param;
193-
private boolean on;
194-
195-
Settings(String param, String name) {
196-
this(param, name, false);
143+
HIDE_BRIDGE_METHODS("rbr", "Hide Bridge Methods", "true"),
144+
HIDE_SYNTHETIC_CLASS_MEMBERS("rsy", "Hide Synthetic Class Members", "false"),
145+
DECOMPILE_INNER_CLASSES("din", "Decompile Inner Classes", "true"),
146+
COLLAPSE_14_CLASS_REFERENCES("dc4", "Collapse 1.4 Class References", "true"),
147+
DECOMPILE_ASSERTIONS("das", "Decompile Assertions", "true"),
148+
HIDE_EMPTY_SUPER_INVOCATION("hes", "Hide Empty Super Invocation", "true"),
149+
HIDE_EMPTY_DEFAULT_CONSTRUCTOR("hec", "Hide Empty Default Constructor", "true"),
150+
DECOMPILE_GENERIC_SIGNATURES("dgs", "Decompile Generic Signatures", "false"),
151+
ASSUME_RETURN_NOT_THROWING_EXCEPTIONS("ner", "Assume return not throwing exceptions", "true"),
152+
DECOMPILE_ENUMS("den", "Decompile enumerations", "true"),
153+
REMOVE_GETCLASS("rgn", "Remove getClass()", "true"),
154+
OUTPUT_NUMBERIC_LITERALS("lit", "Output numeric literals 'as-is'", "false"),
155+
ENCODE_UNICODE("asc", "Encode non-ASCII as unicode escapes", "true"),
156+
INT_1_AS_BOOLEAN_TRUE("bto", "Assume int 1 is boolean true", "true"),
157+
ALLOW_NOT_SET_SYNTHETIC("nns", "Allow not set synthetic attribute", "true"),
158+
NAMELESS_TYPES_AS_OBJECT("uto", "Consider nameless types as java.lang.Object", "true"),
159+
RECOVER_VARIABLE_NAMES("udv", "Recover variable names", "true"),
160+
REMOVE_EMPTY_EXCEPTIONS("rer", "Remove empty exceptions", "true"),
161+
DEINLINE_FINALLY("fdi", "De-inline finally", "true"),
162+
TIME_LIMIT("mpm", "Maximum processing time", "0", SettingType.INT), // this is a numeric setting!
163+
RENAME_AMBIGIOUS_MEMBERS("ren", "Rename ambigious members", "false"),
164+
// urc: IIDentifierRenamer
165+
REMOVE_INTELLIJ_NOTNULL("inn", "Remove IntelliJ @NotNull", "true"),
166+
DECOMPILE_LAMBDA_TO_ANONYMOUS("lac", "Decompile lambdas to anonymous classes", "false"),
167+
// NEWLINE_TYPE("nls", "Newline character"); // this is an optional argument!
168+
INDENTATION("ind", "Indentation string", " ", SettingType.STRING);
169+
170+
private final String name;
171+
private final String param;
172+
private final String defaultValue;
173+
private final SettingType type;
174+
175+
Settings(String param, String name, String defaultValue) {
176+
this(param, name, defaultValue, SettingType.BOOLEAN);
197177
}
198178

199-
Settings(String param, String name, boolean on) {
179+
Settings(String param, String name, String defaultValue, SettingType type) {
200180
this.name = name;
201181
this.param = param;
202-
this.on = on;
182+
this.defaultValue = defaultValue;
183+
this.type = type;
203184
}
204185

205186
public String getText() {
206187
return name;
207188
}
208189

209-
public boolean isDefaultOn() {
210-
return on;
211-
}
212-
213190
public String getParam() {
214191
return param;
215192
}
193+
194+
public String getDefaultValue() {
195+
return defaultValue;
196+
}
197+
198+
public SettingType getType() {
199+
return type;
200+
}
216201
}
217202
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public DecompilerSettings getDecompilerSettings() {
4444
JCommander jCommander = new JCommander(options);
4545
List<String> args = new ArrayList<>();
4646
for (the.bytecode.club.jda.settings.DecompilerSettings.SettingsEntry entry : Settings.values())
47-
if (getSettings().isSelected(entry))
47+
if (getSettings().getBoolean(entry))
4848
args.add("--" + entry.getParam());
4949
String[] argsArr = new String[args.size()];
5050
args.toArray(argsArr);
@@ -195,6 +195,7 @@ private void doSaveJarDecompiled(File inFile, File outFile) throws Exception {
195195
}
196196
}
197197

198+
// TODO: Rewrite!
198199
public enum Settings implements the.bytecode.club.jda.settings.DecompilerSettings.SettingsEntry {
199200
SHOW_DEBUG_LINE_NUMBERS("debug-line-numbers", "Show Debug Line Numbers"),
200201
SIMPLIFY_MEMBER_REFERENCES("simplify-member-references", "Simplify Member References"),
@@ -234,5 +235,13 @@ public boolean isDefaultOn() {
234235
public String getParam() {
235236
return param;
236237
}
238+
239+
public String getDefaultValue() {
240+
return String.valueOf(on);
241+
}
242+
243+
public SettingType getType() {
244+
return SettingType.BOOLEAN;
245+
}
237246
}
238247
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList<St
7575
sb.append(JDA.nl);
7676
}
7777

78-
if (getSettings().isSelected(Settings.DECOMPILE_INNER_CLASSES))
78+
if (getSettings().getBoolean(Settings.DECOMPILE_INNER_CLASSES))
7979
for (InnerClassNode innerClassNode : cn.innerClasses) {
8080
String innerClassName = innerClassNode.name;
8181
if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
@@ -156,6 +156,7 @@ public static String getAccessString(int access) {
156156
public void decompileToZip(String zipName) {
157157
}
158158

159+
// TODO: Refactor!
159160
public enum Settings implements DecompilerSettings.SettingsEntry {
160161
DEBUG_HELPERS("debug-helpers", "Debug Helpers", true),
161162
APPEND_BRACKETS_TO_LABELS("append-brackets-to-labels", "Append Brackets to Labels", true),
@@ -187,5 +188,13 @@ public boolean isDefaultOn() {
187188
public String getParam() {
188189
return param;
189190
}
191+
192+
public String getDefaultValue() {
193+
return String.valueOf(on);
194+
}
195+
196+
public SettingType getType() {
197+
return SettingType.BOOLEAN;
198+
}
190199
}
191200
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ protected static String getAccessString(int access) {
255255
}
256256

257257
boolean createComments() {
258-
return parent.getSettings().isSelected(ClassNodeDecompiler.Settings.DEBUG_HELPERS);
258+
return parent.getSettings().getBoolean(ClassNodeDecompiler.Settings.DEBUG_HELPERS);
259259
}
260260

261261
boolean createLabelBrackets() {
262-
return parent.getSettings().isSelected(ClassNodeDecompiler.Settings.APPEND_BRACKETS_TO_LABELS);
262+
return parent.getSettings().getBoolean(ClassNodeDecompiler.Settings.APPEND_BRACKETS_TO_LABELS);
263263
}
264264

265265
boolean createDescriptors() {
266-
return parent.getSettings().isSelected(ClassNodeDecompiler.Settings.SHOW_METHOD_DESCRIPTORS);
266+
return parent.getSettings().getBoolean(ClassNodeDecompiler.Settings.SHOW_METHOD_DESCRIPTORS);
267267
}
268268
}

src/main/java/the/bytecode/club/jda/gui/MainViewerGUI.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import the.bytecode.club.jda.gui.fileviewer.FileViewerPane;
1313
import the.bytecode.club.jda.gui.fileviewer.Viewer;
1414
import the.bytecode.club.jda.gui.navigation.FileNavigationPane;
15-
import the.bytecode.club.jda.settings.DecompilerSettings;
1615
import the.bytecode.club.jda.settings.IPersistentWindow;
1716
import the.bytecode.club.jda.settings.Settings;
1817

@@ -260,12 +259,9 @@ private void initializeMenubar() {
260259
settingsMenu.add(new JSeparator());
261260

262261
for (Decompiler decompiler : Decompilers.getAllDecompilers()) {
263-
JMenu decompilerSettingsMenu = new JMenu(decompiler.getName());
264-
DecompilerSettings decompilerSettings = decompiler.getSettings();
265-
for (DecompilerSettings.SettingsEntry entry : decompilerSettings.getEntries()) {
266-
decompilerSettingsMenu.add(decompilerSettings.getMenuItem(entry));
267-
}
268-
settingsMenu.add(decompilerSettingsMenu);
262+
JMenuItem settingsButton = new JMenuItem(decompiler.getName());
263+
settingsButton.addActionListener(e -> decompiler.getSettings().displayDialog());
264+
settingsMenu.add(settingsButton);
269265
}
270266

271267
menuBar.add(settingsMenu);

0 commit comments

Comments
 (0)