Skip to content

Commit a094e52

Browse files
committed
JADX Decompiler Update
1 parent b100479 commit a094e52

1 file changed

Lines changed: 57 additions & 71 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/JADXDecompiler.java

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222
import jadx.api.JadxDecompiler;
2323
import me.konloch.kontainer.io.DiskReader;
2424
import org.objectweb.asm.tree.ClassNode;
25-
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
25+
import the.bytecode.club.bytecodeviewer.Constants;
26+
import the.bytecode.club.bytecodeviewer.Settings;
2627
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
2728
import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler;
2829
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
30+
import the.bytecode.club.bytecodeviewer.util.ExceptionUtils;
2931
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
32+
import the.bytecode.club.bytecodeviewer.util.TempFile;
3033

3134
import java.io.*;
3235

3336
import static the.bytecode.club.bytecodeviewer.Constants.*;
34-
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.ERROR;
35-
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.JADX;
37+
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.*;
3638

3739
/**
3840
* JADX Java Wrapper
@@ -49,96 +51,80 @@ public JADXDecompiler()
4951
@Override
5052
public String decompileClassNode(ClassNode cn, byte[] bytes)
5153
{
52-
String fileStart = TEMP_DIRECTORY + FS;
54+
TempFile tempFile = null;
55+
String exception;
5356

54-
String exception = "";
55-
final File tempClass = new File(MiscUtils.getUniqueNameBroken(fileStart, ".class") + ".class");
56-
57-
try (FileOutputStream fos = new FileOutputStream(tempClass))
58-
{
59-
fos.write(bytes);
60-
}
61-
catch (IOException e)
57+
try
6258
{
63-
BytecodeViewer.handleException(e);
64-
}
59+
//create the temporary files
60+
tempFile = TempFile.createTemporaryFile(true, ".class");
61+
File tempDirectory = tempFile.getParent();
62+
File tempClassFile = tempFile.getFile();
6563

66-
File freeDirectory = new File(findUnusedFile(fileStart));
67-
freeDirectory.mkdirs();
64+
//write the class-file with bytes
65+
try (FileOutputStream fos = new FileOutputStream(tempClassFile))
66+
{
67+
fos.write(bytes);
68+
}
6869

69-
try
70-
{
70+
//setup JADX Args
7171
JadxArgs args = new JadxArgs();
72-
args.setInputFile(tempClass);
73-
args.setOutDir(freeDirectory);
74-
args.setOutDirSrc(freeDirectory);
75-
args.setOutDirRes(freeDirectory);
72+
args.setInputFile(tempClassFile);
73+
args.setOutDir(tempDirectory);
74+
args.setOutDirSrc(tempDirectory);
75+
args.setOutDirRes(tempDirectory);
7676

77+
//init jadx decompiler
7778
JadxDecompiler jadx = new JadxDecompiler(args);
79+
80+
//load jadx
7881
jadx.load();
82+
83+
//decompile
7984
jadx.saveSources();
85+
86+
//handle simulated errors
87+
if(Constants.DEV_FLAG_DECOMPILERS_SIMULATED_ERRORS)
88+
throw new RuntimeException(DEV_MODE_SIMULATED_ERROR.toString());
89+
90+
return searchForJavaFile(MiscUtils.listFiles(tempDirectory));
8091
}
81-
catch (StackOverflowError | Exception e)
92+
catch (Throwable e)
8293
{
83-
StringWriter exceptionWriter = new StringWriter();
84-
e.printStackTrace(new PrintWriter(exceptionWriter));
85-
e.printStackTrace();
86-
exception = exceptionWriter.toString();
94+
exception = ExceptionUtils.exceptionToString(e);
8795
}
88-
89-
tempClass.delete();
90-
91-
if (freeDirectory.exists())
92-
return findFile(MiscUtils.listFiles(freeDirectory));
93-
94-
if (exception.isEmpty())
95-
exception = "Decompiled source file not found!";
96-
97-
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
98-
}
99-
100-
public String findUnusedFile(String start)
101-
{
102-
long index = 0;
103-
104-
while (true)
96+
finally
10597
{
106-
File f = new File(start + index);
107-
108-
if (!f.exists())
109-
return f.toString();
98+
//cleanup temp files
99+
if(tempFile != null)
100+
tempFile.cleanup();
110101
}
102+
103+
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
104+
+ TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
111105
}
112106

113-
public String findFile(File[] fileArray)
107+
public String searchForJavaFile(File[] files) throws Exception
114108
{
115-
for (File f : fileArray)
109+
for (File file : files)
116110
{
117-
if (f.isDirectory())
118-
return findFile(MiscUtils.listFiles(f));
119-
else
111+
if (file.isDirectory())
112+
return searchForJavaFile(MiscUtils.listFiles(file));
113+
else if(file.getName().toLowerCase().endsWith(".java"))
120114
{
121-
String s;
122-
123-
try
124-
{
125-
s = DiskReader.loadAsString(f.getAbsolutePath());
126-
}
127-
catch (Exception e)
128-
{
129-
StringWriter sw = new StringWriter();
130-
e.printStackTrace(new PrintWriter(sw));
131-
e.printStackTrace();
132-
String exception = ExceptionUI.SEND_STACKTRACE_TO_NL + sw;
133-
134-
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL + exception;
135-
}
136-
137-
return s;
115+
String contents = DiskReader.loadAsString(file.getAbsolutePath());
116+
117+
//cleanup
118+
if(Settings.DECOMPILERS_AUTOMATICALLY_CLEANUP)
119+
file.delete();
120+
121+
return contents;
138122
}
139123
}
140124

141-
return "JADX error!" + NL + NL + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR;
125+
return JADX + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
126+
+ TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + NL + NL
127+
+ "JADX failed to produce any Java files from the provided source.";
142128
}
143129

144130
@Override

0 commit comments

Comments
 (0)