Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit ebcd891

Browse files
committed
search
1 parent 7b6acdf commit ebcd891

File tree

7 files changed

+333
-0
lines changed

7 files changed

+333
-0
lines changed

src/main/java/cn/enaium/joe/JavaOctetEditor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import cn.enaium.joe.gui.panel.file.tree.FileTreePanel;
2323
import cn.enaium.joe.gui.panel.menu.FileMenu;
2424
import cn.enaium.joe.gui.panel.menu.HelpMenu;
25+
import cn.enaium.joe.gui.panel.menu.SearchMenu;
2526
import cn.enaium.joe.jar.Jar;
2627
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
2728
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
@@ -58,6 +59,7 @@ public void run() {
5859
window.setJMenuBar(new JMenuBar() {{
5960
add(new FileMenu());
6061
add(new HelpMenu());
62+
add(new SearchMenu());
6163
}});
6264

6365
window.setContentPane(new JPanel(new BorderLayout()) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.dialog;
18+
19+
import cn.enaium.joe.gui.panel.search.Result;
20+
21+
import java.awt.*;
22+
23+
/**
24+
* @author Enaium
25+
*/
26+
public class SearchDialog extends Dialog {
27+
public Result result = new Result();
28+
29+
public SearchDialog() {
30+
super("Search");
31+
setLayout(new BorderLayout());
32+
setSize(400, 300);
33+
add(result, BorderLayout.CENTER);
34+
}
35+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.dialog.search;
18+
19+
import cn.enaium.joe.JavaOctetEditor;
20+
import cn.enaium.joe.dialog.SearchDialog;
21+
import cn.enaium.joe.gui.panel.search.ResultNode;
22+
import cn.enaium.joe.jar.Jar;
23+
import cn.enaium.joe.util.ASyncUtil;
24+
import org.objectweb.asm.tree.AbstractInsnNode;
25+
import org.objectweb.asm.tree.ClassNode;
26+
import org.objectweb.asm.tree.LdcInsnNode;
27+
import org.objectweb.asm.tree.MethodNode;
28+
29+
import javax.swing.*;
30+
import java.awt.*;
31+
import java.util.Map;
32+
33+
/**
34+
* @author Enaium
35+
*/
36+
public class SearchLdcDialog extends SearchDialog {
37+
public SearchLdcDialog() {
38+
setTitle("Search LDC");
39+
add(new JPanel(new FlowLayout()) {{
40+
JTextField text = new JTextField(15);
41+
add(text);
42+
add(new JButton("Search") {{
43+
addActionListener(e -> {
44+
if (!text.getText().replace(" ", "").isEmpty()) {
45+
Jar jar = JavaOctetEditor.getInstance().jar;
46+
ASyncUtil.execute(() -> {
47+
float loaded = 0;
48+
float total = 0;
49+
for (Map.Entry<String, ClassNode> stringClassNodeEntry : jar.classes.entrySet()) {
50+
for (MethodNode method : stringClassNodeEntry.getValue().methods) {
51+
total += method.instructions.size();
52+
}
53+
}
54+
55+
for (Map.Entry<String, ClassNode> stringClassNodeEntry : jar.classes.entrySet()) {
56+
for (MethodNode method : stringClassNodeEntry.getValue().methods) {
57+
58+
for (AbstractInsnNode instruction : method.instructions) {
59+
if (instruction instanceof LdcInsnNode) {
60+
String ldc = ((LdcInsnNode) instruction).cst.toString();
61+
if (ldc.contains(text.getText())) {
62+
((DefaultListModel<ResultNode>) result.getList().getModel()).addElement(new ResultNode(stringClassNodeEntry.getValue(), ldc));
63+
}
64+
}
65+
JavaOctetEditor.getInstance().bottomPanel.setProcess((int) ((loaded++ / total) * 100f));
66+
}
67+
}
68+
}
69+
JavaOctetEditor.getInstance().bottomPanel.setProcess(0);
70+
});
71+
}
72+
});
73+
}});
74+
}}, BorderLayout.SOUTH);
75+
}
76+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.gui.panel.menu;
18+
19+
import cn.enaium.joe.gui.panel.menu.search.LdcMenuItem;
20+
21+
import javax.swing.*;
22+
23+
/**
24+
* @author Enaium
25+
*/
26+
public class SearchMenu extends JMenu {
27+
public SearchMenu() {
28+
super("Search");
29+
add(new LdcMenuItem());
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.gui.panel.menu.search;
18+
19+
import cn.enaium.joe.JavaOctetEditor;
20+
import cn.enaium.joe.dialog.search.SearchLdcDialog;
21+
import cn.enaium.joe.jar.Jar;
22+
23+
import javax.swing.*;
24+
25+
/**
26+
* @author Enaium
27+
*/
28+
public class LdcMenuItem extends JMenuItem {
29+
public LdcMenuItem() {
30+
super("LDC");
31+
addActionListener(e -> {
32+
Jar jar = JavaOctetEditor.getInstance().jar;
33+
if (jar == null) {
34+
return;
35+
}
36+
37+
new SearchLdcDialog().setVisible(true);
38+
});
39+
}
40+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.gui.panel.search;
18+
19+
import cn.enaium.joe.JavaOctetEditor;
20+
import cn.enaium.joe.gui.panel.file.tree.FileTreePanel;
21+
import cn.enaium.joe.gui.panel.file.tree.node.*;
22+
import cn.enaium.joe.util.ASyncUtil;
23+
import org.objectweb.asm.tree.ClassNode;
24+
import org.objectweb.asm.tree.MethodNode;
25+
26+
import javax.swing.*;
27+
import javax.swing.table.DefaultTableModel;
28+
import javax.swing.tree.DefaultTreeModel;
29+
import javax.swing.tree.TreePath;
30+
import java.awt.*;
31+
import java.awt.event.MouseAdapter;
32+
import java.awt.event.MouseEvent;
33+
import java.awt.event.MouseListener;
34+
import java.nio.charset.StandardCharsets;
35+
36+
/**
37+
* @author Enaium
38+
*/
39+
public class Result extends JPanel {
40+
41+
private final JList<ResultNode> list;
42+
43+
public Result() {
44+
super(new BorderLayout());
45+
list = new JList<>(new DefaultListModel<>());
46+
add(new JScrollPane(list), BorderLayout.CENTER);
47+
48+
JPopupMenu jPopupMenu = new JPopupMenu();
49+
JMenuItem jMenuItem = new JMenuItem("Jump to Node");
50+
jMenuItem.addActionListener(e -> {
51+
if (list.getSelectedValue() != null) {
52+
ASyncUtil.execute(() -> {
53+
SwingUtilities.invokeLater(() -> {
54+
FileTreePanel fileTreePanel = JavaOctetEditor.getInstance().fileTreePanel;
55+
DefaultTreeModel model = (DefaultTreeModel) fileTreePanel.getModel();
56+
if (selectEntry(fileTreePanel, list.getSelectedValue().getClassNode(), model, (DefaultTreeNode) model.getRoot())) {
57+
fileTreePanel.repaint();
58+
}
59+
});
60+
});
61+
}
62+
});
63+
jPopupMenu.add(jMenuItem);
64+
65+
list.addMouseListener(new MouseAdapter() {
66+
@Override
67+
public void mousePressed(MouseEvent e) {
68+
if (SwingUtilities.isRightMouseButton(e)) {
69+
if (list.getSelectedValue() != null) {
70+
jPopupMenu.show(list, e.getX(), e.getY());
71+
}
72+
}
73+
}
74+
});
75+
}
76+
77+
public boolean selectEntry(JTree jTree, ClassNode classNode, DefaultTreeModel defaultTreeModel, DefaultTreeNode defaultTreeNode) {
78+
for (int i = 0; i < defaultTreeModel.getChildCount(defaultTreeNode); i++) {
79+
DefaultTreeNode child = ((DefaultTreeNode) defaultTreeModel.getChild(defaultTreeNode, i));
80+
if (child instanceof PackageTreeNode) {
81+
PackageTreeNode packageTreeNode = (PackageTreeNode) child;
82+
if (packageTreeNode instanceof ClassTreeNode) {
83+
ClassTreeNode classTreeNode = (ClassTreeNode) packageTreeNode;
84+
if (classNode.equals(classTreeNode.classNode)) {
85+
TreePath treePath = new TreePath(defaultTreeModel.getPathToRoot(classTreeNode));
86+
jTree.setSelectionPath(treePath);
87+
jTree.scrollPathToVisible(treePath);
88+
return true;
89+
}
90+
}
91+
}
92+
if (!defaultTreeNode.isLeaf()) {
93+
if (selectEntry(jTree, classNode, defaultTreeModel, child)) {
94+
return true;
95+
}
96+
}
97+
}
98+
return false;
99+
}
100+
101+
public JList<ResultNode> getList() {
102+
return list;
103+
}
104+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2022 Enaium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cn.enaium.joe.gui.panel.search;
18+
19+
import org.objectweb.asm.tree.ClassNode;
20+
21+
/**
22+
* @author Enaium
23+
*/
24+
public class ResultNode {
25+
private final ClassNode classNode;
26+
private final String result;
27+
28+
public ResultNode(ClassNode classNode, String result) {
29+
this.classNode = classNode;
30+
this.result = result;
31+
}
32+
33+
public ClassNode getClassNode() {
34+
return classNode;
35+
}
36+
37+
public String getResult() {
38+
return result;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return result;
44+
}
45+
}

0 commit comments

Comments
 (0)