Skip to content

Commit

Permalink
fix(gui): make link for full class names (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Oct 28, 2018
1 parent a8a3164 commit a3464d7
Show file tree
Hide file tree
Showing 24 changed files with 602 additions and 463 deletions.
28 changes: 28 additions & 0 deletions jadx-core/src/main/java/jadx/api/JadxDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,38 @@ Map<MethodNode, JavaMethod> getMethodsMap() {
return methodsMap;
}

JavaMethod getJavaMethodByNode(MethodNode mth) {
JavaMethod javaMethod = methodsMap.get(mth);
if (javaMethod != null) {
return javaMethod;
}
// parent class not loaded yet
JavaClass javaClass = classesMap.get(mth.getParentClass());
if (javaClass != null) {
javaClass.decompile();
return methodsMap.get(mth);
}
return null;
}

Map<FieldNode, JavaField> getFieldsMap() {
return fieldsMap;
}

JavaField getJavaFieldByNode(FieldNode fld) {
JavaField javaField = fieldsMap.get(fld);
if (javaField != null) {
return javaField;
}
// parent class not loaded yet
JavaClass javaClass = classesMap.get(fld.getParentClass());
if (javaClass != null) {
javaClass.decompile();
return fieldsMap.get(fld);
}
return null;
}

public JadxArgs getArgs() {
return args;
}
Expand Down
15 changes: 3 additions & 12 deletions jadx-core/src/main/java/jadx/api/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ private JavaNode convertNode(Object obj) {
return getRootDecompiler().getClassesMap().get(obj);
}
if (obj instanceof MethodNode) {
return getRootDecompiler().getMethodsMap().get(obj);
return getRootDecompiler().getJavaMethodByNode(((MethodNode) obj));
}
if (obj instanceof FieldNode) {
return getRootDecompiler().getFieldsMap().get(obj);
return getRootDecompiler().getJavaFieldByNode((FieldNode) obj);
}
return null;
}
Expand All @@ -181,15 +181,6 @@ public JavaNode getJavaNodeAtPosition(int line, int offset) {
return convertNode(obj);
}

@Nullable
public CodePosition getDefinitionPosition(int line, int offset) {
JavaNode javaNode = getJavaNodeAtPosition(line, offset);
if (javaNode == null) {
return null;
}
return getDefinitionPosition(javaNode);
}

@Nullable
public CodePosition getDefinitionPosition(JavaNode javaNode) {
JavaClass jCls = javaNode.getTopParentClass();
Expand Down Expand Up @@ -265,6 +256,6 @@ public int hashCode() {

@Override
public String toString() {
return cls.getFullName() + "[ " + getFullName() + " ]";
return getFullName();
}
}
30 changes: 6 additions & 24 deletions jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jadx.core.codegen;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -84,35 +83,18 @@ public CodeWriter makeClass() throws CodegenException {
}
int importsCount = imports.size();
if (importsCount != 0) {
List<String> sortImports = new ArrayList<>(importsCount);
for (ClassInfo ic : imports) {
sortImports.add(ic.getAlias().getFullName());
}
Collections.sort(sortImports);

for (String imp : sortImports) {
ClassInfo importClassInfo = ClassInfo.fromName(cls.dex().root(), imp);
ClassNode classNode = cls.dex().resolveClass(importClassInfo);
// Clickable element seems to be limited by the next dot, therefore
// we can't just use the complete class name including packagename
int clsDotIdx = imp.lastIndexOf('.');
String pkg = "";
if (clsDotIdx >= 0) {
pkg = imp.substring(0, clsDotIdx + 1);
imp = imp.substring(clsDotIdx + 1);
}
List<ClassInfo> sortedImports = new ArrayList<>(imports);
sortedImports.sort(Comparator.comparing(classInfo -> classInfo.getAlias().getFullName()));
sortedImports.forEach(classInfo -> {
clsCode.startLine("import ");
clsCode.add(pkg);
ClassNode classNode = cls.root().resolveClass(classInfo);
if (classNode != null) {
// attach the clickable link info to the class name
clsCode.attachAnnotation(classNode);
}
clsCode.add(imp);
clsCode.add(classInfo.getAlias().getFullName());
clsCode.add(';');
}
});
clsCode.newLine();

sortImports.clear();
imports.clear();
}
clsCode.add(clsBody);
Expand Down
4 changes: 2 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/settings/JadxSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import jadx.api.JadxArgs;
import jadx.cli.JadxCLIArgs;
import jadx.gui.ui.CodeArea;
import jadx.gui.ui.codearea.EditorTheme;
import jadx.gui.utils.LangLocale;
import jadx.gui.utils.NLS;

Expand Down Expand Up @@ -263,7 +263,7 @@ private void upgradeSettings(int fromVersion) {
fromVersion++;
}
if (fromVersion == 1) {
setEditorThemePath(CodeArea.getAllThemes()[0].getPath());
setEditorThemePath(EditorTheme.ALL_THEMES[0].getPath());
fromVersion++;
}
if (fromVersion == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import org.slf4j.LoggerFactory;
import say.swing.JFontChooser;

import jadx.gui.ui.CodeArea;
import jadx.gui.ui.CodeArea.EditorTheme;
import jadx.gui.ui.codearea.EditorTheme;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.LangLocale;
import jadx.gui.utils.NLS;
Expand Down Expand Up @@ -191,7 +190,7 @@ public void mouseClicked(MouseEvent e) {
}
});

EditorTheme[] editorThemes = CodeArea.getAllThemes();
EditorTheme[] editorThemes = EditorTheme.ALL_THEMES;
JComboBox<EditorTheme> themesCbx = new JComboBox<>(editorThemes);
for (EditorTheme theme : editorThemes) {
if (theme.getPath().equals(settings.getEditorThemePath())) {
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/CertificatePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import jadx.gui.treemodel.JNode;
import jadx.gui.ui.codearea.CodeArea;

public final class CertificatePanel extends ContentPanel {
private static final long serialVersionUID = 8566591625905036877L;
Expand Down
Loading

0 comments on commit a3464d7

Please sign in to comment.