From b70002e9d544b832d6c962bd477e1bc23092cfd6 Mon Sep 17 00:00:00 2001 From: afffsdd Date: Thu, 24 Dec 2015 19:24:29 -0500 Subject: [PATCH] Make it so you can have two classes of the same name from different jars open. --- .../club/bytecodeviewer/BytecodeViewer.java | 9 ++-- .../club/bytecodeviewer/CommandLineInput.java | 5 ++- .../bytecodeviewer/FileChangeNotifier.java | 4 +- .../bytecodeviewer/api/BytecodeViewer.java | 4 +- .../bytecode/ClassNodeDecompiler.java | 19 +++++++-- .../club/bytecodeviewer/gui/ClassViewer.java | 10 +++-- .../gui/FileNavigationPane.java | 15 +++---- .../club/bytecodeviewer/gui/FileViewer.java | 4 +- .../bytecodeviewer/gui/MainViewerGUI.java | 25 +++++------ .../bytecodeviewer/gui/PaneUpdaterThread.java | 2 +- .../bytecodeviewer/gui/SearchingPane.java | 7 ++-- .../bytecodeviewer/gui/VisibleComponent.java | 4 +- .../club/bytecodeviewer/gui/WorkPane.java | 42 ++++++++++--------- 13 files changed, 87 insertions(+), 63 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java index bcf013a6..513e6111 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java @@ -427,21 +427,22 @@ public class BytecodeViewer { /** * Returns the ClassNode by the specified name * + * @param containerName name of the FileContainer that this class is in * @param name the class name * @return the ClassNode instance */ - public static ClassNode getClassNode(String name) { + public static ClassNode getClassNode(String containerName, String name) { for (FileContainer container : files) { - if (container.getData().containsKey(name + ".class")) { + if (container.name.equals(containerName) && container.getData().containsKey(name + ".class")) { return container.getClassNode(name); } } return null; } - public static byte[] getClassBytes(String name) { + public static byte[] getClassBytes(String containerName, String name) { for (FileContainer container : files) { - if (container.getData().containsKey(name)) { + if (container.name.equals(containerName) && container.getData().containsKey(name)) { return container.getData().get(name); } } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java b/src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java index ac6281f8..57288ea1 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java @@ -160,13 +160,14 @@ public class CommandLineInput { System.out.println("Decompiling " + input.getAbsolutePath() + " with " + use.getName()); BytecodeViewer.openFiles(new File[]{input}, false); + String containerName = BytecodeViewer.files.get(0).name; Thread.sleep(5 * 1000); if (target.equalsIgnoreCase("all")) { use.decompileToZip(output.getAbsolutePath()); } else { try { - ClassNode cn = BytecodeViewer.getClassNode(target); - byte[] bytes = BytecodeViewer.getClassBytes(target); + ClassNode cn = BytecodeViewer.getClassNode(containerName, target); + byte[] bytes = BytecodeViewer.getClassBytes(containerName, target); String contents = use.decompileClassNode(cn, bytes); FileUtils.write(output, contents, "UTF-8", false); } catch (Exception e) { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/FileChangeNotifier.java b/src/main/java/the/bytecode/club/bytecodeviewer/FileChangeNotifier.java index 03fc84af..ee4d69af 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/FileChangeNotifier.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/FileChangeNotifier.java @@ -28,6 +28,6 @@ import org.objectweb.asm.tree.ClassNode; */ public interface FileChangeNotifier { - void openClassFile(String name, ClassNode cn); - void openFile(String name, byte[] contents); + void openClassFile(String name, String container, ClassNode cn); + void openFile(String name, String container, byte[] contents); } \ No newline at end of file diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java index e54bc6ee..51a7d43f 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/api/BytecodeViewer.java @@ -154,8 +154,8 @@ public class BytecodeViewer { * the full name of the ClassNode * @return the ClassNode */ - public static ClassNode getClassNode(String name) { - return the.bytecode.club.bytecodeviewer.BytecodeViewer.getClassNode(name); + public static ClassNode getClassNode(String containerName, String name) { + return the.bytecode.club.bytecodeviewer.BytecodeViewer.getClassNode(containerName, name); } /** diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/ClassNodeDecompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/ClassNodeDecompiler.java index ec9f71e0..a44aae63 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/ClassNodeDecompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/ClassNodeDecompiler.java @@ -7,6 +7,7 @@ import org.objectweb.asm.tree.InnerClassNode; import org.objectweb.asm.tree.MethodNode; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.DecompilerSettings; +import the.bytecode.club.bytecodeviewer.FileContainer; import the.bytecode.club.bytecodeviewer.decompilers.Decompiler; import java.util.ArrayList; @@ -51,10 +52,20 @@ public class ClassNodeDecompiler extends Decompiler { } public String decompileClassNode(ClassNode cn, byte[] b) { - return decompile(new PrefixedStringBuilder(), new ArrayList(), cn).toString(); + String containerName = null; + for (FileContainer container : BytecodeViewer.files) { + String name = cn.name + ".class"; + if (container.getData().containsKey(name)) { + if (container.getClassNode(name) == cn) + containerName = container.name; + } + } + System.out.println(containerName); + + return decompile(new PrefixedStringBuilder(), new ArrayList(), containerName, cn).toString(); } - protected static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList decompiledClasses, ClassNode cn) { + protected static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList decompiledClasses, String containerName, ClassNode cn) { ArrayList unableToDecompile = new ArrayList(); decompiledClasses.add(cn.name); sb.append(getAccessString(cn.access)); @@ -97,11 +108,11 @@ public class ClassNodeDecompiler extends Decompiler { String innerClassName = innerClassNode.name; if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) { decompiledClasses.add(innerClassName); - ClassNode cn1 = BytecodeViewer.getClassNode(innerClassName); + ClassNode cn1 = BytecodeViewer.getClassNode(containerName, innerClassName); if (cn1 != null) { sb.appendPrefix(" "); sb.append(BytecodeViewer.nl + BytecodeViewer.nl); - sb = decompile(sb, decompiledClasses, cn1); + sb = decompile(sb, decompiledClasses, containerName,cn1); sb.trimPrefix(5); sb.append(BytecodeViewer.nl); } else { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/ClassViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/ClassViewer.java index 98004945..5f0db855 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/ClassViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/ClassViewer.java @@ -120,6 +120,7 @@ public class ClassViewer extends Viewer { } String name; + String container; JSplitPane sp; JSplitPane sp2; public List decompilers = Arrays.asList(null, null, null); @@ -273,7 +274,7 @@ public class ClassViewer extends Viewer { } } - public ClassViewer(final String name, final ClassNode cn) { + public ClassViewer(final String name, final String container, final ClassNode cn) { for (int i = 0; i < panels.size(); i++) { final JTextField textField = fields.get(i); JPanel searchPanel = searches.get(i); @@ -318,12 +319,13 @@ public class ClassViewer extends Viewer { } this.name = name; + this.container = container; this.cn = cn; - this.setName(name); + this.setName(name + "(" + container + ")"); this.setLayout(new BorderLayout()); this.sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panels.get(0), panels.get(1)); - JHexEditor hex = new JHexEditor(BytecodeViewer.getClassBytes(cn.name + ".class")); + JHexEditor hex = new JHexEditor(BytecodeViewer.getClassBytes(container, cn.name + ".class")); this.sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, panels.get(2)); this.add(sp2, BorderLayout.CENTER); @@ -365,7 +367,7 @@ public class ClassViewer extends Viewer { } public void startPaneUpdater(final JButton button) { - this.cn = BytecodeViewer.getClassNode(cn.name); //update the classnode + this.cn = BytecodeViewer.getClassNode(container, cn.name); //update the classnode setPanes(); for (JPanel jpanel : panels) { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileNavigationPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileNavigationPane.java index bfcaf83e..151d5c89 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileNavigationPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileNavigationPane.java @@ -234,12 +234,12 @@ public class FileNavigationPane extends VisibleComponent implements FileDrop.Lis new FileDrop(this, this); } - public void openClassFileToWorkSpace(final String name, final ClassNode node) { - fcn.openClassFile(name, node); + public void openClassFileToWorkSpace(final String name, final String container, final ClassNode node) { + fcn.openClassFile(name, container, node); } - public void openFileToWorkSpace(String name, byte[] contents) { - fcn.openFile(name, contents); + public void openFileToWorkSpace(String name, final String container, byte[] contents) { + fcn.openFile(name, container, contents); } @Override @@ -454,13 +454,14 @@ public class FileNavigationPane extends VisibleComponent implements FileDrop.Lis } String name = nameBuffer.toString(); + String containerName = path.getPathComponent(1).toString(); if (name.endsWith(".class")) { - final ClassNode cn = BytecodeViewer.getClassNode(name.substring(0, name.length() - ".class".length())); + final ClassNode cn = BytecodeViewer.getClassNode(containerName, name.substring(0, name.length() - ".class".length())); if (cn != null) { - openClassFileToWorkSpace(nameBuffer.toString(), cn); + openClassFileToWorkSpace(nameBuffer.toString(), containerName, cn); } } else { - openFileToWorkSpace(nameBuffer.toString(), BytecodeViewer.getFileContents(nameBuffer.toString())); + openFileToWorkSpace(nameBuffer.toString(), containerName, BytecodeViewer.getFileContents(nameBuffer.toString())); } } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileViewer.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileViewer.java index ab4d43bd..59be284b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileViewer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/FileViewer.java @@ -51,6 +51,7 @@ public class FileViewer extends Viewer { private static final long serialVersionUID = 6103372882168257164L; String name; + String container; private byte[] contents; RSyntaxTextArea panelArea = new RSyntaxTextArea(); JPanel panel = new JPanel(new BorderLayout()); @@ -189,8 +190,9 @@ public class FileViewer extends Viewer { return asciiEncoder.canEncode(v); } - public FileViewer(final String name, final byte[] contents) { + public FileViewer(final String name, final String container, final byte[] contents) { this.name = name; + this.container = container; this.contents = contents; this.setName(name); this.setLayout(new BorderLayout()); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java index 3ec19e9b..0c946e45 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java @@ -791,14 +791,15 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier { int result = -1; for (int k = 0; k < options.length; k++) if (options[k].equals(obj)) result = k; + String containerName = BytecodeViewer.files.get(0).name; if (result == 0) { Thread t = new Thread() { @Override public void run() { try { - ClassNode cn = BytecodeViewer.getClassNode(s); - byte[] bytes = BytecodeViewer.getClassBytes(s); + ClassNode cn = BytecodeViewer.getClassNode(containerName, s); + byte[] bytes = BytecodeViewer.getClassBytes(containerName, s); String contents = Decompiler.PROCYON.decompileClassNode(cn, bytes); FileUtils.write(new File(path), contents, "UTF-8", false); BytecodeViewer.viewer.setIcon(false); @@ -814,8 +815,8 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier { @Override public void run() { try { - ClassNode cn = BytecodeViewer.getClassNode(s); - byte[] bytes = BytecodeViewer.getClassBytes(s); + ClassNode cn = BytecodeViewer.getClassNode(containerName, s); + byte[] bytes = BytecodeViewer.getClassBytes(containerName, s); String contents = Decompiler.CFR.decompileClassNode(cn, bytes); FileUtils.write(new File(path), contents, "UTF-8", false); BytecodeViewer.viewer.setIcon(false); @@ -831,8 +832,8 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier { @Override public void run() { try { - ClassNode cn = BytecodeViewer.getClassNode(s); - byte[] bytes = BytecodeViewer.getClassBytes(s); + ClassNode cn = BytecodeViewer.getClassNode(containerName, s); + byte[] bytes = BytecodeViewer.getClassBytes(containerName, s); String contents = Decompiler.FERNFLOWER.decompileClassNode(cn, bytes); FileUtils.write(new File(path), contents, "UTF-8", false); BytecodeViewer.viewer.setIcon(false); @@ -848,8 +849,8 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier { @Override public void run() { try { - ClassNode cn = BytecodeViewer.getClassNode(s); - byte[] bytes = BytecodeViewer.getClassBytes(s); + ClassNode cn = BytecodeViewer.getClassNode(containerName, s); + byte[] bytes = BytecodeViewer.getClassBytes(containerName, s); String contents = Decompiler.KRAKATAU.decompileClassNode(cn, bytes); FileUtils.write(new File(path), contents, "UTF-8", false); BytecodeViewer.viewer.setIcon(false); @@ -1258,16 +1259,16 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier { } @Override - public void openClassFile(final String name, final ClassNode cn) { + public void openClassFile(final String name, String container, final ClassNode cn) { for (final VisibleComponent vc : rfComps) { - vc.openClassFile(name, cn); + vc.openClassFile(name, container, cn); } } @Override - public void openFile(final String name, byte[] content) { + public void openFile(final String name, String container, byte[] content) { for (final VisibleComponent vc : rfComps) { - vc.openFile(name, content); + vc.openFile(name, container, content); } } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/PaneUpdaterThread.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/PaneUpdaterThread.java index 0db6550b..1a9b566f 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/PaneUpdaterThread.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/PaneUpdaterThread.java @@ -54,7 +54,7 @@ public class PaneUpdaterThread extends Thread { public void run() { try { - final byte[] b = BytecodeViewer.getClassBytes(viewer.cn.name + ".class"); + final byte[] b = BytecodeViewer.getClassBytes(viewer.container, viewer.cn.name + ".class"); if (decompiler != Decompiler.HEXCODE) { RSyntaxTextArea panelArea = new RSyntaxTextArea(); panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/SearchingPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/SearchingPane.java index 3c291d6b..655f6c4d 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/SearchingPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/SearchingPane.java @@ -195,12 +195,13 @@ public class SearchingPane extends VisibleComponent { @Override public void valueChanged(final TreeSelectionEvent arg0) { String path = arg0.getPath().toString(); + String containerName = arg0.getPath().getPathComponent(1).toString(); String className = path.split(", ")[1].split("\\.")[0]; - final ClassNode fN = BytecodeViewer.getClassNode(className); + final ClassNode fN = BytecodeViewer.getClassNode(containerName, className); if (fN != null) { MainViewerGUI.getComponent(FileNavigationPane.class) - .openClassFileToWorkSpace(className + ".class", fN); + .openClassFileToWorkSpace(className + ".class", containerName, fN); } System.out.println(className); @@ -232,7 +233,7 @@ public class SearchingPane extends VisibleComponent { } @Override - public void openFile(String name, byte[] contents) { + public void openFile(String name, String container, byte[] contents) { // TODO Auto-generated method stub } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/VisibleComponent.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/VisibleComponent.java index 7f8bca1a..c60c043f 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/VisibleComponent.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/VisibleComponent.java @@ -49,10 +49,10 @@ public abstract class VisibleComponent extends JInternalFrame implements } @Override - public void openClassFile(final String name, final ClassNode cn) { + public void openClassFile(final String name, String container, final ClassNode cn) { } @Override - public void openFile(final String name, byte[] contents) { + public void openFile(final String name, String container, byte[] contents) { } } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/WorkPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/WorkPane.java index 42498a94..d0acc8ba 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/WorkPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/WorkPane.java @@ -85,10 +85,12 @@ public class WorkPane extends VisibleComponent implements ActionListener { public void componentRemoved(final ContainerEvent e) { final Component c = e.getChild(); if (c instanceof ClassViewer) { - workingOn.remove(((ClassViewer) c).name); + ClassViewer cv = (ClassViewer) c; + workingOn.remove(cv.name + "$" + cv.name); } if (c instanceof FileViewer) { - workingOn.remove(((FileViewer) c).name); + FileViewer fv = (FileViewer) c; + workingOn.remove(fv.name + "$" + fv.name); } } @@ -106,43 +108,45 @@ public class WorkPane extends VisibleComponent implements ActionListener { int tabCount = 0; - public void addWorkingFile(final String name, final ClassNode cn) { - if (!workingOn.containsKey(name)) { - final JPanel tabComp = new ClassViewer(name, cn); + public void addWorkingFile(final String name, String container, final ClassNode cn) { + String key = container + "$" + name; + if (!workingOn.containsKey(key)) { + final JPanel tabComp = new ClassViewer(name, container, cn); tabs.add(tabComp); final int tabCount = tabs.indexOfComponent(tabComp); - workingOn.put(name, tabCount); - tabs.setTabComponentAt(tabCount, new TabbedPane(name,tabs)); + workingOn.put(key, tabCount); + tabs.setTabComponentAt(tabCount, new TabbedPane(name, tabs)); tabs.setSelectedIndex(tabCount); } else { - tabs.setSelectedIndex(workingOn.get(name)); + tabs.setSelectedIndex(workingOn.get(key)); } } - public void addFile(final String name, byte[] contents) { + public void addFile(final String name, String container, byte[] contents) { if(contents == null) //a directory return; - - if (!workingOn.containsKey(name)) { - final Component tabComp = new FileViewer(name, contents); + + String key = container + "$" + name; + if (!workingOn.containsKey(key)) { + final Component tabComp = new FileViewer(name, container, contents); tabs.add(tabComp); final int tabCount = tabs.indexOfComponent(tabComp); - workingOn.put(name, tabCount); - tabs.setTabComponentAt(tabCount, new TabbedPane(name,tabs)); + workingOn.put(key, tabCount); + tabs.setTabComponentAt(tabCount, new TabbedPane(name, tabs)); tabs.setSelectedIndex(tabCount); } else { - tabs.setSelectedIndex(workingOn.get(name)); + tabs.setSelectedIndex(workingOn.get(key)); } } @Override - public void openClassFile(final String name, final ClassNode cn) { - addWorkingFile(name, cn); + public void openClassFile(final String name, String container, final ClassNode cn) { + addWorkingFile(name, container, cn); } @Override - public void openFile(final String name, byte[] content) { - addFile(name, content); + public void openFile(final String name, String container, byte[] content) { + addFile(name, container, content); } public Viewer getCurrentViewer() {