void () { String source = """ class A {{ try (InputStream in = new FileInputStream("tmp")) { } try { } catch (FileNotFoundException e) { } finally {} try { } catch(NullPointerException | UnsupportedOperationException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } finally {} try { throw new NoRouteToHostException(); } catch (NoRouteToHostException e) { System.out.println(); } catch (SocketException e) { System.out.println(); } catch (IOException e) { } catch (RuntimeException e) { System.out.println(); } finally {} }}"""; String pattern1 = "try ('_ResourceType '_Var = '_exp) { '_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)); String pattern4 = "try { '_St1*; } catch (NullPointerException | IllegalArgumentException '_e) { '_St2*; }"; assertEquals("Match multi catch correctly", 0, findMatchesCount(source, pattern4)); String pattern5 = "try { '_St1*; } catch (UnsupportedOperationException | NullPointerException '_e) { '_St2*; }"; assertEquals("Find multi catch", 1, findMatchesCount(source, pattern5)); String pattern6 = "try { '_St1*; } catch ('_E1 | '_E2 '_e) { '_St2*; }"; assertEquals("Find multi catch with variables", 1, findMatchesCount(source, pattern6)); String pattern7 = "try { '_St1*; } catch ('E '_e) { '_St2*; }"; final List<MatchResult> matches = findMatches(source, pattern7); assertEquals(3, matches.size()); assertEquals("NullPointerException | UnsupportedOperationException", matches.get(1).getMatchImage()); String pattern8 = "try { '_St1*; } catch ('_E '_e{2,2}) { '_St2*; }"; final List<MatchResult> matches2 = findMatches(source, pattern8); assertEquals(1, matches2.size()); assertEquals("Find try with exactly 2 catch blocks", """ try { } catch(NullPointerException | UnsupportedOperationException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } finally {}""", matches2.get(0).getMatchImage()); String pattern9 = "try { '_st1*; } catch ('_E '_e{0,0}) { '_St2*; }"; final List<MatchResult> matches3 = findMatches(source, pattern9); assertEquals(1, matches3.size()); assertEquals("Should find try without catch blocks", "try (InputStream in = new FileInputStream(\"tmp\")) {\n" + " }", matches3.get(0).getMatchImage()); String source2 = """ class X {{ try {} catch (Thowable e) {} finally {} try {} finally {} }}"""; String pattern10 = "try { '_st1*; } catch ('_E '_e{0,0}) { '_St2*; } finally { '_St3*; }"; final List<MatchResult> matches4 = findMatches(source2, pattern10); assertEquals(1, matches4.size()); assertEquals("Should find try without catch blocks", "try {} finally {}", matches4.get(0).getMatchImage()); }