FernFlower Decompiler Changes

Uses the new Temp Files API. Exceptions are properly handled. File system errors are properly displayed. No more temporary files left behind. `-ren` / rename classes option fixed.
This commit is contained in:
Konloch 2024-10-02 12:08:34 -06:00
parent 96495c8469
commit b69389cc5f

AI 샘플 코드 생성 중입니다

Loading...

View File

@ -24,7 +24,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.TempFile;
import java.io.*;
@ -72,23 +72,20 @@ public class FernFlowerDecompiler extends AbstractDecompiler
@Override
public String decompileClassNode(ClassNode cn, byte[] bytes)
{
String start = TEMP_DIRECTORY + FS + MiscUtils.getUniqueNameBroken("", ".class");
String exception = "This decompiler didn't throw an exception - this is probably a BCV logical bug";
final File tempClass = new File(start + ".class");
try
{
final TempFile tempFile = TempFile.createTemporaryFile(true, ".class");
final File tempClassFile = tempFile.createFileFromExtension(false, false, ".class");
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");
String exception = "";
try (FileOutputStream fos = new FileOutputStream(tempClass))
try (FileOutputStream fos = new FileOutputStream(tempClassFile))
{
fos.write(bytes);
}
catch (IOException e)
{
StringWriter exceptionWriter = new StringWriter();
e.printStackTrace(new PrintWriter(exceptionWriter));
e.printStackTrace();
exception = exceptionWriter.toString();
}
if (LAUNCH_DECOMPILERS_IN_NEW_PROCESS)
{
@ -111,39 +108,35 @@ public class FernFlowerDecompiler extends AbstractDecompiler
}
else
{
try
{
org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.main(generateMainMethod(tempClass.getAbsolutePath(), new File(TEMP_DIRECTORY).getAbsolutePath()));
}
catch (Throwable e)
{
StringWriter exceptionWriter = new StringWriter();
e.printStackTrace(new PrintWriter(exceptionWriter));
e.printStackTrace();
exception = exceptionWriter.toString();
}
org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.main(generateMainMethod(tempClassFile.getAbsolutePath(), new File(TEMP_DIRECTORY).getAbsolutePath()));
}
tempClass.delete();
tempClassFile.delete();
String javaDir = start;
//if rename is enabled the file name will be the actual class name
if (BytecodeViewer.viewer.ren.isSelected())
{
javaDir = TEMP_DIRECTORY + "class_0";
int indexOfLastPackage = cn.name.lastIndexOf('/');
String classNameNoPackages = indexOfLastPackage < 0 ? cn.name : cn.name.substring(indexOfLastPackage);
tempOutputJavaFile = new File(tempFile.getParent(), classNameNoPackages + ".java");
tempFile.markAsCreatedFile(tempOutputJavaFile);
}
final File outputJava = new File(javaDir + ".java");
if (outputJava.exists())
//if the output file is found, read it
if (tempOutputJavaFile.exists())
{
String s;
try
{
s = DiskReader.loadAsString(outputJava.getAbsolutePath());
String s = DiskReader.loadAsString(tempOutputJavaFile.getAbsolutePath());
outputJava.delete();
//cleanup temp files
tempFile.delete();
return s;
}
else
{
exception = "BCV Error: " + tempOutputJavaFile.getAbsolutePath() + " does not exist.";
}
}
catch (Exception e)
{
StringWriter exceptionWriter = new StringWriter();
@ -152,44 +145,43 @@ public class FernFlowerDecompiler extends AbstractDecompiler
exception += NL + NL + exceptionWriter;
}
}
return FERNFLOWER + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
return FERNFLOWER + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
+ TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
}
private String[] generateMainMethod(String className, String folder)
{
return new String[]{"-rbr=" + r(BytecodeViewer.viewer.rbr.isSelected()),
"-rsy=" + r(BytecodeViewer.viewer.rsy.isSelected()),
"-din=" + r(BytecodeViewer.viewer.din.isSelected()),
"-dc4=" + r(BytecodeViewer.viewer.dc4.isSelected()),
"-das=" + r(BytecodeViewer.viewer.das.isSelected()),
"-hes=" + r(BytecodeViewer.viewer.hes.isSelected()),
"-hdc=" + r(BytecodeViewer.viewer.hdc.isSelected()),
"-dgs=" + r(BytecodeViewer.viewer.dgs.isSelected()),
"-ner=" + r(BytecodeViewer.viewer.ner.isSelected()),
"-den=" + r(BytecodeViewer.viewer.den.isSelected()),
"-rgn=" + r(BytecodeViewer.viewer.rgn.isSelected()),
"-bto=" + r(BytecodeViewer.viewer.bto.isSelected()),
"-nns=" + r(BytecodeViewer.viewer.nns.isSelected()),
"-uto=" + r(BytecodeViewer.viewer.uto.isSelected()),
"-udv=" + r(BytecodeViewer.viewer.udv.isSelected()),
"-rer=" + r(BytecodeViewer.viewer.rer.isSelected()),
"-fdi=" + r(BytecodeViewer.viewer.fdi.isSelected()),
"-asc=" + r(BytecodeViewer.viewer.asc.isSelected()),
"-ren=" + r(BytecodeViewer.viewer.ren.isSelected()),
className, folder};
return new String[]
{
"-rbr=" + ffOnValue(BytecodeViewer.viewer.rbr.isSelected()),
"-rsy=" + ffOnValue(BytecodeViewer.viewer.rsy.isSelected()),
"-din=" + ffOnValue(BytecodeViewer.viewer.din.isSelected()),
"-dc4=" + ffOnValue(BytecodeViewer.viewer.dc4.isSelected()),
"-das=" + ffOnValue(BytecodeViewer.viewer.das.isSelected()),
"-hes=" + ffOnValue(BytecodeViewer.viewer.hes.isSelected()),
"-hdc=" + ffOnValue(BytecodeViewer.viewer.hdc.isSelected()),
"-dgs=" + ffOnValue(BytecodeViewer.viewer.dgs.isSelected()),
"-ner=" + ffOnValue(BytecodeViewer.viewer.ner.isSelected()),
"-den=" + ffOnValue(BytecodeViewer.viewer.den.isSelected()),
"-rgn=" + ffOnValue(BytecodeViewer.viewer.rgn.isSelected()),
"-bto=" + ffOnValue(BytecodeViewer.viewer.bto.isSelected()),
"-nns=" + ffOnValue(BytecodeViewer.viewer.nns.isSelected()),
"-uto=" + ffOnValue(BytecodeViewer.viewer.uto.isSelected()),
"-udv=" + ffOnValue(BytecodeViewer.viewer.udv.isSelected()),
"-rer=" + ffOnValue(BytecodeViewer.viewer.rer.isSelected()),
"-fdi=" + ffOnValue(BytecodeViewer.viewer.fdi.isSelected()),
"-asc=" + ffOnValue(BytecodeViewer.viewer.asc.isSelected()),
"-ren=" + ffOnValue(BytecodeViewer.viewer.ren.isSelected()),
className, folder
};
}
private String r(boolean b)
private String ffOnValue(boolean b)
{
if (b)
{
return "1";
}
else
{
return "0";
}
}
}