IDEA-129079 (Using structural search to find multiply try/catch/catch statements)

This commit is contained in:
Bas Leijdekkers
2015-04-22 18:58:29 +02:00
parent 045ac0db5d
commit d966c2eb07
2 changed files with 43 additions and 7 deletions
@@ -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;
}
@@ -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));
}
}