void () { final String source = """ package a; class BX extends java.util.List { private static final java.util.List VALUE = new BX(); } class CX extends java.util.List { private static final String S = ""; }"""; // find static final fields whose type is a proper ancestor of the class declaring their fields assertEquals("all variables accessible from script", 1, findMatchesCount(source, "[script(\"" + "import com.intellij.psi.util.InheritanceUtil\n" + "import com.intellij.psi.util.PsiTreeUtil\n" + "import com.intellij.psi.PsiClass\n" + "init != null &&" + // redundant reference to '_init "InheritanceUtil.isInheritor(\n" + " PsiTreeUtil.getParentOfType(variable, PsiClass.class),\n" + // reference to 'variable " true, \n" + " Type.type.canonicalText\n" + // reference to '_Type ")\n\")]" + "static final '_Type 'variable = '_init;")); final String source2 = """ class A { String s = new String(); @SuppressWarnings("") int m() { n(); int i = 2+1; return i; } void n() {} }"""; assertEquals("type of variables in script are as expected", 1, findMatchesCount(source2, """ [script(" import com.intellij.psi.* __context__ instanceof PsiElement && a instanceof PsiClass && b instanceof PsiTypeElement && c instanceof PsiField && d instanceof PsiNewExpression && e instanceof PsiTypeElement && f instanceof PsiMethod && g instanceof PsiTypeElement && h instanceof PsiLocalVariable && i instanceof PsiPolyadicExpression && j instanceof PsiReferenceExpression && k instanceof PsiMethodCallExpression && l instanceof PsiAnnotation ")] class '_a { '_b '_c = new '_d(); @'_l '_e '_f() { '_k(); '_g '_h = '_i; return '_j; } }""")); assertEquals("Current variable should be available under own name", 1, findMatchesCount(source2, "'_a + '_b:[script(\"__log__.info(b)\n__log__.info(__context__)\ntrue\")]")); final String in = """ class C { { int i = 0; i += 1; (i) = 3; int j = i; i(); } void i() {} }"""; // existing pattern "fields/variables read" assertEquals("Find reads of symbol (including operator assignment)", 2, findMatchesCount(in, """ '_Symbol:[script("import com.intellij.psi.* import static com.intellij.psi.util.PsiUtil.* Symbol instanceof PsiReferenceExpression && isAccessedForReading(Symbol)")]""")); // existing pattern "fields/variables with given name pattern updated" assertEquals("Find writes of symbol", 3, findMatchesCount(in, """ '_Symbol:[regex( i ) && script("import com.intellij.psi.* import static com.intellij.psi.util.PsiUtil.* Symbol instanceof PsiExpression && isAccessedForWriting(Symbol) || Symbol instanceof PsiVariable && Symbol.getInitializer() != null")]""")); try { findMatchesCount(in, "[script( com.intellij.psi.PsiField field = __context__; true; )]\n" + "int i;"); fail("Catch RuntimeExceptions from Groovy runtime"); } catch (StructuralSearchException ignore) { } catch (Throwable t) { fail("Catch RuntimeExceptions from Groovy runtime"); } String source3 = """ class X {{ new String(); }}"""; assertEquals("Variables initialized to null even when not present in search results", 1, findMatchesCount(source3, "[script(\"args == null\")]new String('_args*)")); String source4 = """ class X {{ // comment new /*!*/ Object() {}; }}"""; assertEquals("expected variable of type anonymous class", 1, findMatchesCount(source4, "[script (\"XX instanceof com.intellij.psi.PsiAnonymousClass\")]new 'XX()")); assertEquals("expected variable of type anonymous class 2", 1, findMatchesCount(source4, "[script (\"XX instanceof com.intellij.psi.PsiAnonymousClass\")]class 'XX {}")); assertEquals("expected variable of type anonymous class 3", 1, findMatchesCount(source4, "[script (\"__context__ instanceof com.intellij.psi.PsiExpressionStatement\")]new Object() {};")); assertEquals("expected variable of type anonymous class 4", 1, findMatchesCount(source4, "[script (\"__context__ instanceof com.intellij.psi.PsiAnonymousClass\")]class 'XX {}")); }