From d966c2eb07ddd7ba52370f19da2710fd6fc158eb Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 22 Apr 2015 17:46:31 +0200 Subject: [PATCH] IDEA-129079 (Using structural search to find multiply try/catch/catch statements) --- .../impl/matcher/JavaMatchingVisitor.java | 15 ++++---- .../StructuralSearchTest.java | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java index 14a71763e4a2..42f113f281f0 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java @@ -1384,11 +1384,11 @@ public class JavaMatchingVisitor extends JavaElementVisitor { final PsiCatchSection[] catches2 = try2.getCatchSections(); final PsiCodeBlock finally2 = try2.getFinallyBlock(); - final boolean looseMatching = myMatchingVisitor.getMatchContext().getOptions().isLooseMatching(); - if (!looseMatching && + if (!myMatchingVisitor.getMatchContext().getOptions().isLooseMatching() && ((catches1.length == 0 && catches2.length != 0) || (finally1 == null && finally2 != null) || - (resourceList1 == null && resourceList2 != null)) + (resourceList1 == null && resourceList2 != null)) || + catches2.length < catches1.length ) { myMatchingVisitor.setResult(false); } @@ -1410,17 +1410,18 @@ public class JavaMatchingVisitor extends JavaElementVisitor { ContainerUtil.addAll(unmatchedCatchSections, catches2); - for (int i = 0, j; i < catches1.length; ++i) { - MatchingHandler handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(catches1[i]); + for (PsiCatchSection catchSection : catches1) { + final MatchingHandler handler = myMatchingVisitor.getMatchContext().getPattern().getHandler(catchSection); final PsiElement pinnedNode = handler.getPinnedNode(null); if (pinnedNode != null) { - myMatchingVisitor.setResult(handler.match(catches1[i], pinnedNode, myMatchingVisitor.getMatchContext())); + myMatchingVisitor.setResult(handler.match(catchSection, pinnedNode, myMatchingVisitor.getMatchContext())); if (!myMatchingVisitor.getResult()) return; } else { + int j; for (j = 0; j < unmatchedCatchSections.size(); ++j) { - if (handler.match(catches1[i], unmatchedCatchSections.get(j), myMatchingVisitor.getMatchContext())) { + if (handler.match(catchSection, unmatchedCatchSections.get(j), myMatchingVisitor.getMatchContext())) { unmatchedCatchSections.remove(j); break; } diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java index 2f7d631b7632..3c7f704343cf 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java @@ -3189,4 +3189,39 @@ public class StructuralSearchTest extends StructuralSearchTestCase { String pattern12 = "@x"; assertEquals("should find annotation case insensitively", 1, findMatchesCount(source, pattern12)); } + + public void testFindTry() { + String source = "class A {{\n" + + " try (InputStream in = new FileInputStream(\"tmp\")) {\n" + + " }\n" + + " try {\n" + + " } catch (FileNotFoundException e) {\n" + + " } finally {}\n" + + " try {\n" + + " } catch(NullPointerException | UnsupportedOperationException e) {\n" + + " throw e;\n" + + " } catch(Exception e) {\n" + + " throw new RuntimeException(e);\n" + + " } finally {}\n" + + " try {\n" + + " throw new NoRouteToHostException();\n" + + " } catch (NoRouteToHostException e) {\n" + + " System.out.println();\n" + + " } catch (SocketException e) {\n" + + " System.out.println();\n" + + " } catch (IOException e) {\n" + + " } catch (RuntimeException e) {\n" + + " System.out.println();\n" + + " } finally {}\n" + + "}}"; + + String pattern1 = "try ('_Resource) { '_Statement*; }"; + assertEquals("Find try-with-resources", 1, findMatchesCount(source, pattern1)); + + String pattern2 = "try { '_St1*; } catch ('_ExceptionType1 '_e1) { '_St2*; } catch ('_ExceptionType2 '_e2) { '_St3*; }"; + assertEquals("Find try with two or more catch blocks", 2, findMatchesCount(source, pattern2)); + + String pattern3 = "try { '_St1*; } finally { '_St2*; }"; + assertEquals("Find try with finally block", 3, findMatchesCount(source, pattern3)); + } }