void () { final String s2 = "class X {{ ((dialog==null)? (dialog = new SearchDialog()): dialog).show(); }}"; assertFalse("subexpr match", findMatchesCount(s2, "dialog = new SearchDialog()") == 0); final String s10 = """ class X {{ listener.add(new Runnable() { public void run() {} }); }}"""; assertEquals("search for new ", 0, findMatchesCount(s10, " new XXX()")); final String s12 = """ class X {{ new Runnable() { public void run() { matchContext.getSink().matchingFinished(); } }; }}"""; assertEquals("search for anonymous classes", 1, findMatchesCount(s12, "new Runnable() {}")); String source = """ import java.util.*; class X {{ new ArrayList() {}; new ArrayList(); new ArrayList<String>(); new ArrayList<String>() {}; }}"""; assertEquals("search for parameterized anonymous class", 1, findMatchesCount(source, "new '_A<'_B>() {}")); final String s53 = """ class C { String a = System.getProperty("abcd"); static { String s = System.getProperty(a); } static void b() { String s = System.getProperty(a); } }"""; assertEquals("expr in def initializer", 3, findMatchesCount(s53, "System.getProperty('T)")); final String s55 = """ class X {{ a = b.class; }}"""; assertEquals("a.class expression", 1, findMatchesCount(s55, "'T.class")); String complexCode = """ class X {{ interface I { void b(); } interface I2 extends I {} class I3 extends I {} class A implements I2 { void b() {} } class B implements I3 { void b() {}} I2 a; I3 b; a.b(); b.b(); b.b(); A c; B d; c.b(); d.b(); d.b(); }}"""; assertEquals("expr type condition", 1, findMatchesCount(complexCode, "'t:[exprtype( I2 )].b();")); assertEquals("expr type condition 2", 5, findMatchesCount(complexCode, "'t:[!exprtype( I2 )].b();")); assertEquals("expr type condition 3", 2, findMatchesCount(complexCode, "'t:[exprtype( *I2 )].b();")); assertEquals("expr type condition 4", 4, findMatchesCount(complexCode, "'t:[!exprtype( *I2 )].b();")); String complexCode2 = "enum X { XXX, YYY }\n class C { static void ordinal() {} void test() { C c; c.ordinal(); c.ordinal(); X.XXX.ordinal(); } }"; assertEquals("expr type condition with enums", 1, findMatchesCount(complexCode2, "'t:[exprtype( *java\\.lang\\.Enum )].ordinal()")); final String in = """ class X {{ processInheritors(1,2,3,4); processInheritors(1,2,3); processInheritors(1,2,3,4,5,6); }}"""; assertEquals("no smart detection of search target", 3, findMatchesCount(in, "'_instance?.processInheritors('_param1{1,6});")); String someCode = "class X {{ a *= 2; a+=2; }}"; assertEquals("Improper *= 2 search", 1, findMatchesCount(someCode, "a *= 2;")); String s1 = """ class X {{ Thread t = new Thread("my thread","my another thread") { public void run() { // do stuff } }; }}"""; assertEquals("Find inner class parameters", 2, findMatchesCount(s1, "new Thread('args) { '_Other* }")); String s3 = """ class X {{ Thread t = new Thread("my thread") { public void run() { // do stuff } }; }}"""; assertEquals("Find inner class by new", 1, findMatchesCount(s3, "new Thread('_args)")); String s5 = """ class A { public static <T> T[] copy(T[] array, Class<T> aClass) { int i = (int)0; int b = (int)0; return (T[])array.clone(); } }"""; assertEquals("Find cast to array", 1, findMatchesCount(s5, "('_T[])'_expr")); String s6 = """ import java.util.HashMap; class X { HashMap x() { x(); return null; } }"""; assertEquals("Find expression only once for method call", 1, findMatchesCount(s6, "'Clz:[exprtype( java.util.HashMap )]")); String s7 = """ import java.math.BigDecimal; public class Prorator { public void prorate(BigDecimal[] array) { // do nothing } public void prorate2(java.math.BigDecimal[] array) { // do nothing } public void prorate(BigDecimal bd) { // do nothing } public static void main(String[] args) { BigDecimal[] something = new BigDecimal[2]; java.math.BigDecimal[] something2 = new BigDecimal[2]; something[0] = new BigDecimal(1.0); something[1] = new BigDecimal(1.0); Prorator prorator = new Prorator(); // --------------------------------------------------- // the line below should've been found, in my opinion. // -------------------------------------------------- prorator.prorate(something); prorator.prorate(something2); prorator.prorate(something[0]); prorator.prorate(something[1]); prorator.prorate(something[0]); } }"""; assertEquals("Find method call with array for parameter expr type", 2, findMatchesCount(s7, "'_Instance.'_MethodCall:[regex( prorate )]('_Param:[exprtype( BigDecimal\\[\\] )]) ")); String s13 = "class X {{ try { } catch(Exception e) { e.printStackTrace(); }}}"; assertEquals("Find statement in catch", 1, findMatchesCount(s13, "'_Instance.'_MethodCall('_Parameter*)")); String s4 = """ class X {{ int time = 99; String str = time < 0 ? "" : ""; String str2 = time < time ? "" : ""; }}"""; assertEquals("Find expressions mistaken for declarations by parser in block mode", 1, findMatchesCount(s4, "time < time")); assertEquals("Find expressions mistaken for declarations by parser in block mode 2", 1, findMatchesCount(s4, "time < 0")); assertEquals("Find expressions mistaken for declarations by parser in block mode 3", 1, findMatchesCount(s4, "time < 0 ? '_a : '_b")); assertEquals("Find expressions mistaken for declarations by parser in block mode 4", 2, findMatchesCount(s4, "'_a < '_b")); String s11 = """ import java.io.*; class X { void m() throws IOException { try (InputStream in = null) {} } }"""; assertEquals("Find expression inside try-with-resources", 1, findMatchesCount(s11, "null")); }