From c4cdd8514dc2fe013804a231c3e5ee8278840f3d Mon Sep 17 00:00:00 2001 From: Skylot Date: Tue, 23 Jul 2013 23:01:48 +0400 Subject: [PATCH] gui: add fields and methods to tree --- NOTICE | 38 +++++++++++- .../src/main/java/jadx/gui/MainWindow.java | 40 ++++++------ .../main/java/jadx/gui/treemodel/JClass.java | 38 ++++++++++-- .../main/java/jadx/gui/treemodel/JField.java | 57 ++++++++++++++++++ .../main/java/jadx/gui/treemodel/JMethod.java | 57 ++++++++++++++++++ .../main/java/jadx/gui/treemodel/JNode.java | 4 ++ .../java/jadx/gui/treemodel/JPackage.java | 11 ++++ .../main/java/jadx/gui/treemodel/JRoot.java | 10 +++ .../resources/icons-16/field_default_obj.png | Bin 0 -> 218 bytes 9 files changed, 230 insertions(+), 25 deletions(-) create mode 100644 jadx-gui/src/main/java/jadx/gui/treemodel/JField.java create mode 100644 jadx-gui/src/main/java/jadx/gui/treemodel/JMethod.java create mode 100644 jadx-gui/src/main/resources/icons-16/field_default_obj.png diff --git a/NOTICE b/NOTICE index 6fc624ea..ec8d67f8 100644 --- a/NOTICE +++ b/NOTICE @@ -2,7 +2,7 @@ The majority of jadx is written and copyrighted by me (Skylot) and released under the Apache 2.0 license: ******************************************************************************* -Copyright 2013 Skylot +Copyright 2013, Skylot Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -105,7 +105,41 @@ as published by the Free Software Foundation. ******************************************************************************* -GUI icons copied from several places: + +Jadx-gui components +=================== + +RSyntaxTextArea library licensed under modified BSD liense: + +******************************************************************************* +Copyright (c) 2012, Robert Futrell +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************* + + +Icons copied from several places: - Eclipse Project (JDT UI) - licensed under EPL v1.0 (http://www.eclipse.org/legal/epl-v10.html) - famfamfam silk icon set (http://www.famfamfam.com/lab/icons/silk/) - licensed under Creative Commons Attribution 2.5 License (http://creativecommons.org/licenses/by/2.5/) diff --git a/jadx-gui/src/main/java/jadx/gui/MainWindow.java b/jadx-gui/src/main/java/jadx/gui/MainWindow.java index 447bfdec..481315c4 100644 --- a/jadx-gui/src/main/java/jadx/gui/MainWindow.java +++ b/jadx-gui/src/main/java/jadx/gui/MainWindow.java @@ -1,8 +1,6 @@ package jadx.gui; -import jadx.api.JavaClass; import jadx.cli.JadxArgs; -import jadx.gui.treemodel.JClass; import jadx.gui.treemodel.JNode; import jadx.gui.treemodel.JRoot; @@ -23,10 +21,10 @@ import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.text.BadLocationException; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import java.awt.BorderLayout; import java.awt.Color; @@ -76,7 +74,6 @@ public class MainWindow extends JFrame { treeModel.setRoot(treeRoot); treeModel.reload(); tree.expandRow(0); -// expandTree(); } private void toggleFlattenPackage() { @@ -89,15 +86,26 @@ public class MainWindow extends JFrame { } } - private void expandTree() { - DefaultMutableTreeNode currentNode = ((DefaultMutableTreeNode) tree.getModel().getRoot()).getNextNode(); - do { - if (currentNode.getLevel() == 1) { - tree.expandPath(new TreePath(currentNode.getPath())); + private void treeClickAction() { + Object obj = tree.getLastSelectedPathComponent(); + if (obj instanceof JNode) { + JNode node = (JNode) obj; + if (node.getJParent() != null) { + textArea.setText(node.getJParent().getCode()); + scrollToLine(node.getLine()); } - currentNode = currentNode.getNextNode(); } - while (currentNode != null); + } + + private void scrollToLine(int line) { + if (line < 2) { + return; + } + try { + textArea.setCaretPosition(textArea.getLineStartOffset(line - 1)); + } catch (BadLocationException e) { + LOG.error("Can't scroll to " + line, e); + } } private void initMenuAndToolbar() { @@ -165,14 +173,8 @@ public class MainWindow extends JFrame { tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(new TreeSelectionListener() { @Override - public void valueChanged(TreeSelectionEvent e) { - Object obj = tree.getLastSelectedPathComponent(); - if (obj instanceof JClass) { - JavaClass jc = ((JClass) obj).getCls(); - String code = jc.getCode(); - textArea.setText(code); - textArea.setCaretPosition(0); - } + public void valueChanged(TreeSelectionEvent event) { + treeClickAction(); } }); diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JClass.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JClass.java index fabf948e..822cee4d 100644 --- a/jadx-gui/src/main/java/jadx/gui/treemodel/JClass.java +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JClass.java @@ -1,6 +1,8 @@ package jadx.gui.treemodel; import jadx.api.JavaClass; +import jadx.api.JavaField; +import jadx.api.JavaMethod; import jadx.core.dex.info.AccessInfo; import jadx.gui.Utils; @@ -19,10 +21,10 @@ public class JClass extends DefaultMutableTreeNode implements JNode { private static final ImageIcon ICON_ANNOTATION = Utils.openIcon("annotation_obj"); private final JavaClass cls; + private JClass jParrent; public JClass(JavaClass cls) { this.cls = cls; - updateChilds(); } public JavaClass getCls() { @@ -31,9 +33,23 @@ public class JClass extends DefaultMutableTreeNode implements JNode { @Override public void updateChilds() { -// for (JavaClass javaClass : cls.getInnerClasses()) { -// add(new JClass(javaClass)); -// } + JClass currentParent = jParrent == null ? this : jParrent; + for (JavaClass javaClass : cls.getInnerClasses()) { + JClass child = new JClass(javaClass); + child.setJParent(currentParent); + child.updateChilds(); + add(child); + } + for (JavaField f : cls.getFields()) { + add(new JField(f, currentParent)); + } + for (JavaMethod m : cls.getMethods()) { + add(new JMethod(m, currentParent)); + } + } + + public String getCode(){ + return cls.getCode(); } @Override @@ -57,6 +73,20 @@ public class JClass extends DefaultMutableTreeNode implements JNode { } } + public void setJParent(JClass parent) { + this.jParrent = parent; + } + + @Override + public JClass getJParent() { + return jParrent; + } + + @Override + public int getLine() { + return cls.getDecompiledLine(); + } + @Override public String toString() { return cls.getShortName(); diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JField.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JField.java new file mode 100644 index 00000000..aa3eb22c --- /dev/null +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JField.java @@ -0,0 +1,57 @@ +package jadx.gui.treemodel; + +import jadx.api.JavaField; +import jadx.core.dex.info.AccessInfo; +import jadx.gui.Utils; + +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.tree.DefaultMutableTreeNode; + +public class JField extends DefaultMutableTreeNode implements JNode { + private static final ImageIcon ICON_FLD_DEF = Utils.openIcon("field_default_obj"); + private static final ImageIcon ICON_FLD_PRI = Utils.openIcon("field_private_obj"); + private static final ImageIcon ICON_FLD_PRO = Utils.openIcon("field_protected_obj"); + private static final ImageIcon ICON_FLD_PUB = Utils.openIcon("field_public_obj"); + + private final JavaField field; + private final JClass jParent; + + public JField(JavaField javaField, JClass jClass) { + this.field = javaField; + this.jParent = jClass; + } + + @Override + public void updateChilds() { + } + + @Override + public JClass getJParent() { + return jParent; + } + + @Override + public int getLine() { + return field.getDecompiledLine(); + } + + @Override + public Icon getIcon() { + AccessInfo af = field.getAccessFlags(); + if(af.isPublic()){ + return ICON_FLD_PUB; + } else if(af.isPrivate()) { + return ICON_FLD_PRI; + } else if(af.isProtected()) { + return ICON_FLD_PRO; + } else { + return ICON_FLD_DEF; + } + } + + @Override + public String toString() { + return field.getName(); + } +} diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JMethod.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JMethod.java new file mode 100644 index 00000000..7c4c83e3 --- /dev/null +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JMethod.java @@ -0,0 +1,57 @@ +package jadx.gui.treemodel; + +import jadx.api.JavaMethod; +import jadx.core.dex.info.AccessInfo; +import jadx.gui.Utils; + +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.tree.DefaultMutableTreeNode; + +public class JMethod extends DefaultMutableTreeNode implements JNode { + private static final ImageIcon ICON_MTH_DEF = Utils.openIcon("methdef_obj"); + private static final ImageIcon ICON_MTH_PRI = Utils.openIcon("methpri_obj"); + private static final ImageIcon ICON_MTH_PRO = Utils.openIcon("methpro_obj"); + private static final ImageIcon ICON_MTH_PUB = Utils.openIcon("methpub_obj"); + + private final JavaMethod mth; + private final JClass jparent; + + public JMethod(JavaMethod javaMethod, JClass jClass) { + this.mth = javaMethod; + this.jparent = jClass; + } + + @Override + public void updateChilds() { + } + + @Override + public JClass getJParent() { + return jparent; + } + + @Override + public int getLine() { + return mth.getDecompiledLine(); + } + + @Override + public Icon getIcon() { + AccessInfo af = mth.getAccessFlags(); + if (af.isPublic()) { + return ICON_MTH_PUB; + } else if (af.isPrivate()) { + return ICON_MTH_PRI; + } else if (af.isProtected()) { + return ICON_MTH_PRO; + } else { + return ICON_MTH_DEF; + } + } + + @Override + public String toString() { + return mth.getName(); + } +} diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JNode.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JNode.java index 45a12aa8..5601cdb2 100644 --- a/jadx-gui/src/main/java/jadx/gui/treemodel/JNode.java +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JNode.java @@ -4,6 +4,10 @@ import javax.swing.Icon; public interface JNode { + JClass getJParent(); + + int getLine(); + void updateChilds(); Icon getIcon(); diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JPackage.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JPackage.java index 158f8285..7480c557 100644 --- a/jadx-gui/src/main/java/jadx/gui/treemodel/JPackage.java +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JPackage.java @@ -40,6 +40,7 @@ public class JPackage extends DefaultMutableTreeNode implements JNode, Comparabl add(pkg); } for (JClass cls : classes) { + cls.updateChilds(); add(cls); } } @@ -65,6 +66,16 @@ public class JPackage extends DefaultMutableTreeNode implements JNode, Comparabl return PACKAGE_ICON; } + @Override + public JClass getJParent() { + return null; + } + + @Override + public int getLine() { + return 0; + } + @Override public int compareTo(JPackage o) { return name.compareTo(o.name); diff --git a/jadx-gui/src/main/java/jadx/gui/treemodel/JRoot.java b/jadx-gui/src/main/java/jadx/gui/treemodel/JRoot.java index f00cabb7..7e0912c7 100644 --- a/jadx-gui/src/main/java/jadx/gui/treemodel/JRoot.java +++ b/jadx-gui/src/main/java/jadx/gui/treemodel/JRoot.java @@ -118,6 +118,16 @@ public class JRoot extends DefaultMutableTreeNode implements JNode { return ROOT_ICON; } + @Override + public JClass getJParent() { + return null; + } + + @Override + public int getLine() { + return 0; + } + @Override public String toString() { File file = wrapper.getOpenFile(); diff --git a/jadx-gui/src/main/resources/icons-16/field_default_obj.png b/jadx-gui/src/main/resources/icons-16/field_default_obj.png new file mode 100644 index 0000000000000000000000000000000000000000..becfe6b118bd7f5ec63412bee50aa6ff2ef72b31 GIT binary patch literal 218 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFP2=EDUbu3>W+_p;~W{za?0=4Yr zIt42{nm2DadH+8c09By_8JTXN5LZc%Uob=skYw0&M&~?GlrhQM-GyIb7sE{;hqJ&V zvKUBvfU(=jY&#%B*VDx@gyVYh3{F;_BStn0doq&(+%mIOTM{oOa;Ew4XK=6^t7&R# ePE0W3U|^_^_sE}m|GykiBZH@_pUXO@geCxhNkORq literal 0 HcmV?d00001