Skip to content

Commit

Permalink
Merge pull request #522 from Bl3nd/go-to-enhancement
Browse files Browse the repository at this point in the history
Inner classes and a few additions to the parser
  • Loading branch information
Konloch authored Oct 18, 2024
2 parents edadceb + 9356ebf commit 5f8fd42
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InnerClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler;
import the.bytecode.club.bytecodeviewer.resources.ExternalResources;
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.ExceptionUtils;
import the.bytecode.club.bytecodeviewer.util.ProcessUtils;
import the.bytecode.club.bytecodeviewer.util.TempFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import static the.bytecode.club.bytecodeviewer.Constants.*;
import static the.bytecode.club.bytecodeviewer.translation.TranslatedStrings.*;
Expand All @@ -51,12 +57,67 @@ public FernFlowerDecompiler()
super("FernFlower Decompiler", "fernflower");
}

private String[] inners;
private final List<File> innerFiles = new ArrayList<>();

@Override
public String decompileClassNode(ClassNode cn, byte[] bytes)
{
TempFile tempFile = null;
String exception;

List<InnerClassNode> innerClasses = cn.innerClasses;
List<TempFile> innerTempFiles = new ArrayList<>();
AtomicReference<TempFile> innerTempFile = new AtomicReference<>();
if (BytecodeViewer.viewer.din.isSelected())
{
inners = new String[innerClasses.size()];
for (int i = 0; i < innerClasses.size(); i++)
{
if (innerClasses.get(i).outerName != null && innerClasses.get(i).outerName.equals(cn.name))
{
inners[i] = innerClasses.get(i).name;
}
else if (innerClasses.get(i).outerName == null)
{
String name = innerClasses.get(i).name;
name = name.substring(name.lastIndexOf('/') + 1);
if (name.contains(cn.name.substring(cn.name.lastIndexOf('/') + 1)))
{
inners[i] = innerClasses.get(i).name;
}
}
}

for (ResourceContainer container : BytecodeViewer.resourceContainers.values())
{
container.resourceClasses.forEach((s, classNode) -> {
for (String innerClassName : inners)
{
if (s.equals(innerClassName))
{
innerTempFile.set(TempFile.createTemporaryFile(true, ".class"));
File tempInputClassFile2 = innerTempFile.get().getFile();
try (FileOutputStream fos = new FileOutputStream(tempInputClassFile2))
{
fos.write(ASMUtil.nodeToBytes(classNode));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
innerFiles.add(tempInputClassFile2);
innerTempFile.get().markAsCreatedFile(tempInputClassFile2);
innerTempFiles.add(innerTempFile.get());
}
}
}
});
}
}

try
{
//create the temporary files
Expand Down Expand Up @@ -85,7 +146,12 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
}
else
{
org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.main(generateMainMethod(tempInputClassFile.getAbsolutePath(), new File(TEMP_DIRECTORY).getAbsolutePath()));
List<String> strings = generate(tempInputClassFile.getAbsolutePath(),
new File(TEMP_DIRECTORY).getAbsolutePath());

String[] args = strings.toArray(new String[0]);

org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler.main(args);
}

//if rename is enabled the file name will be the actual class name
Expand All @@ -110,8 +176,23 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
finally
{
//cleanup temp files
if(tempFile != null)
if (tempFile != null)
tempFile.cleanup();

if (innerTempFile.get() != null)
innerTempFile.get().cleanup();

for (TempFile file : innerTempFiles)
{
file.cleanup();
File file1 = new File(TEMP_DIRECTORY + file.getUniqueName() + ".java");
if (file1.exists())
{
file1.delete();
}
}

innerFiles.clear();
}

return FERNFLOWER + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + NL + NL
Expand Down Expand Up @@ -140,31 +221,64 @@ public void decompileToZip(String sourceJar, String zipName)

}

private List<String> generate(String className, String folder)
{
List<String> strings = new ArrayList<>();
strings.add("-rbr=" + ffOnValue(BytecodeViewer.viewer.rbr.isSelected()));
strings.add("-rsy=" + ffOnValue(BytecodeViewer.viewer.rsy.isSelected()));
strings.add("-din=" + ffOnValue(BytecodeViewer.viewer.din.isSelected()));
strings.add("-dc4=" + ffOnValue(BytecodeViewer.viewer.dc4.isSelected()));
strings.add("-das=" + ffOnValue(BytecodeViewer.viewer.das.isSelected()));
strings.add("-hes=" + ffOnValue(BytecodeViewer.viewer.hes.isSelected()));
strings.add("-hdc=" + ffOnValue(BytecodeViewer.viewer.hdc.isSelected()));
strings.add("-dgs=" + ffOnValue(BytecodeViewer.viewer.dgs.isSelected()));
strings.add("-ner=" + ffOnValue(BytecodeViewer.viewer.ner.isSelected()));
strings.add("-den=" + ffOnValue(BytecodeViewer.viewer.den.isSelected()));
strings.add("-rgn=" + ffOnValue(BytecodeViewer.viewer.rgn.isSelected()));
strings.add("-bto=" + ffOnValue(BytecodeViewer.viewer.bto.isSelected()));
strings.add("-nns=" + ffOnValue(BytecodeViewer.viewer.nns.isSelected()));
strings.add("-uto=" + ffOnValue(BytecodeViewer.viewer.uto.isSelected()));
strings.add("-udv=" + ffOnValue(BytecodeViewer.viewer.udv.isSelected()));
strings.add("-rer=" + ffOnValue(BytecodeViewer.viewer.rer.isSelected()));
strings.add("-fdi=" + ffOnValue(BytecodeViewer.viewer.fdi.isSelected()));
strings.add("-asc=" + ffOnValue(BytecodeViewer.viewer.asc.isSelected()));
strings.add("-ren=" + ffOnValue(BytecodeViewer.viewer.ren.isSelected()));
strings.add(className);
if (BytecodeViewer.viewer.din.isSelected())
{
for (File file : innerFiles)
strings.add(file.getAbsolutePath());
}

strings.add(folder);
return strings;
}

private String[] generateMainMethod(String className, String 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
};
{
"-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 ffOnValue(boolean b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@
package the.bytecode.club.bytecodeviewer.decompilers.impl;

import com.konloch.disklib.DiskReader;
import org.apache.commons.io.FilenameUtils;
import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InnerClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.decompilers.AbstractDecompiler;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.CommonPreferences;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.DirectoryLoader;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.JDGUIClassFileUtil;
import the.bytecode.club.bytecodeviewer.decompilers.jdgui.PlainTextPrinter;
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
import the.bytecode.club.bytecodeviewer.util.ExceptionUtils;
import the.bytecode.club.bytecodeviewer.util.TempFile;

import java.io.*;
import java.util.List;

import static the.bytecode.club.bytecodeviewer.Constants.FS;
import static the.bytecode.club.bytecodeviewer.Constants.NL;
Expand All @@ -59,6 +65,26 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
TempFile tempFile = null;
String exception;

List<InnerClassNode> innerClasses = cn.innerClasses;
String[] inners = new String[innerClasses.size()];
for (int i = 0; i < innerClasses.size(); i++)
{
if (innerClasses.get(i).name.equals(cn.name))
break;

if (innerClasses.get(i).outerName != null && innerClasses.get(i).outerName.equals(cn.name))
{
inners[i] = innerClasses.get(i).name;
}
else if (innerClasses.get(i).outerName == null)
{
String name = innerClasses.get(i).name;
name = name.substring(name.lastIndexOf('/') + 1);
if (name.contains(cn.name.substring(cn.name.lastIndexOf('/') + 1)))
inners[i] = innerClasses.get(i).name;
}
}

try
{
//create the temporary files
Expand All @@ -75,9 +101,33 @@ public String decompileClassNode(ClassNode cn, byte[] bytes)
fos.write(bytes);
}

// create the inner class temp files
File innerTempFile;
for (ResourceContainer container : BytecodeViewer.resourceContainers.values())
{
for (String s : container.resourceClasses.keySet())
{
for (String innerClassName : inners)
{
if (s.equals(innerClassName))
{
ClassNode cn2 = container.resourceClasses.get(innerClassName);
tempFile.setUniqueName(cn2.name);
innerTempFile = tempFile.createFileFromExtension(false, false, ".class");
try (FileOutputStream fos = new FileOutputStream(innerTempFile))
{
fos.write(ASMUtil.nodeToBytes(cn2));
}
}
}
}
}

String pathToClass = tempClassFile.getAbsolutePath().replace('/', File.separatorChar).replace('\\', File.separatorChar);
String directoryPath = JDGUIClassFileUtil.ExtractDirectoryPath(pathToClass);
String internalPath = JDGUIClassFileUtil.ExtractInternalPath(directoryPath, pathToClass);
String internalPath = FilenameUtils.removeExtension(JDGUIClassFileUtil.ExtractInternalPath(directoryPath,
pathToClass));


CommonPreferences preferences = new CommonPreferences()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public DirectoryLoader(File file) throws LoaderException
@Override
public byte[] load(String internalPath) throws LoaderException
{
if (!internalPath.endsWith(".class"))
internalPath = internalPath + ".class";

File file = new File(this.codebase, internalPath);

try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis))
Expand All @@ -61,7 +64,7 @@ public byte[] load(String internalPath) throws LoaderException
@Override
public boolean canLoad(String internalPath)
{
File file = new File(this.codebase, internalPath);
File file = new File(this.codebase, internalPath + ".class");
return file.exists() && file.isFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private int yToLine(int y)
if (y < h)
{
float at = y / (float) h;
line = Math.round((Math.max(lineCount, linesPerVisibleRect) - 1) * at);
line = Math.round((float) (Math.max(lineCount, linesPerVisibleRect) - 1) * at);
}

return line;
Expand Down Expand Up @@ -199,7 +199,6 @@ public void removeNotify()

private class Listener extends MouseAdapter
{
private final Rectangle r = new Rectangle();

@Override
public void mouseClicked(@NotNull MouseEvent e)
Expand All @@ -219,7 +218,7 @@ public void mouseClicked(@NotNull MouseEvent e)
{
try
{
int offset = textArea.getLineOfOffset(line);
int offset = textArea.getLineStartOffset(line);
textArea.setCaretPosition(offset);
RSyntaxUtilities.selectAndPossiblyCenter(textArea, new DocumentRange(offset, offset), false);
}
Expand Down
Loading

0 comments on commit 5f8fd42

Please sign in to comment.