From 741e16f51d6dc91602ad5d38ab93ca936a2f3afe Mon Sep 17 00:00:00 2001 From: System2333 <2350884891@qq.com> Date: Tue, 13 Jul 2021 09:40:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=AD=A3=E5=88=99=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .../org/jd/gui/controller/MainController.java | 10 +- .../SearchInConstantPoolsController.java | 190 +++++++----------- .../main/java/org/jd/gui/view/MainView.java | 7 + .../gui/view/SearchInConstantPoolsView.java | 54 ++++- .../gui/view/component/AbstractTextPage.java | 18 +- .../view/component/ModuleInfoFilePage.java | 6 +- .../org/jd/gui/view/component/TypePage.java | 18 +- .../gui/view/component/TypeReferencePage.java | 7 +- 9 files changed, 167 insertions(+), 146 deletions(-) diff --git a/.gitignore b/.gitignore index f2fa73d0..f56a7c7e 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,6 @@ build/ # WinMerge *.bak + + +*/bin \ No newline at end of file diff --git a/app/src/main/java/org/jd/gui/controller/MainController.java b/app/src/main/java/org/jd/gui/controller/MainController.java index 94b0e881..87a4b1b7 100644 --- a/app/src/main/java/org/jd/gui/controller/MainController.java +++ b/app/src/main/java/org/jd/gui/controller/MainController.java @@ -52,6 +52,8 @@ import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; public class MainController implements API { protected Configuration configuration; @@ -304,10 +306,14 @@ protected void onFind() { mainView.showFindPanel(); } } - protected void onFindCriteriaChanged() { if (currentPage instanceof ContentSearchable) { - mainView.setFindBackgroundColor(((ContentSearchable)currentPage).highlightText(mainView.getFindText(), mainView.getFindCaseSensitive())); + try { + Pattern.compile(mainView.getFindText()); + mainView.setFindBackgroundColor(((ContentSearchable)currentPage).highlightText(mainView.getFindText(), mainView.getFindCaseSensitive()),null); + } catch (PatternSyntaxException e) { + mainView.setFindBackgroundColor(false,e.getMessage()); + } } } diff --git a/app/src/main/java/org/jd/gui/controller/SearchInConstantPoolsController.java b/app/src/main/java/org/jd/gui/controller/SearchInConstantPoolsController.java index d87182ba..fcfef821 100644 --- a/app/src/main/java/org/jd/gui/controller/SearchInConstantPoolsController.java +++ b/app/src/main/java/org/jd/gui/controller/SearchInConstantPoolsController.java @@ -20,8 +20,12 @@ import org.jd.gui.view.SearchInConstantPoolsView; import javax.swing.*; + +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.Charset; import java.util.*; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; @@ -210,99 +214,77 @@ protected HashSet getOuterEntries(Set matching return matchingOuterEntriesSet; } - protected void filter(Indexes indexes, String pattern, int flags, Set matchingEntries) { + protected void filter(Indexes indexes, String regex, int flags, Set matchingEntries) { boolean declarations = ((flags & SearchInConstantPoolsView.SEARCH_DECLARATION) != 0); boolean references = ((flags & SearchInConstantPoolsView.SEARCH_REFERENCE) != 0); - + Pattern pattern=Pattern.compile(regex, (flags&SearchInConstantPoolsView.SEARCH_MATCHCASE)!=0?Pattern.CASE_INSENSITIVE:0); if ((flags & SearchInConstantPoolsView.SEARCH_TYPE) != 0) { if (declarations) match(indexes, "typeDeclarations", pattern, - SearchInConstantPoolsController::matchTypeEntriesWithChar, SearchInConstantPoolsController::matchTypeEntriesWithString, matchingEntries); if (references) match(indexes, "typeReferences", pattern, - SearchInConstantPoolsController::matchTypeEntriesWithChar, SearchInConstantPoolsController::matchTypeEntriesWithString, matchingEntries); } if ((flags & SearchInConstantPoolsView.SEARCH_CONSTRUCTOR) != 0) { if (declarations) match(indexes, "constructorDeclarations", pattern, - SearchInConstantPoolsController::matchTypeEntriesWithChar, SearchInConstantPoolsController::matchTypeEntriesWithString, matchingEntries); if (references) match(indexes, "constructorReferences", pattern, - SearchInConstantPoolsController::matchTypeEntriesWithChar, SearchInConstantPoolsController::matchTypeEntriesWithString, matchingEntries); } if ((flags & SearchInConstantPoolsView.SEARCH_METHOD) != 0) { if (declarations) match(indexes, "methodDeclarations", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); if (references) match(indexes, "methodReferences", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); } if ((flags & SearchInConstantPoolsView.SEARCH_FIELD) != 0) { if (declarations) match(indexes, "fieldDeclarations", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); if (references) match(indexes, "fieldReferences", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); } if ((flags & SearchInConstantPoolsView.SEARCH_STRING) != 0) { if (declarations || references) match(indexes, "strings", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); } if ((flags & SearchInConstantPoolsView.SEARCH_MODULE) != 0) { if (declarations) match(indexes, "javaModuleDeclarations", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); if (references) match(indexes, "javaModuleReferences", pattern, - SearchInConstantPoolsController::matchWithChar, SearchInConstantPoolsController::matchWithString, matchingEntries); } } @SuppressWarnings("unchecked") - protected void match(Indexes indexes, String indexName, String pattern, - BiFunction, Map> matchWithCharFunction, - BiFunction, Map> matchWithStringFunction, + protected void match(Indexes indexes, String indexName, Pattern pattern, + BiFunction, Map> matchWithStringFunction, Set matchingEntries) { - int patternLength = pattern.length(); - + int patternLength = pattern.pattern().length(); + if (patternLength > 0) { - String key = String.valueOf(indexes.hashCode()) + "***" + indexName + "***" + pattern; + String key = String.valueOf(indexes.hashCode()) + "***" + indexName + "***" + pattern+"@"+pattern.flags(); Map matchedEntries = cache.get(key); if (matchedEntries == null) { Map index = indexes.getIndex(indexName); if (index != null) { - if (patternLength == 1) { - matchedEntries = matchWithCharFunction.apply(pattern.charAt(0), index); - } else { - String lastKey = key.substring(0, key.length() - 1); - Map lastMatchedTypes = cache.get(lastKey); - if (lastMatchedTypes != null) { - matchedEntries = matchWithStringFunction.apply(pattern, lastMatchedTypes); - } else { - matchedEntries = matchWithStringFunction.apply(pattern, index); - } - } + matchedEntries = matchWithStringFunction.apply(pattern, index); } // Cache matchingEntries @@ -317,29 +299,9 @@ protected void match(Indexes indexes, String indexName, String pattern, } } - protected static Map matchTypeEntriesWithChar(char c, Map index) { - if ((c == '*') || (c == '?')) { - return index; - } else { - Map map = new HashMap<>(); - - for (String typeName : index.keySet()) { - // Search last package separator - int lastPackageSeparatorIndex = typeName.lastIndexOf('/') + 1; - int lastTypeNameSeparatorIndex = typeName.lastIndexOf('$') + 1; - int lastIndex = Math.max(lastPackageSeparatorIndex, lastTypeNameSeparatorIndex); - if ((lastIndex < typeName.length()) && (typeName.charAt(lastIndex) == c)) { - map.put(typeName, index.get(typeName)); - } - } - - return map; - } - } - - protected static Map matchTypeEntriesWithString(String pattern, Map index) { - Pattern p = createPattern(pattern); + protected static Map matchTypeEntriesWithString(Pattern pattern, Map index) { + // Pattern p = createPattern(pattern); Map map = new HashMap<>(); for (String typeName : index.keySet()) { @@ -348,7 +310,7 @@ protected static Map matchTypeEntriesWithString(String patte int lastTypeNameSeparatorIndex = typeName.lastIndexOf('$') + 1; int lastIndex = Math.max(lastPackageSeparatorIndex, lastTypeNameSeparatorIndex); - if (p.matcher(typeName.substring(lastIndex)).matches()) { + if (pattern.matcher(typeName.substring(lastIndex)).matches()) { map.put(typeName, index.get(typeName)); } } @@ -356,28 +318,14 @@ protected static Map matchTypeEntriesWithString(String patte return map; } - protected static Map matchWithChar(char c, Map index) { - if ((c == '*') || (c == '?')) { - return index; - } else { - Map map = new HashMap<>(); - - for (String key : index.keySet()) { - if (!key.isEmpty() && (key.charAt(0) == c)) { - map.put(key, index.get(key)); - } - } - return map; - } - } - - protected static Map matchWithString(String pattern, Map index) { - Pattern p = createPattern(pattern); + protected static Map matchWithString(Pattern pattern, Map index) { + // Pattern p = createPattern(pattern); Map map = new HashMap<>(); for (String key : index.keySet()) { - if (p.matcher(key).matches()) { + // if (p.matcher(key).matches()) { + if (pattern.matcher(key).find()) { map.put(key, index.get(key)); } } @@ -393,7 +341,8 @@ protected static Map matchWithString(String pattern, Map 0) { @@ -120,7 +136,20 @@ public SearchInConstantPoolsView( @Override public void insertUpdate(DocumentEvent e) { call(); } @Override public void removeUpdate(DocumentEvent e) { call(); } @Override public void changedUpdate(DocumentEvent e) { call(); } - protected void call() { changedPatternCallback.accept(searchInConstantPoolsEnterTextField.getText(), getFlags()); } + protected void call() { + try { + String text=searchInConstantPoolsEnterTextField.getText(); + Pattern.compile(text); + searchInConstantPoolsEnterTextField.setBorder(defaultBorder); + searchInConstantPoolsEnterTextField.setBackground(defaultBackground); + searchInConstantPoolsEnterTextField.setToolTipText(null); + changedPatternCallback.accept(searchText=text, getFlags()); + } catch (PatternSyntaxException exception) { + searchInConstantPoolsEnterTextField.setBorder(new LineBorder(Color.decode("#FF0000"))); + searchInConstantPoolsEnterTextField.setBackground(Color.decode("#FFA0A0")); + searchInConstantPoolsEnterTextField.setToolTipText(exception.getLocalizedMessage()); + } + } }); vbox.add(Box.createVerticalStrut(10)); @@ -137,9 +166,10 @@ public SearchInConstantPoolsView( subpanel.add(subhbox, BorderLayout.WEST); ItemListener checkBoxListener = (e) -> { - changedPatternCallback.accept(searchInConstantPoolsEnterTextField.getText(), getFlags()); + changedPatternCallback.accept(getPattern(), getFlags()); searchInConstantPoolsEnterTextField.requestFocus(); }; + searchInConstantPoolsCheckBoxMatchCase.addItemListener(checkBoxListener); JPanel subsubpanel = new JPanel(); subsubpanel.setLayout(new GridLayout(2, 1)); @@ -210,7 +240,7 @@ public SearchInConstantPoolsView( if (e.getClickCount() == 2) { T node = (T)searchInConstantPoolsTree.getLastSelectedPathComponent(); if (node != null) { - selectedTypeCallback.accept(node.getUri(), searchInConstantPoolsEnterTextField.getText(), getFlags()); + selectedTypeCallback.accept(node.getUri(), getPattern(), getFlags()); } } } @@ -248,7 +278,7 @@ public SearchInConstantPoolsView( @Override public void actionPerformed(ActionEvent actionEvent) { T selectedTreeNode = (T)searchInConstantPoolsTree.getLastSelectedPathComponent(); if (selectedTreeNode != null) { - selectedTypeCallback.accept(selectedTreeNode.getUri(), searchInConstantPoolsEnterTextField.getText(), getFlags()); + selectedTypeCallback.accept(selectedTreeNode.getUri(), getPattern(), getFlags()); } } }; @@ -335,7 +365,7 @@ public void show() { public boolean isVisible() { return searchInConstantPoolsDialog.isVisible(); } - public String getPattern() { return searchInConstantPoolsEnterTextField.getText(); } + public String getPattern() { return searchText; } public int getFlags() { int flags = 0; @@ -356,7 +386,9 @@ public int getFlags() { flags += SEARCH_DECLARATION; if (searchInConstantPoolsCheckBoxReferences.isSelected()) flags += SEARCH_REFERENCE; - + if(!searchInConstantPoolsCheckBoxMatchCase.isSelected()){ + flags+=SEARCH_MATCHCASE; + } return flags; } diff --git a/services/src/main/java/org/jd/gui/view/component/AbstractTextPage.java b/services/src/main/java/org/jd/gui/view/component/AbstractTextPage.java index 88cc7490..f8d18ebd 100644 --- a/services/src/main/java/org/jd/gui/view/component/AbstractTextPage.java +++ b/services/src/main/java/org/jd/gui/view/component/AbstractTextPage.java @@ -27,6 +27,7 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -259,7 +260,7 @@ public boolean highlightText(String text, boolean caseSensitive) { textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR); textArea.setCaretPosition(textArea.getSelectionStart()); - SearchContext context = newSearchContext(text, caseSensitive, false, true, false); + SearchContext context = newSearchContext(text, caseSensitive, false, true, true); SearchResult result = SearchEngine.find(textArea, context); if (!result.wasFound()) { @@ -277,7 +278,7 @@ public void findNext(String text, boolean caseSensitive) { if (text.length() > 1) { textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR); - SearchContext context = newSearchContext(text, caseSensitive, false, true, false); + SearchContext context = newSearchContext(text, caseSensitive, false, true, true); SearchResult result = SearchEngine.find(textArea, context); if (!result.wasFound()) { @@ -291,7 +292,7 @@ public void findPrevious(String text, boolean caseSensitive) { if (text.length() > 1) { textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR); - SearchContext context = newSearchContext(text, caseSensitive, false, false, false); + SearchContext context = newSearchContext(text, caseSensitive, false, false, true); SearchResult result = SearchEngine.find(textArea, context); if (!result.wasFound()) { @@ -341,13 +342,15 @@ public boolean openUri(URI uri) { } else if (parameters.containsKey("highlightFlags")) { String highlightFlags = parameters.get("highlightFlags"); - if ((highlightFlags.indexOf('s') != -1) && parameters.containsKey("highlightPattern")) { + if (/*(highlightFlags.indexOf('s') != -1) && */parameters.containsKey("highlightPattern")) { textArea.setMarkAllHighlightColor(SELECT_HIGHLIGHT_COLOR); textArea.setCaretPosition(0); - + boolean matchCase=highlightFlags.indexOf("X")!=-1; // Highlight all - String searchFor = createRegExp(parameters.get("highlightPattern")); - SearchContext context = newSearchContext(searchFor, true, false, true, true); + // String searchFor = createRegExp(parameters.get("highlightPattern")); + // String searchFor = URLDecoder.decode(parameters.get("highlightPattern"), Charset.forName("utf-8")); + String searchFor = parameters.get("highlightPattern");//URLDecoder.decode(parameters.get("highlightPattern"), Charset.forName("utf-8")); + SearchContext context = newSearchContext(searchFor, matchCase, false, true, true); SearchResult result = SearchEngine.find(textArea, context); if (result.getMatchRange() != null) { @@ -392,6 +395,7 @@ protected Map parseQuery(String query) { * '*' matchTypeEntries 0 ou N characters * '?' matchTypeEntries 1 character */ + @Deprecated public static String createRegExp(String pattern) { int patternLength = pattern.length(); StringBuilder sbPattern = new StringBuilder(patternLength * 2); diff --git a/services/src/main/java/org/jd/gui/view/component/ModuleInfoFilePage.java b/services/src/main/java/org/jd/gui/view/component/ModuleInfoFilePage.java index 29609c9e..0e0ac7e7 100644 --- a/services/src/main/java/org/jd/gui/view/component/ModuleInfoFilePage.java +++ b/services/src/main/java/org/jd/gui/view/component/ModuleInfoFilePage.java @@ -157,8 +157,10 @@ public boolean openUri(URI uri) { String highlightPattern = parameters.get("highlightPattern"); if ((highlightFlags != null) && (highlightPattern != null)) { - String regexp = createRegExp(highlightPattern); - Pattern pattern = Pattern.compile(regexp + ".*"); + + // String regexp = createRegExp(highlightPattern); + // Pattern pattern = Pattern.compile(regexp + ".*"); + Pattern pattern = Pattern.compile(highlightPattern,highlightPattern.indexOf("X")!=-1? Pattern.CASE_INSENSITIVE:0); boolean t = (highlightFlags.indexOf('t') != -1); // Highlight types boolean M = (highlightFlags.indexOf('M') != -1); // Highlight modules diff --git a/services/src/main/java/org/jd/gui/view/component/TypePage.java b/services/src/main/java/org/jd/gui/view/component/TypePage.java index 99dcaa9b..29d8977c 100644 --- a/services/src/main/java/org/jd/gui/view/component/TypePage.java +++ b/services/src/main/java/org/jd/gui/view/component/TypePage.java @@ -235,18 +235,19 @@ public static void matchQueryAndAddDocumentRange( if ((highlightFlags != null) && (highlightPattern != null)) { String highlightScope = parameters.get("highlightScope"); - String regexp = createRegExp(highlightPattern); - Pattern pattern = Pattern.compile(regexp + ".*"); + // String regexp = createRegExp(highlightPattern); + // Pattern pattern = Pattern.compile(regexp + ".*"); + Pattern pattern = Pattern.compile(highlightPattern,highlightPattern.indexOf("X")!=-1? Pattern.CASE_INSENSITIVE:0); if (highlightFlags.indexOf('s') != -1) { // Highlight strings - Pattern patternForString = Pattern.compile(regexp); + // Pattern patternForString = Pattern.compile(regexp); + Pattern patternForString = pattern; for (StringData data : strings) { if (matchScope(highlightScope, data.owner)) { Matcher matcher = patternForString.matcher(data.text); int offset = data.startPosition; - while(matcher.find()) { ranges.add(new DocumentRange(offset + matcher.start(), offset + matcher.end())); } @@ -254,6 +255,7 @@ public static void matchQueryAndAddDocumentRange( } } + boolean s = (highlightFlags.indexOf('s') != -1); // Highlight types boolean t = (highlightFlags.indexOf('t') != -1); // Highlight types boolean f = (highlightFlags.indexOf('f') != -1); // Highlight fields boolean m = (highlightFlags.indexOf('m') != -1); // Highlight methods @@ -271,6 +273,9 @@ public static void matchQueryAndAddDocumentRange( if ((f && declaration.isAField()) || (m && declaration.isAMethod())) { matchAndAddDocumentRange(pattern, declaration.name, declaration.startPosition, declaration.endPosition, ranges); } + if ((s && declaration.isAField())) { + findAndAddDocumentRange(pattern, declaration.name, declaration.startPosition, declaration.endPosition, ranges); + } } } } @@ -307,6 +312,11 @@ public static void matchAndAddDocumentRange(Pattern pattern, String text, int st ranges.add(new DocumentRange(start, end)); } } + public static void findAndAddDocumentRange(Pattern pattern, String text, int start, int end, List ranges) { + if (pattern.matcher(text).find()) { + ranges.add(new DocumentRange(start, end)); + } + } public static String getMostInnerTypeName(String typeName) { int lastPackageSeparatorIndex = typeName.lastIndexOf('/') + 1; diff --git a/services/src/main/java/org/jd/gui/view/component/TypeReferencePage.java b/services/src/main/java/org/jd/gui/view/component/TypeReferencePage.java index b8f4c169..00ad1f9d 100644 --- a/services/src/main/java/org/jd/gui/view/component/TypeReferencePage.java +++ b/services/src/main/java/org/jd/gui/view/component/TypeReferencePage.java @@ -57,11 +57,12 @@ public boolean openUri(URI uri) { String highlightPattern = parameters.get("highlightPattern"); if ((highlightFlags != null) && (highlightPattern != null)) { - String regexp = createRegExp(highlightPattern); + // String regexp = createRegExp(highlightPattern); + Pattern pattern = Pattern.compile(highlightPattern,highlightPattern.indexOf("X")!=-1? Pattern.CASE_INSENSITIVE:0); if (highlightFlags.indexOf('s') != -1) { // Highlight strings - Pattern pattern = Pattern.compile(regexp); + // Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(textArea.getText()); while (matcher.find()) { @@ -71,7 +72,7 @@ public boolean openUri(URI uri) { if ((highlightFlags.indexOf('t') != -1) && (highlightFlags.indexOf('r') != -1)) { // Highlight type references - Pattern pattern = Pattern.compile(regexp + ".*"); + // Pattern pattern = Pattern.compile(regexp + ".*"); for (Map.Entry entry : hyperlinks.entrySet()) { TypeHyperlinkData hyperlink = (TypeHyperlinkData)entry.getValue();