From 61be1d8b0a9b069cf3a3a6b54f66494662acc951 Mon Sep 17 00:00:00 2001 From: Skylot Date: Thu, 18 Jan 2024 20:36:51 +0000 Subject: [PATCH] fix(gui): allow to reset variable name, fix renames for inner classes --- .../src/main/java/jadx/api/JavaVariable.java | 1 + .../events/types/NodeRenamedByUser.java | 22 +++++++++++++++++-- .../visitors/rename/CodeRenameVisitor.java | 2 +- .../gui/events/services/RenameService.java | 8 +++---- .../java/jadx/gui/ui/dialog/RenameDialog.java | 13 +++++++---- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/jadx-core/src/main/java/jadx/api/JavaVariable.java b/jadx-core/src/main/java/jadx/api/JavaVariable.java index 679ffeb486c..49a040533b5 100644 --- a/jadx-core/src/main/java/jadx/api/JavaVariable.java +++ b/jadx-core/src/main/java/jadx/api/JavaVariable.java @@ -79,6 +79,7 @@ public List getUseIn() { @Override public void removeAlias() { + varNode.setName(null); } @Override diff --git a/jadx-core/src/main/java/jadx/api/plugins/events/types/NodeRenamedByUser.java b/jadx-core/src/main/java/jadx/api/plugins/events/types/NodeRenamedByUser.java index 27b4f83c240..0b4822d96d3 100644 --- a/jadx-core/src/main/java/jadx/api/plugins/events/types/NodeRenamedByUser.java +++ b/jadx-core/src/main/java/jadx/api/plugins/events/types/NodeRenamedByUser.java @@ -1,5 +1,7 @@ package jadx.api.plugins.events.types; +import org.jetbrains.annotations.Nullable; + import jadx.api.metadata.ICodeNodeRef; import jadx.api.plugins.events.IJadxEvent; import jadx.api.plugins.events.JadxEventType; @@ -14,7 +16,12 @@ public class NodeRenamedByUser implements IJadxEvent { /** * Optional JRenameNode instance */ - private Object renameNode; + private @Nullable Object renameNode; + + /** + * Request reset name to original + */ + private boolean resetName = false; public NodeRenamedByUser(ICodeNodeRef node, String oldName, String newName) { this.node = node; @@ -42,6 +49,14 @@ public void setRenameNode(Object renameNode) { this.renameNode = renameNode; } + public boolean isResetName() { + return resetName; + } + + public void setResetName(boolean resetName) { + this.resetName = resetName; + } + @Override public JadxEventType getType() { return JadxEvents.NODE_RENAMED_BY_USER; @@ -49,6 +64,9 @@ public JadxEventType getType() { @Override public String toString() { - return "NodeRenamedByUser{" + node + ", '" + oldName + "' -> '" + newName + "'}"; + return "NodeRenamedByUser{" + node + + ", '" + oldName + "' -> '" + newName + '\'' + + (resetName ? ", reset name" : "") + + '}'; } } diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/rename/CodeRenameVisitor.java b/jadx-core/src/main/java/jadx/core/dex/visitors/rename/CodeRenameVisitor.java index 1fe6b1d3e22..7181dd1ceb5 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/rename/CodeRenameVisitor.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/rename/CodeRenameVisitor.java @@ -106,7 +106,7 @@ private List getRenames(ClassNode cls) { if (clsRenamesMap == null) { return Collections.emptyList(); } - List clsComments = clsRenamesMap.get(cls.getClassInfo().getFullName()); + List clsComments = clsRenamesMap.get(cls.getClassInfo().getRawName()); if (clsComments == null) { return Collections.emptyList(); } diff --git a/jadx-gui/src/main/java/jadx/gui/events/services/RenameService.java b/jadx-gui/src/main/java/jadx/gui/events/services/RenameService.java index f97283c328b..32b761324a5 100644 --- a/jadx-gui/src/main/java/jadx/gui/events/services/RenameService.java +++ b/jadx-gui/src/main/java/jadx/gui/events/services/RenameService.java @@ -61,7 +61,7 @@ private void process(NodeRenamedByUser event) { try { LOG.debug("Applying rename event: {}", event); JRenameNode node = getRenameNode(event); - updateCodeRenames(set -> processRename(node, event.getNewName(), set)); + updateCodeRenames(set -> processRename(node, event, set)); refreshState(node); } catch (Exception e) { LOG.error("Rename failed", e); @@ -85,10 +85,10 @@ private void process(NodeRenamedByUser event) { throw new JadxRuntimeException("Failed to resolve node: " + event.getNode()); } - private void processRename(JRenameNode node, String newName, Set renames) { - ICodeRename rename = node.buildCodeRename(newName, renames); + private void processRename(JRenameNode node, NodeRenamedByUser event, Set renames) { + ICodeRename rename = node.buildCodeRename(event.getNewName(), renames); renames.remove(rename); - if (newName.isEmpty()) { + if (event.isResetName() || event.getNewName().isEmpty()) { node.removeAlias(); } else { renames.add(rename); diff --git a/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java b/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java index de36f0c2045..78c5c46d465 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java @@ -22,6 +22,7 @@ import jadx.api.metadata.ICodeNodeRef; import jadx.api.plugins.events.types.NodeRenamedByUser; +import jadx.core.utils.Utils; import jadx.gui.treemodel.JClass; import jadx.gui.treemodel.JNode; import jadx.gui.treemodel.JPackage; @@ -98,19 +99,23 @@ private void rename(String newName) { return; } String oldName = node.getName(); - if (newName.isEmpty()) { + String newNodeName; + boolean reset = newName.isEmpty(); + if (reset) { node.removeAlias(); - sendRenameEvent(oldName, node.getJavaNode().getName()); + newNodeName = Utils.getOrElse(node.getJavaNode().getName(), ""); } else { - sendRenameEvent(oldName, newName); + newNodeName = newName; } + sendRenameEvent(oldName, newNodeName, reset); dispose(); } - private void sendRenameEvent(String oldName, String newName) { + private void sendRenameEvent(String oldName, String newName, boolean reset) { ICodeNodeRef nodeRef = node.getJavaNode().getCodeNodeRef(); NodeRenamedByUser event = new NodeRenamedByUser(nodeRef, oldName, newName); event.setRenameNode(node); + event.setResetName(reset); mainWindow.events().send(event); }