From 03d74f3dedfcc3f5e780a34fe316f8624965f0be Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 2 Oct 2024 12:13:00 -0600 Subject: [PATCH] FernFlower Decompiler Refactoring --- .../impl/FernFlowerDecompiler.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/FernFlowerDecompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/FernFlowerDecompiler.java index 26331439..2ed9b0d9 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/FernFlowerDecompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/FernFlowerDecompiler.java @@ -72,21 +72,25 @@ public class FernFlowerDecompiler extends AbstractDecompiler @Override public String decompileClassNode(ClassNode cn, byte[] bytes) { + TempFile tempFile = null; String exception = "This decompiler didn't throw an exception - this is probably a BCV logical bug"; try { - final TempFile tempFile = TempFile.createTemporaryFile(true, ".class"); - final File tempClassFile = tempFile.createFileFromExtension(false, false, ".class"); + tempFile = TempFile.createTemporaryFile(true, ".class"); + File tempClassFile = tempFile.createFileFromExtension(false, false, ".class"); + + //load java source from temp directory tempFile.setParent(new File(TEMP_DIRECTORY)); File tempOutputJavaFile = tempFile.createFileFromExtension(false, true, ".java"); - //File tempOutputJavaFile = new File(TEMP_DIRECTORY, tempClassFile.getName().substring(0, tempClassFile.getName().length()-6) + ".java"); + //write the class-file with bytes try (FileOutputStream fos = new FileOutputStream(tempClassFile)) { fos.write(bytes); } + //decompile the class-file if (LAUNCH_DECOMPILERS_IN_NEW_PROCESS) { /*try @@ -111,8 +115,6 @@ public class FernFlowerDecompiler extends AbstractDecompiler org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.main(generateMainMethod(tempClassFile.getAbsolutePath(), new File(TEMP_DIRECTORY).getAbsolutePath())); } - tempClassFile.delete(); - //if rename is enabled the file name will be the actual class name if (BytecodeViewer.viewer.ren.isSelected()) { @@ -125,12 +127,12 @@ public class FernFlowerDecompiler extends AbstractDecompiler //if the output file is found, read it if (tempOutputJavaFile.exists()) { - String s = DiskReader.loadAsString(tempOutputJavaFile.getAbsolutePath()); + String javaSource = DiskReader.loadAsString(tempOutputJavaFile.getAbsolutePath()); //cleanup temp files tempFile.delete(); - return s; + return javaSource; } else { @@ -146,6 +148,10 @@ public class FernFlowerDecompiler extends AbstractDecompiler exception += NL + NL + exceptionWriter; } + //cleanup temp files + if(tempFile != null) + tempFile.delete(); + return FERNFLOWER + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception; }