/
FindAllBox.java
354 lines (307 loc) · 11.8 KB
/
FindAllBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package us.deathmarine.luyten;
import com.strobel.assembler.metadata.TypeDefinition;
import com.strobel.assembler.metadata.TypeReference;
import com.strobel.core.StringUtilities;
import com.strobel.decompiler.DecompilationOptions;
import com.strobel.decompiler.DecompilerSettings;
import com.strobel.decompiler.PlainTextOutput;
import us.deathmarine.luyten.ConfigSaver;
import us.deathmarine.luyten.MainWindow;
import us.deathmarine.luyten.Model;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
public class FindAllBox extends JDialog {
private static final long serialVersionUID = -4125409760166690462L;
private boolean searching;
private JButton findButton;
private JTextField textField;
private JCheckBox mcase;
private JCheckBox regex;
private JCheckBox wholew;
private JCheckBox classname;
private JList<String> list;
private JProgressBar progressBar;
boolean locked;
private JLabel statusLabel = new JLabel("");
private DefaultListModel<String> classesList = new DefaultListModel<String>();
private Thread tmp_thread;
public FindAllBox(final MainWindow mainWindow) {
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setHideOnEscapeButton();
progressBar = new JProgressBar(0, 100);
JLabel label = new JLabel("Find What:");
textField = new JTextField();
findButton = new JButton("Find");
findButton.addActionListener(new FindButton());
mcase = new JCheckBox("Match Case");
regex = new JCheckBox("Regex");
wholew = new JCheckBox("Whole Words");
classname = new JCheckBox("Classnames");
this.getRootPane().setDefaultButton(findButton);
list = new JList<String>(classesList);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(-1);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
@SuppressWarnings("unchecked")
JList<String> list = (JList<String>) evt.getSource();
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint());
String entryName = (String) list.getModel().getElementAt(index);
String[] array = entryName.split("/");
if (entryName.toLowerCase().endsWith(".class")) {
String internalName = StringUtilities.removeRight(entryName, ".class");
TypeReference type = Model.metadataSystem.lookupType(internalName);
try {
mainWindow.getModel().extractClassToTextPane(type, array[array.length - 1], entryName,
null);
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
} else {
try {
JarFile jfile = new JarFile(MainWindow.model.getOpenedFile());
mainWindow.getModel().extractSimpleFileEntryToTextPane(
jfile.getInputStream(jfile.getEntry(entryName)), array[array.length - 1],
entryName);
jfile.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
list.setLayoutOrientation(JList.VERTICAL);
JScrollPane listScroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension center = new Dimension((int) (screenSize.width * 0.35), 500);
final int x = (int) (center.width * 0.2);
final int y = (int) (center.height * 0.2);
this.setBounds(x, y, center.width, center.height);
this.setResizable(false);
GroupLayout layout = new GroupLayout(getRootPane());
getRootPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup().addComponent(label)
.addGroup(
layout.createParallelGroup(Alignment.LEADING).addComponent(statusLabel)
.addComponent(textField)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(mcase))
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(wholew))
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(regex))
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(classname)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(listScroller)
.addComponent(progressBar))))
.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(findButton))
);
layout.linkSize(SwingConstants.HORIZONTAL, findButton);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(textField)
.addComponent(findButton))
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(mcase).addComponent(wholew)
.addComponent(regex).addComponent(classname))
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(listScroller))))
.addGroup(layout.createParallelGroup(Alignment.LEADING)).addComponent(statusLabel)
.addComponent(progressBar));
this.adjustWindowPositionBySavedState();
this.setSaveWindowPositionOnClosing();
this.setName("Find All");
this.setTitle("Find All");
}
private class FindButton extends AbstractAction {
private static final long serialVersionUID = 75954129199541874L;
@Override
public void actionPerformed(ActionEvent event) {
tmp_thread = new Thread() {
public void run() {
if (findButton.getText().equals("Stop")) {
if (tmp_thread != null)
tmp_thread.interrupt();
setStatus("Stopped.");
findButton.setText("Find");
locked = false;
} else {
findButton.setText("Stop");
classesList.clear();
ConfigSaver configSaver = ConfigSaver.getLoadedInstance();
DecompilerSettings settings = configSaver.getDecompilerSettings();
File inFile = MainWindow.model.getOpenedFile();
boolean filter = ConfigSaver.getLoadedInstance().getLuytenPreferences()
.isFilterOutInnerClassEntries();
try {
JarFile jfile = new JarFile(inFile);
Enumeration<JarEntry> entLength = jfile.entries();
initProgressBar(Collections.list(entLength).size());
Enumeration<JarEntry> ent = jfile.entries();
while (ent.hasMoreElements() && findButton.getText().equals("Stop")) {
JarEntry entry = ent.nextElement();
String name = entry.getName();
setStatus(name);
if (filter && name.contains("$"))
continue;
if(locked || classname.isSelected()){
locked = true;
if(search(entry.getName()))
addClassName(entry.getName());
}else{
if (entry.getName().endsWith(".class")) {
synchronized (settings) {
String internalName = StringUtilities.removeRight(entry.getName(), ".class");
TypeReference type = Model.metadataSystem.lookupType(internalName);
TypeDefinition resolvedType = null;
if (type == null || ((resolvedType = type.resolve()) == null)) {
throw new Exception("Unable to resolve type.");
}
StringWriter stringwriter = new StringWriter();
DecompilationOptions decompilationOptions;
decompilationOptions = new DecompilationOptions();
decompilationOptions.setSettings(settings);
decompilationOptions.setFullDecompilation(true);
PlainTextOutput plainTextOutput = new PlainTextOutput(stringwriter);
plainTextOutput.setUnicodeOutputEnabled(
decompilationOptions.getSettings().isUnicodeOutputEnabled());
settings.getLanguage().decompileType(resolvedType, plainTextOutput,
decompilationOptions);
if (search(stringwriter.toString()))
addClassName(entry.getName());
}
} else {
StringBuilder sb = new StringBuilder();
long nonprintableCharactersCount = 0;
try (InputStreamReader inputStreamReader = new InputStreamReader(
jfile.getInputStream(entry));
BufferedReader reader = new BufferedReader(inputStreamReader);) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
for (byte nextByte : line.getBytes()) {
if (nextByte <= 0) {
nonprintableCharactersCount++;
}
}
}
}
if (nonprintableCharactersCount < 5 && search(sb.toString()))
addClassName(entry.getName());
}
}
}
setSearching(false);
if (findButton.getText().equals("Stop")) {
setStatus("Done.");
findButton.setText("Find");
locked = false;
}
jfile.close();
locked = false;
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
}
};
tmp_thread.start();
}
}
private boolean search(String bulk) {
String a = textField.getText();
String b = bulk;
if (regex.isSelected())
return Pattern.matches(a, b);
if (wholew.isSelected())
a = " " + a + " ";
if (!mcase.isSelected()) {
a = a.toLowerCase();
b = b.toLowerCase();
}
if (b.contains(a))
return true;
return false;
}
private void setHideOnEscapeButton() {
Action escapeAction = new AbstractAction() {
private static final long serialVersionUID = 6846566740472934801L;
@Override
public void actionPerformed(ActionEvent e) {
FindAllBox.this.setVisible(false);
}
};
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
}
private void adjustWindowPositionBySavedState() {
WindowPosition windowPosition = ConfigSaver.getLoadedInstance().getFindWindowPosition();
if (windowPosition.isSavedWindowPositionValid()) {
this.setLocation(windowPosition.getWindowX(), windowPosition.getWindowY());
}
}
private void setSaveWindowPositionOnClosing() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowDeactivated(WindowEvent e) {
WindowPosition windowPosition = ConfigSaver.getLoadedInstance().getFindWindowPosition();
windowPosition.readPositionFromDialog(FindAllBox.this);
}
});
}
public void showFindBox() {
this.setVisible(true);
this.textField.requestFocus();
}
public void hideFindBox() {
this.setVisible(false);
}
public void setStatus(String text) {
if (text.length() > 25) {
this.statusLabel.setText("Searching in file: ..." + text.substring(text.length() - 25));
} else {
this.statusLabel.setText("Searching in file: " + text);
}
progressBar.setValue(progressBar.getValue() + 1);
}
public void addClassName(String className) {
this.classesList.addElement(className);
}
public void initProgressBar(Integer length) {
progressBar.setMaximum(length);
progressBar.setValue(0);
progressBar.setStringPainted(true);
}
public boolean isSearching() {
return searching;
}
public void setSearching(boolean searching) {
this.searching = searching;
}
}