void () { String in = """ public class MyFile { void test(String a, Object b) { if(a.length() == 0) { System.out.println("empty"); } if(((String) b).length() == 0) { System.out.println("empty"); } } }"""; String what = "'_expr:[exprtype( String )].length() == 0"; String by = "$expr$.isEmpty()"; assertEquals("parentheses should remain", """ public class MyFile { void test(String a, Object b) { if(a.isEmpty()) { System.out.println("empty"); } if(((String) b).isEmpty()) { System.out.println("empty"); } } }""", replace(in, what, by, true)); options.getMatchOptions().setRecursiveSearch(true); String in2 = """ class X {{ int i = (((3))); }}"""; String what2 = "('_expr:[exprtype( int )])"; String by2 = "2"; assertEquals("don't throw exceptions when replacing", """ class X {{ int i = 2; }}""", replace(in2, what2, by2, true)); }