Skip to content

Commit 9353c96

Browse files
committed
Code Style Cleanup / General Code Improvements
1 parent 3ab9932 commit 9353c96

File tree

84 files changed

+544
-384
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+544
-384
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class Constants
6868

6969
public static final String FS = System.getProperty("file.separator");
7070
public static final String NL = System.getProperty("line.separator");
71-
public static final String[] SUPPORTED_FILE_EXTENSIONS = ResourceType.supportedBCVExtensionMap.keySet().toArray(new String[0]);
71+
public static final String[] SUPPORTED_FILE_EXTENSIONS = ResourceType.SUPPORTED_BCV_EXTENSION_MAP.keySet().toArray(new String[0]);
7272
public static final int ASM_VERSION = Opcodes.ASM9;
7373

7474
public static final File BCVDir = resolveBCVRoot();

src/main/java/the/bytecode/club/bytecodeviewer/GlobalHotKeys.java

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ else if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiersEx() & KeyEvent.CT
106106
JFileChooser fc = new FileChooser(Configuration.getLastSaveDirectory(), "Select Zip Export", "Zip Archives", "zip");
107107

108108
int returnVal = fc.showSaveDialog(BytecodeViewer.viewer);
109+
109110
if (returnVal == JFileChooser.APPROVE_OPTION)
110111
{
111112
Configuration.setLastSaveDirectory(fc.getSelectedFile());
@@ -124,6 +125,7 @@ else if ((e.getKeyCode() == KeyEvent.VK_S) && ((e.getModifiersEx() & KeyEvent.CT
124125
jarExport.start();
125126
}
126127
}, "Resource Export");
128+
127129
resourceExport.start();
128130
}
129131

src/main/java/the/bytecode/club/bytecodeviewer/Settings.java

+6
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,38 @@ protected static void resetRecentFilesMenu()
127127
{
128128
//build recent files
129129
BytecodeViewer.viewer.recentFilesSecondaryMenu.removeAll();
130+
130131
for (String s : recentFiles)
131132
{
132133
if (!s.isEmpty())
133134
{
134135
JMenuItem m = new JMenuItem(s);
136+
135137
m.addActionListener(e ->
136138
{
137139
JMenuItem m12 = (JMenuItem) e.getSource();
138140
BytecodeViewer.openFiles(new File[]{new File(m12.getText())}, true);
139141
});
142+
140143
BytecodeViewer.viewer.recentFilesSecondaryMenu.add(m);
141144
}
142145
}
143146

144147
//build recent plugins
145148
BytecodeViewer.viewer.recentPluginsSecondaryMenu.removeAll();
149+
146150
for (String s : recentPlugins)
147151
{
148152
if (!s.isEmpty())
149153
{
150154
JMenuItem m = new JMenuItem(s);
155+
151156
m.addActionListener(e ->
152157
{
153158
JMenuItem m1 = (JMenuItem) e.getSource();
154159
BytecodeViewer.startPlugin(new File(m1.getText()));
155160
});
161+
156162
BytecodeViewer.viewer.recentPluginsSecondaryMenu.add(m);
157163
}
158164
}

src/main/java/the/bytecode/club/bytecodeviewer/SettingsSerializer.java

+3
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,13 @@ public static void loadSettings()
338338
BytecodeViewer.viewer.refreshOnChange.setSelected(asBoolean(84));
339339

340340
boolean bool = Boolean.parseBoolean(asString(85));
341+
341342
if (bool)
342343
{
343344
BytecodeViewer.viewer.setExtendedState(JFrame.MAXIMIZED_BOTH);
344345
BytecodeViewer.viewer.isMaximized = true;
345346
}
347+
346348
//86 is deprecated
347349
//87 is deprecated
348350
Configuration.lastOpenDirectory = asString(88);
@@ -388,6 +390,7 @@ public static void loadSettings()
388390
//line 130 is used for preload
389391
if (Configuration.language != Language.ENGLISH)
390392
Configuration.language.setLanguageTranslations(); //load language translations
393+
391394
Settings.hasSetLanguageAsSystemLanguage = true;
392395

393396
BytecodeViewer.viewer.viewPane1.setPaneEditable(asBoolean(131));

src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/JavaCompiler.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,23 @@ public class JavaCompiler extends InternalCompiler
4343
@Override
4444
public byte[] compile(String contents, String fullyQualifiedName)
4545
{
46-
String fileStart = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
47-
String fileStart2 = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
48-
File java = new File(fileStart + FS + fullyQualifiedName + ".java");
49-
File clazz = new File(fileStart2 + FS + fullyQualifiedName + ".class");
50-
File cp = new File(TEMP_DIRECTORY + FS + "cpath_" + MiscUtils.randomString(12) + ".jar");
51-
File tempD = new File(fileStart + FS + fullyQualifiedName.substring(0, fullyQualifiedName.length() -
46+
final String fileStart = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
47+
final String fileStart2 = TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(12) + FS;
48+
49+
final File javaFile = new File(fileStart + FS + fullyQualifiedName + ".java");
50+
final File classFile = new File(fileStart2 + FS + fullyQualifiedName + ".class");
51+
final File classPath = new File(TEMP_DIRECTORY + FS + "cpath_" + MiscUtils.randomString(12) + ".jar");
52+
final File tempDirectory = new File(fileStart + FS + fullyQualifiedName.substring(0, fullyQualifiedName.length() -
5253
fullyQualifiedName.split("/")[fullyQualifiedName.split("/").length - 1].length()));
5354

54-
tempD.mkdirs();
55+
//create the temp directories
56+
tempDirectory.mkdirs();
5557
new File(fileStart2).mkdirs();
5658

5759
if (Configuration.javac.isEmpty() || !new File(Configuration.javac).exists())
5860
{
59-
BytecodeViewer.showMessage("You need to set your Javac path, this requires the JDK to be downloaded." + NL + "(C:/Program Files/Java/JDK_xx/bin/javac.exe)");
61+
BytecodeViewer.showMessage("You need to set your Javac path, this requires the JDK to be downloaded."
62+
+ NL + "(C:/Program Files/Java/JDK_xx/bin/javac.exe)");
6063
ExternalResources.getSingleton().selectJavac();
6164
}
6265

@@ -66,8 +69,11 @@ public byte[] compile(String contents, String fullyQualifiedName)
6669
return null;
6770
}
6871

69-
DiskWriter.replaceFile(java.getAbsolutePath(), contents, false);
70-
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), cp.getAbsolutePath());
72+
//write the file we're assembling to disk
73+
DiskWriter.replaceFile(javaFile.getAbsolutePath(), contents, false);
74+
75+
//write the entire temporary classpath to disk
76+
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), classPath.getAbsolutePath());
7177

7278
boolean cont = true;
7379
try
@@ -77,10 +83,10 @@ public byte[] compile(String contents, String fullyQualifiedName)
7783

7884
if (Configuration.library.isEmpty())
7985
pb = new ProcessBuilder(Configuration.javac, "-d", fileStart2,
80-
"-classpath", cp.getAbsolutePath(), java.getAbsolutePath());
86+
"-classpath", classPath.getAbsolutePath(), javaFile.getAbsolutePath());
8187
else
8288
pb = new ProcessBuilder(Configuration.javac, "-d", fileStart2,
83-
"-classpath", cp.getAbsolutePath() + System.getProperty("path.separator") + Configuration.library, java.getAbsolutePath());
89+
"-classpath", classPath.getAbsolutePath() + System.getProperty("path.separator") + Configuration.library, javaFile.getAbsolutePath());
8490

8591
Process process = pb.start();
8692
BytecodeViewer.createdProcesses.add(process);
@@ -111,6 +117,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
111117
}
112118

113119
log.append(NL).append(NL).append(TranslatedStrings.ERROR2).append(NL).append(NL);
120+
114121
try (InputStream is = process.getErrorStream();
115122
InputStreamReader isr = new InputStreamReader(is);
116123
BufferedReader br = new BufferedReader(isr))
@@ -123,7 +130,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
123130
log.append(NL).append(NL).append(TranslatedStrings.EXIT_VALUE_IS).append(" ").append(exitValue);
124131
System.out.println(log);
125132

126-
if (!clazz.exists())
133+
if (!classFile.exists())
127134
throw new Exception(log.toString());
128135
}
129136
catch (Exception e)
@@ -132,12 +139,12 @@ public byte[] compile(String contents, String fullyQualifiedName)
132139
e.printStackTrace();
133140
}
134141

135-
cp.delete();
142+
classPath.delete();
136143

137144
if (cont)
138145
try
139146
{
140-
return org.apache.commons.io.FileUtils.readFileToByteArray(clazz);
147+
return org.apache.commons.io.FileUtils.readFileToByteArray(classFile);
141148
}
142149
catch (IOException e)
143150
{

src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/KrakatauAssembler.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ public byte[] compile(String contents, String fullyQualifiedName)
5151
if (!ExternalResources.getSingleton().hasSetPython2Command())
5252
return null;
5353

54-
File tempD = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
55-
tempD.mkdir();
54+
final File tempDirectory1 = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
55+
final File tempDirectory2 = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
56+
final File javaFile = new File(tempDirectory1.getAbsolutePath() + FS + fullyQualifiedName + ".j");
57+
final File tempJar = new File(Constants.TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + ".jar");
5658

57-
File tempJ = new File(tempD.getAbsolutePath() + FS + fullyQualifiedName + ".j");
58-
DiskWriter.replaceFile(tempJ.getAbsolutePath(), contents, true);
59+
//create the temp directories
60+
tempDirectory1.mkdir();
61+
tempDirectory2.mkdir();
5962

60-
final File tempDirectory = new File(Constants.TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS);
61-
tempDirectory.mkdir();
63+
//write the file we're assembling to disk
64+
DiskWriter.replaceFile(javaFile.getAbsolutePath(), contents, true);
6265

63-
final File tempJar = new File(Constants.TEMP_DIRECTORY + FS + "temp" + MiscUtils.randomString(32) + ".jar");
66+
//write the entire temporary classpath to disk
6467
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath());
6568

6669
StringBuilder log = new StringBuilder();
@@ -72,7 +75,7 @@ public byte[] compile(String contents, String fullyQualifiedName)
7275
pythonCommands = ArrayUtils.addAll(pythonCommands, "-2");
7376

7477
ProcessBuilder pb = new ProcessBuilder(ArrayUtils.addAll(pythonCommands, "-O", //love you storyyeller <3
75-
krakatauWorkingDirectory + FS + "assemble.py", "-out", tempDirectory.getAbsolutePath(), tempJ.getAbsolutePath()));
78+
krakatauWorkingDirectory + FS + "assemble.py", "-out", tempDirectory2.getAbsolutePath(), javaFile.getAbsolutePath()));
7679

7780
Process process = pb.start();
7881
BytecodeViewer.createdProcesses.add(process);
@@ -101,10 +104,15 @@ public byte[] compile(String contents, String fullyQualifiedName)
101104
log.append(NL).append(NL).append(TranslatedStrings.EXIT_VALUE_IS).append(" ").append(exitValue);
102105
System.err.println(log);
103106

104-
byte[] b = FileUtils.readFileToByteArray(Objects.requireNonNull(ExternalResources.getSingleton().findFile(tempDirectory, ".class")));
105-
tempDirectory.delete();
107+
//read the assembled bytes from disk
108+
byte[] assembledBytes = FileUtils.readFileToByteArray(Objects.requireNonNull(ExternalResources.getSingleton().findFile(tempDirectory2, ".class")));
109+
110+
//cleanup
111+
tempDirectory2.delete();
106112
tempJar.delete();
107-
return b;
113+
114+
//return the assembled file
115+
return assembledBytes;
108116
}
109117
catch (Exception e)
110118
{

src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/SmaliAssembler.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,21 @@ public class SmaliAssembler extends InternalCompiler
4444
@Override
4545
public byte[] compile(String contents, String fullyQualifiedName)
4646
{
47-
String fileStart = TEMP_DIRECTORY + FS + "temp";
48-
int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");
49-
47+
final String fileStart = TEMP_DIRECTORY + FS + "temp";
48+
final int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");
5049
final File tempSmaliFolder = new File(fileStart + fileNumber + "-smalifolder" + FS);
51-
tempSmaliFolder.mkdir();
5250

53-
File tempSmali = new File(tempSmaliFolder.getAbsolutePath() + FS + fileNumber + ".smali");
54-
File tempDex = new File("./out.dex");
55-
File tempJar = new File(fileStart + fileNumber + ".jar");
56-
File tempJarFolder = new File(fileStart + fileNumber + "-jar" + FS);
51+
final File tempSmali = new File(tempSmaliFolder.getAbsolutePath() + FS + fileNumber + ".smali");
52+
final File tempDex = new File("./out.dex");
53+
final File tempJar = new File(fileStart + fileNumber + ".jar");
54+
final File tempJarFolder = new File(fileStart + fileNumber + "-jar" + FS);
55+
56+
//create the temp directory
57+
tempSmaliFolder.mkdir();
5758

5859
try
5960
{
61+
//write the file we're assembling to disk
6062
DiskWriter.replaceFile(tempSmali.getAbsolutePath(), contents, false);
6163
}
6264
catch (Exception e)
@@ -107,6 +109,7 @@ else if (BytecodeViewer.viewer.apkConversionGroup.isSelected(BytecodeViewer.view
107109

108110
System.out.println("Saved as: " + outputClass.getAbsolutePath());
109111

112+
//return the assembled file
110113
return FileUtils.readFileToByteArray(outputClass);
111114
}
112115
catch (java.lang.NullPointerException ignored)

src/main/java/the/bytecode/club/bytecodeviewer/decompilers/InternalDecompiler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
public abstract class InternalDecompiler
2929
{
30-
public abstract String decompileClassNode(ClassNode cn, byte[] b);
30+
public abstract String decompileClassNode(ClassNode cn, byte[] bytes);
3131

3232
public abstract void decompileToZip(String sourceJar, String zipName);
3333
}

src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/FieldNodeDecompiler.java

+8
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, FieldNod
3737
{
3838
String s = getAccessString(f.access);
3939
sb.append(s);
40+
4041
if (s.length() > 0)
4142
sb.append(" ");
43+
4244
sb.append(Type.getType(f.desc).getClassName());
4345
sb.append(" ");
4446
sb.append(f.name);
47+
4548
if (f.value != null)
4649
{
4750
sb.append(" = ");
@@ -59,13 +62,16 @@ public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, FieldNod
5962
sb.append(")");
6063
}
6164
}
65+
6266
sb.append(";");
67+
6368
return sb;
6469
}
6570

6671
private static String getAccessString(int access)
6772
{
6873
List<String> tokens = new ArrayList<>();
74+
6975
if ((access & Opcodes.ACC_PUBLIC) != 0)
7076
tokens.add("public");
7177
if ((access & Opcodes.ACC_PRIVATE) != 0)
@@ -84,13 +90,15 @@ private static String getAccessString(int access)
8490
tokens.add("volatile");
8591
if (tokens.size() == 0)
8692
return "";
93+
8794
// hackery delimeters
8895
StringBuilder sb = new StringBuilder(tokens.get(0));
8996
for (int i = 1; i < tokens.size(); i++)
9097
{
9198
sb.append(" ");
9299
sb.append(tokens.get(i));
93100
}
101+
94102
return sb.toString();
95103
}
96104
}

0 commit comments

Comments
 (0)