Skip to content

Commit

Permalink
fix(gui): check user renames (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jun 29, 2022
1 parent e7a86a2 commit 2f2fbea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ private void process(RootNode root) {
deobfuscator.execute();
}

checkClasses(deobfuscator, root, args);
UserRenames.applyForNodes(root);
checkClasses(deobfuscator, root, args);

if (args.isDeobfuscationOn() || !args.isJsonOutput()) {
deobfuscator.savePresets();
Expand Down
28 changes: 24 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 @@ -38,6 +38,7 @@
import jadx.api.data.impl.JadxCodeRef;
import jadx.api.data.impl.JadxCodeRename;
import jadx.api.data.impl.JadxNodeRef;
import jadx.core.deobf.NameMapper;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.gui.jobs.TaskStatus;
Expand All @@ -58,6 +59,7 @@
import jadx.gui.utils.NLS;
import jadx.gui.utils.TextStandardActions;
import jadx.gui.utils.UiUtils;
import jadx.gui.utils.ui.DocumentUpdateListener;

public class RenameDialog extends JDialog {
private static final long serialVersionUID = -3269715644416902410L;
Expand All @@ -69,14 +71,16 @@ public class RenameDialog extends JDialog {
private final transient JNode source;
private final transient JNode node;
private transient JTextField renameField;
private transient JButton renameBtn;

public static boolean rename(MainWindow mainWindow, JNode node) {
return rename(mainWindow, node, node);
}

public static boolean rename(MainWindow mainWindow, JNode source, JNode node) {
RenameDialog renameDialog = new RenameDialog(mainWindow, source, node);
renameDialog.setVisible(true);
UiUtils.uiRun(() -> renameDialog.setVisible(true));
UiUtils.uiRun(renameDialog::initRenameField); // wait for UI events to propagate
return true;
}

Expand All @@ -89,6 +93,11 @@ private RenameDialog(MainWindow mainWindow, JNode source, JNode node) {
initUI();
}

private void initRenameField() {
renameField.setText(node.getName());
renameField.selectAll();
}

private JNode replaceNode(JNode node) {
if (node instanceof JMethod) {
JavaMethod javaMethod = ((JMethod) node).getJavaMethod();
Expand All @@ -103,7 +112,19 @@ private JNode replaceNode(JNode node) {
return node;
}

private boolean checkNewName() {
boolean valid = NameMapper.isValidIdentifier(renameField.getText());
if (renameBtn.isEnabled() != valid) {
renameBtn.setEnabled(valid);
renameField.putClientProperty("JComponent.outline", valid ? "" : "error");
}
return valid;
}

private void rename() {
if (!checkNewName()) {
return;
}
try {
updateCodeRenames(set -> processRename(node, renameField.getText(), set));
refreshState();
Expand Down Expand Up @@ -270,7 +291,7 @@ private void refreshTabs(TabbedPane tabbedPane, Set<JClass> updatedClasses) {
protected JPanel initButtonsPanel() {
JButton cancelButton = new JButton(NLS.str("search_dialog.cancel"));
cancelButton.addActionListener(event -> dispose());
JButton renameBtn = new JButton(NLS.str("common_dialog.ok"));
renameBtn = new JButton(NLS.str("common_dialog.ok"));
renameBtn.addActionListener(event -> rename());
getRootPane().setDefaultButton(renameBtn);

Expand All @@ -291,9 +312,8 @@ private void initUI() {
lbl.setLabelFor(nodeLabel);

renameField = new JTextField(40);
renameField.getDocument().addDocumentListener(new DocumentUpdateListener(ev -> checkNewName()));
renameField.addActionListener(e -> rename());
renameField.setText(node.getName());
renameField.selectAll();
new TextStandardActions(renameField);

JPanel renamePane = new JPanel();
Expand Down

0 comments on commit 2f2fbea

Please sign in to comment.