Skip to content

Commit

Permalink
fix(gui): allow to reset variable name, fix renames for inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jan 18, 2024
1 parent 4483533 commit 61be1d8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions jadx-core/src/main/java/jadx/api/JavaVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public List<JavaNode> getUseIn() {

@Override
public void removeAlias() {
varNode.setName(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -42,13 +49,24 @@ public void setRenameNode(Object renameNode) {
this.renameNode = renameNode;
}

public boolean isResetName() {
return resetName;
}

public void setResetName(boolean resetName) {
this.resetName = resetName;
}

@Override
public JadxEventType<NodeRenamedByUser> getType() {
return JadxEvents.NODE_RENAMED_BY_USER;
}

@Override
public String toString() {
return "NodeRenamedByUser{" + node + ", '" + oldName + "' -> '" + newName + "'}";
return "NodeRenamedByUser{" + node
+ ", '" + oldName + "' -> '" + newName + '\''
+ (resetName ? ", reset name" : "")
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private List<ICodeRename> getRenames(ClassNode cls) {
if (clsRenamesMap == null) {
return Collections.emptyList();
}
List<ICodeRename> clsComments = clsRenamesMap.get(cls.getClassInfo().getFullName());
List<ICodeRename> clsComments = clsRenamesMap.get(cls.getClassInfo().getRawName());
if (clsComments == null) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<ICodeRename> renames) {
ICodeRename rename = node.buildCodeRename(newName, renames);
private void processRename(JRenameNode node, NodeRenamedByUser event, Set<ICodeRename> 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);
Expand Down
13 changes: 9 additions & 4 deletions jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 61be1d8

Please sign in to comment.