From 0c53463fd791f290cb0542ecf0cce007c2343b02 Mon Sep 17 00:00:00 2001 From: Konloch Date: Sat, 3 Jul 2021 23:24:54 -0700 Subject: [PATCH] Compiler Cleanup --- .../bytecodeviewer/compilers/Compiler.java | 4 +++ .../compilers/InternalCompiler.java | 4 +-- .../compilers/{ => impl}/JavaCompiler.java | 21 ++++++------ .../{ => impl}/KrakatauAssembler.java | 32 +++++++++++-------- .../compilers/{ => impl}/SmaliAssembler.java | 21 ++++++------ 5 files changed, 46 insertions(+), 36 deletions(-) rename src/main/java/the/bytecode/club/bytecodeviewer/compilers/{ => impl}/JavaCompiler.java (93%) rename src/main/java/the/bytecode/club/bytecodeviewer/compilers/{ => impl}/KrakatauAssembler.java (87%) rename src/main/java/the/bytecode/club/bytecodeviewer/compilers/{ => impl}/SmaliAssembler.java (89%) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/Compiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/Compiler.java index 4050643f..e32fae5a 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/Compiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/Compiler.java @@ -18,6 +18,10 @@ package the.bytecode.club.bytecodeviewer.compilers; * along with this program. If not, see . * ***************************************************************************/ +import the.bytecode.club.bytecodeviewer.compilers.impl.JavaCompiler; +import the.bytecode.club.bytecodeviewer.compilers.impl.KrakatauAssembler; +import the.bytecode.club.bytecodeviewer.compilers.impl.SmaliAssembler; + /** * A collection of all of the supported compilers/assemblers inside of BCV * diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/InternalCompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/InternalCompiler.java index 6186457b..be5d3919 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/InternalCompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/InternalCompiler.java @@ -26,7 +26,5 @@ package the.bytecode.club.bytecodeviewer.compilers; public abstract class InternalCompiler { - public abstract byte[] compile(String contents, String name); - -} +} \ No newline at end of file diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/JavaCompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/JavaCompiler.java similarity index 93% rename from src/main/java/the/bytecode/club/bytecodeviewer/compilers/JavaCompiler.java rename to src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/JavaCompiler.java index 4d124ad1..64f7a9bc 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/JavaCompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/JavaCompiler.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bytecodeviewer.compilers; +package the.bytecode.club.bytecodeviewer.compilers.impl; import java.io.BufferedReader; import java.io.File; @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import me.konloch.kontainer.io.DiskWriter; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; +import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler; import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -39,7 +40,6 @@ import static the.bytecode.club.bytecodeviewer.Constants.*; public class JavaCompiler extends InternalCompiler { - @Override public byte[] compile(String contents, String name) { @@ -102,7 +102,8 @@ public class JavaCompiler extends InternalCompiler } } - if (process.isAlive()) { + if (process.isAlive()) + { System.out.println("Force killing javac process, assuming it's gotten stuck"); process.destroyForcibly().destroy(); } @@ -116,18 +117,20 @@ public class JavaCompiler extends InternalCompiler InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; - while ((line = br.readLine()) != null) { + + while ((line = br.readLine()) != null) log.append(nl).append(line); - } + br.close(); log.append(nl).append(nl).append("Error:").append(nl).append(nl); is = process.getErrorStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); - while ((line = br.readLine()) != null) { + + while ((line = br.readLine()) != null) log.append(nl).append(line); - } + br.close(); log.append(nl).append(nl).append("Exit Value is ").append(exitValue); @@ -135,12 +138,12 @@ public class JavaCompiler extends InternalCompiler if (!clazz.exists()) throw new Exception(log.toString()); - } catch (Exception e) { cont = false; e.printStackTrace(); + } finally { + BytecodeViewer.sm.setBlocking(); } - BytecodeViewer.sm.setBlocking(); cp.delete(); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/KrakatauAssembler.java b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/KrakatauAssembler.java similarity index 87% rename from src/main/java/the/bytecode/club/bytecodeviewer/compilers/KrakatauAssembler.java rename to src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/KrakatauAssembler.java index 05418429..5ba485a6 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/KrakatauAssembler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/KrakatauAssembler.java @@ -1,13 +1,15 @@ -package the.bytecode.club.bytecodeviewer.compilers; +package the.bytecode.club.bytecodeviewer.compilers.impl; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import me.konloch.kontainer.io.DiskWriter; +import org.apache.commons.io.FileUtils; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Constants; +import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler; import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -51,11 +53,9 @@ public class KrakatauAssembler extends InternalCompiler return null; } - String origName = name; - name = MiscUtils.randomString(20); + String origName = MiscUtils.randomString(20); - File tempD = - new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs); + File tempD = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs); tempD.mkdir(); File tempJ = new File(tempD.getAbsolutePath() + fs + name + ".j"); @@ -63,12 +63,15 @@ public class KrakatauAssembler extends InternalCompiler final File tempDirectory = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs); tempDirectory.mkdir(); + final File tempJar = new File(Constants.tempDirectory + fs + "temp" + MiscUtils.randomString(32) + ".jar"); JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath()); - - BytecodeViewer.sm.stopBlocking(); + StringBuilder log = new StringBuilder(); - try { + + BytecodeViewer.sm.stopBlocking(); + try + { ProcessBuilder pb = new ProcessBuilder( Configuration.python, "-O", //love you storyyeller <3 @@ -86,26 +89,27 @@ public class KrakatauAssembler extends InternalCompiler InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; - while ((line = br.readLine()) != null) { + + while ((line = br.readLine()) != null) log.append(nl).append(line); - } + br.close(); log.append(nl).append(nl).append("Error:").append(nl).append(nl); is = process.getErrorStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); - while ((line = br.readLine()) != null) { + + while ((line = br.readLine()) != null) log.append(nl).append(line); - } + br.close(); int exitValue = process.waitFor(); log.append(nl).append(nl).append("Exit Value is ").append(exitValue); System.out.println(log); - byte[] b = - org.apache.commons.io.FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class")); + byte[] b = FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class")); tempDirectory.delete(); tempJar.delete(); return b; diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/SmaliAssembler.java b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/SmaliAssembler.java similarity index 89% rename from src/main/java/the/bytecode/club/bytecodeviewer/compilers/SmaliAssembler.java rename to src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/SmaliAssembler.java index 2f71e430..20887b11 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/compilers/SmaliAssembler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/compilers/impl/SmaliAssembler.java @@ -1,9 +1,11 @@ -package the.bytecode.club.bytecodeviewer.compilers; +package the.bytecode.club.bytecodeviewer.compilers.impl; import java.io.File; import java.util.Objects; import me.konloch.kontainer.io.DiskWriter; +import org.apache.commons.io.FileUtils; import the.bytecode.club.bytecodeviewer.BytecodeViewer; +import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler; import the.bytecode.club.bytecodeviewer.util.Dex2Jar; import the.bytecode.club.bytecodeviewer.util.Enjarify; import the.bytecode.club.bytecodeviewer.util.MiscUtils; @@ -37,9 +39,9 @@ import static the.bytecode.club.bytecodeviewer.Constants.*; public class SmaliAssembler extends InternalCompiler { - @Override - public byte[] compile(String contents, String name) { + public byte[] compile(String contents, String name) + { String fileStart = tempDirectory + fs + "temp"; int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex"); @@ -79,21 +81,20 @@ public class SmaliAssembler extends InternalCompiler boolean found = false; File current = tempJarFolder; try { - while (!found) { + while (!found) + { File f = Objects.requireNonNull(current.listFiles())[0]; if (f.isDirectory()) current = f; - else { + else + { outputClass = f; found = true; } - } - return org.apache.commons.io.FileUtils.readFileToByteArray(outputClass); - } catch (java.lang.NullPointerException ignored) { - - } + return FileUtils.readFileToByteArray(outputClass); + } catch (java.lang.NullPointerException ignored) { } } catch (Exception e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); }