Skip to content

Commit

Permalink
feat(gui): reopened tabs on file reload (PR #793 #792)
Browse files Browse the repository at this point in the history
* Add getRealFullName() to ClassNode and JavaClass and searchJavaClassByRealName() to JadxWrapper

Those methods is like getFullName() and searchJavaClassByClassName(), but for class names without aliases.
It is necessary for renaming classes/methods/fields.

* MainWindow: Try to restore open tabs on deobfuscation toggle

Restore open tabs if possible when user toggles deobfuscation mode.
Try to scroll to the position before toggling deobfuscation mode (may be not exact cause of the comments).
  • Loading branch information
S-trace authored and skylot committed Dec 10, 2019
1 parent cc29da8 commit 78eed86
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions jadx-core/src/main/java/jadx/api/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public String getFullName() {
return cls.getFullName();
}

public String getRealFullName() {
return cls.getRealFullName();
}

public String getPackage() {
return cls.getPackage();
}
Expand Down
4 changes: 4 additions & 0 deletions jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ public String getFullName() {
return clsInfo.getAliasFullName();
}

public String getRealFullName() {
return clsInfo.getType().getObject();
}

public String getPackage() {
return clsInfo.getAliasPkg();
}
Expand Down
9 changes: 9 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/JadxWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ public JadxArgs getArgs() {
return decompiler.getClasses().stream().filter(cls -> cls.getFullName().equals(fullName))
.findFirst().orElse(null);
}

/**
* @param realName Real name of an outer class. Inner classes are not supported.
* @return
*/
public @Nullable JavaClass searchJavaClassByRealName(String realName) {
return decompiler.getClasses().stream().filter(cls -> cls.getRealFullName().equals(realName))
.findFirst().orElse(null);
}
}
41 changes: 41 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -38,11 +40,13 @@
import javax.swing.tree.TreeSelectionModel;

import org.fife.ui.rsyntaxtextarea.Theme;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.api.JadxArgs;
import jadx.api.JavaClass;
import jadx.api.JavaNode;
import jadx.api.ResourceFile;
import jadx.gui.JadxWrapper;
Expand Down Expand Up @@ -373,9 +377,46 @@ public synchronized void cancelBackgroundJobs() {

public void reOpenFile() {
File openedFile = wrapper.getOpenFile();
Map<String, Integer> openTabs = storeOpenTabs();
if (openedFile != null) {
open(openedFile.toPath());
}
restoreOpenTabs(openTabs);
}

@NotNull
private Map<String, Integer> storeOpenTabs() {
Map<String, Integer> openTabs = new LinkedHashMap<>();
for (Map.Entry<JNode, ContentPanel> entry : tabbedPane.getOpenTabs().entrySet()) {
JavaNode javaNode = entry.getKey().getJavaNode();
String classRealName = "";
if (javaNode instanceof JavaClass) {
JavaClass javaClass = (JavaClass) javaNode;
classRealName = javaClass.getRealFullName();
}
@Nullable
JumpPosition position = entry.getValue().getTabbedPane().getCurrentPosition();
int line = 0;
if (position != null) {
line = position.getLine();
}
openTabs.put(classRealName, line);
}
return openTabs;
}

private void restoreOpenTabs(Map<String, Integer> openTabs) {
for (Map.Entry<String, Integer> entry : openTabs.entrySet()) {
String classRealName = entry.getKey();
int position = entry.getValue();
@Nullable
JavaClass newClass = wrapper.searchJavaClassByRealName(classRealName);
if (newClass == null) {
continue;
}
JNode newNode = cacheObject.getNodeCache().makeFrom(newClass);
tabbedPane.codeJump(new JumpPosition(newNode, position));
}
}

private void saveAll(boolean export) {
Expand Down
2 changes: 1 addition & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void codeJump(JumpPosition pos) {
}

@Nullable
private JumpPosition getCurrentPosition() {
JumpPosition getCurrentPosition() {
ContentPanel selectedCodePanel = getSelectedCodePanel();
if (selectedCodePanel instanceof AbstractCodeContentPanel) {
return ((AbstractCodeContentPanel) selectedCodePanel).getCodeArea().getCurrentPosition();
Expand Down

0 comments on commit 78eed86

Please sign in to comment.