Unnamed: 0
int64
0
305k
body
stringlengths
7
52.9k
name
stringlengths
1
185
291,700
String () { return field; }
getField
291,701
void (String [] args) { Limitless myClass = new Limitless(); System.out.println(myClass.getField()+" "+myClass.getId()); Example example = new Example(1, "name"); int r = example.getI()+9; myClass.getId(); }
main
291,702
int () { return id; }
getId
291,703
String () { return field; }
getField
291,704
void (String [] args) { Limitless myClass = new Limitless(); System.out.println(myClass.field+" "+myClass.id); Example example = new Example(1, "name"); int r = example.getI()+9; myClass.id; }
main
291,705
void () { final String in = """ class A extends TestCase { void testOne() { System.out.println(); } } """; final String what = "class '_A { void '_b:[regex( test.* )](); }"; final String by = "class $A$ {\n @java.lang.Override void $b$();\n}"; assertEquals(""" class A extends TestCase { @Override void testOne() { System.out.println(); } } """, replace(in, what, by, true)); final String what2 = "void '_a:[regex( test.* )]();"; final String by2 = "@org.junit.Test void $a$();"; assertEquals(""" class A extends TestCase { @org.junit.Test void testOne() { System.out.println(); } } """, replace(in, what2, by2)); }
testMethodContentReplacement
291,706
void () { final String in = """ abstract class A { abstract void a(); }"""; final String what = "void '_a();"; final String by = "void $a$(int i);"; assertEquals(""" abstract class A { abstract void a(int i); }""", replace(in, what, by)); final String what2 = "abstract void '_a('_T '_p*);"; final String by2 = "void $a$($T$ $p$) {}"; assertEquals(""" abstract class A { void a() {} }""", replace(in, what2, by2)); }
testReplaceMethodWithoutBody
291,707
void () { final String in = """ class A { void a(int b) {} }"""; final String what = "int '_a = '_b{0,1};"; final String by = "final long /*!*/ $a$ = $b$;"; assertEquals(""" class A { void a(final long /*!*/ b) {} }""", replace(in, what, by)); final String in2 = """ class X { void m() { for (int x : new int[]{1, 2, 3}) {} } }"""; final String what2 = "'_T '_v = '_i{0,1};"; final String by2 = "final $T$ /*!*/ $v$ = $i$;"; assertEquals("foreach parameter replaced incorrectly", """ class X { void m() { for (final int /*!*/ x : new int[]{1, 2, 3}) {} } }""", replace(in2, what2, by2)); }
testReplaceParameterWithComment
291,708
void () { String in = """ public class A { public class B<T> extends A implements java.io.Serializable {} }"""; String what = """ class '_A { class '_B {} }"""; String by = """ class $A$ { private class $B$ { } }"""; assertEquals(""" public class A { private class B<T> extends A implements java.io.Serializable { } }""", replace(in, what, by)); String in2 = """ public class A { void m1() {} public void m2() {} public class B<T> extends A implements java.io.Serializable { int zero() { return 0; } } void m3() {} }"""; assertEquals("should replace unmatched class content correctly", """ public class A { void m1() {} public void m2() {} private class B<T> extends A implements java.io.Serializable { int zero() { return 0; } } void m3() {} }""", replace(in2, what, by)); }
testReplaceInnerClass
291,709
void () {}
m2
291,710
void () {}
m2
291,711
void () { String in = """ class A { String s; void setS(String s) { System.out.println(this.s); this.s = s; } }"""; String what = "System.out.println('_a);"; String by = "System.out.println(\"$a$\" + $a$);"; assertEquals("don't drop this", """ class A { String s; void setS(String s) { System.out.println("this.s" + this.s); this.s = s; } }""", replace(in, what, by)); }
testReplaceQualifiedReference
291,712
void () { String in = """ class A { void m() { new Object(); } }"""; String what = "'_expr;"; String by = "$expr$.toString();"; assertEquals("too many semicolons", """ class A { void m() { new Object().toString(); } }""", replace(in, what, by, true)); }
testReplaceExpressionStatement
291,713
void () { String in = """ class X { private final int i = 1; }"""; String what = "int '_v;"; String by = "long $v$;"; assertEquals("initializer should remain", """ class X { private final long i=1; }""", replace(in, what, by, true)); }
testReplaceVariableInitializer
291,714
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)); }
testReplaceParentheses
291,715
void () { String in = """ import org.junit.Test; class Help { private String s = "hello"; @Test public void testThisThing(){ System.out.println(); System.out.println(); System.out.println(); s = null; } }"""; String what = """ class 'Class { '_FieldType '_FieldName; @'_Annotation '_MethodType '_MethodName() { '_Statement*; '_FieldName = null; } }"""; String by = """ class $Class$ { $FieldType$ $FieldName$; @$Annotation$ $MethodType$ $MethodName$() { $Statement$; } }"""; assertEquals(""" import org.junit.Test; class Help { private String s="hello"; @Test public void testThisThing() { System.out.println(); System.out.println(); System.out.println(); } }""", replace(in, what, by, true)); }
testReplaceTarget
291,716
void () { System.out.println(); System.out.println(); System.out.println(); s = null; }
testThisThing
291,717
void () { options.setToShortenFQN(false); String in = """ import java.util.ArrayList; import java.util.List; class X { List<String> list = new java.util.LinkedList<String>(); List<Integer> list2 = new java.util.ArrayList<Integer>(); List<Double> list3 = new ArrayList<>(); }"""; assertEquals("should properly replace with diamond", """ import java.util.ArrayList; import java.util.List; class X { List<String> list = new java.util.LinkedList<>(); List<Integer> list2 = new ArrayList<>(); List<Double> list3 = new ArrayList<>(); }""", replace(in, "new '_X<'_p+>()", "new $X$<>()", true)); assertEquals("should keep generics when matching without", """ import java.util.ArrayList; import java.util.List; class X { List<String> list = new /*1*/java.util.LinkedList<String>(); List<Integer> list2 = new /*1*/ArrayList<Integer>(); List<Double> list3 = new /*1*/ArrayList<>(); }""", replace(in, "new '_X()", "new /*1*/$X$()", true)); assertEquals("should not duplicate generic parameters", """ import java.util.ArrayList; import java.util.List; class X { List<String> list = new java.util.LinkedList</*0*/String>(); List<Integer> list2 = new ArrayList</*0*/Integer>(); List<Double> list3 = new ArrayList<>(); }""", replace(in, "new '_X<'_p+>()", "new $X$</*0*/$p$>()", true)); String in2 = """ import java.util.Map; import java.util.List; import java.util.concurrent.ConcurrentHashMap; class X<A, B>{ void x() { Map<String, List<String>> myVar = new ConcurrentHashMap<>(10, 2); } } """; assertEquals("replace multiple generic parameters correctly", """ import java.util.Map; import java.util.List; import java.util.concurrent.ConcurrentHashMap; class X<A, B>{ void x() { var myVar = new ConcurrentHashMap<String, List<String>>(10, 2); } } """, replace(in2, "'_Type<'_GenericArgument+> '_Var = new '_Ctor<>('_Params*);", "var $Var$ = new $Ctor$<$GenericArgument$>($Params$);", true)); assertEquals("replace multiple class type parameters correctly", """ import java.util.Map; import java.util.List; import java.util.concurrent.ConcurrentHashMap; class X<A, B> { void x() { Map<String, List<String>> myVar = new ConcurrentHashMap<>(10, 2); } } """, replace(in2, "class '_C<'_P+> {}", "class $C$<$P$> {}", true)); }
testReplaceGenerics
291,718
void () { String in = """ public abstract class Bar { String[] x; abstract String[] foo(String[] x); }"""; assertEquals("should keep array brackets 1", """ public abstract class Bar { String[] x; abstract String[] foo(String[] x); }""", replace(in, "'_FieldType 'Field = '_Init?;", "$FieldType$ $Field$ = $Init$;", true)); assertEquals("should keep array brackets 2", """ public abstract class Bar { String[] x; abstract String[] foo (String[] x); }""", replace(in, "'_ReturnType '_Method('_ParameterType '_Parameter*);", "$ReturnType$ $Method$ ($ParameterType$ $Parameter$);", true)); String in2 = """ class X { public final X[] EMPTY_ARRAY = {}; }"""; assertEquals("shouldn't delete semicolon", """ class X { public final X[] EMPTY_ARRAY = {}; }""", replace(in2, "'_FieldType 'Field = '_Init?;", "$FieldType$ $Field$ = $Init$;", true)); String in3 = """ class X { void x(int... ss) {} void y() { x(new int[] {1, 2});\s } } """; assertEquals("Should keep commas", """ class X { void x(int... ss) {} void y() { x(1, 2);\s } } """, replace(in3, "new int[] {'_a*}", "$a$", true)); assertEquals("Should keep commas 2", """ class X { void x(int... ss) {} void y() { x(new long[] {1, 2});\s } } """, replace(in3, "new int[] {'_a*}", "new long[] {$a$}")); }
testArrays
291,719
void () { String in = """ class X { void x() {} void y() { x(); this.x(); } }"""; assertEquals("replace (un)qualified calls correctly", """ class X { void x() {} void y() { x(); this.x(); } }""", replace(in, "'_Instance?.'_MethodCall('_arguments*)", "$Instance$.$MethodCall$($arguments$)", true)); String in2 = """ class X { void x() { System.out.println("" + Some.x()); } static class Some { int x() { return 1; } } } """; assertEquals("copy unmatched qualifiers", """ class X { void x() { System.out.println(Some.x()); } static class Some { int x() { return 1; } } } """, replace(in2, "System.out.println(\"\"+'_x());", "System.out.println($x$());", true)); }
testMethodCall
291,720
void () { String in = "@Deprecated\n" + "public class X {}"; final String what = "class '_X {}"; final String replacement = "/** comment */\n" + "class $X$ {}"; final String expected = """ /** comment */ @Deprecated public class X {}"""; assertEquals("keep newline in modifier list", expected, replace(in, what, replacement, true)); }
testKeepModifierFormatting
291,721
void () { final String in = """ class Util { @SafeVarargs @Contract(pure=true) public static <T> T @NotNull [] ar(T @NotNull ... elements) { return elements; } }"""; final String what = "$RT$ ar($T$ $p$);"; final String replacement = "$RT$ ar($T$ $p$);"; assertEquals("keep method type parameters", in, replace(in, what, replacement, true)); }
testTypeParameterReplacement
291,722
Sdk () { return IdeaTestUtil.getMockJdk11(); }
getProjectJDK
291,723
void () { doTest("A.f", "[in code:f]"); }
testClassByQName
291,724
void () { doTest("exception should not be in plan", "class C {" + " void '_m{0,1} () throws OMGWTFBBQException {}" + "}", "[in code:C][in code:class|in code:enum|in code:interface|in code:record]"); doTest("throws should not be in plan", "class C {" + " String m() throws '_E{0,1} {" + " System.out.println();" + " return null;" + " }" + "}", "[in code:C][in code:class|in code:enum|in code:interface|in code:record][in code:m][in code:String][in code:println][in code:out]" + "[in code:System][in code:return][in code:null]"); }
testOptionalMethodWithThrowsClause
291,725
void () { doTest("extends should not be in plan", "class A extends '_B{0,0} {}", "[in code:A][in code:class|in code:enum|in code:interface|in code:record]"); doTest("implements should not be in plan", "class B implements '_I{0,0} {}", "[in code:B][in code:class|in code:enum|in code:interface|in code:record]"); }
testExtendsImplements
291,726
void () { doTest(":: in plan", "'_Q::x", "[in code:x][in code:::]"); doTest("-> in plan", "() -> {}", "[in code:->]"); }
testLambda
291,727
void () { doTest("regex should not be in plan", "'x:[regex( a+ )]", ""); doTest("regex should not be in plan 2", "'x:[ regex(a}
testRegExpChar
291,728
void () { doTest("class A {}", "[in code:A][in code:class|in code:enum|in code:interface|in code:record]"); doTest("interface I {}", "[in code:I][in code:interface]"); doTest("enum E {}", "[in code:E][in code:enum]"); doTest("record R() {}", "[in code:R][in code:record]"); }
testClasses
291,729
void () { doTest("classes outside search scope should also be added to descendants plan", "class '_A:*List {}", "[in code:AbstractImmutableList|in code:AbstractList|in code:AbstractSequentialList|in code:ArrayList|in code:AsList" + "|in code:COWSubList|in code:CheckedList|in code:CheckedRandomAccessList|in code:CopiesList|in code:CopyOnWriteArrayList" + "|in code:EmptyList|in code:LinkedList|in code:List|in code:List12|in code:ListN|in code:RandomAccessSubList" + "|in code:SingletonList|in code:Stack|in code:SubList|in code:SynchronizedList|in code:SynchronizedRandomAccessList" + "|in code:UnmodifiableList|in code:UnmodifiableRandomAccessList|in code:Vector]" + "[in code:class|in code:enum|in code:interface|in code:record]"); doTest("non-existing class name should be added to plan", "enum '_E:*Zyxwvuts {}", "[in code:Zyxwvuts][in code:enum]"); }
testDescendants
291,730
void () { doTest("new java.lang.RuntimeException('_x)", "[in code:new][in code:RuntimeException]"); doTest("new java.lang.reflect.InvocationTargetException('_x)", "[in code:new][in code:InvocationTargetException][in code:reflect][in code:lang][in code:java]"); }
testQualifiedReference
291,731
void () { doTest("try {" + " '_st*;" + "} catch ('_Type '_exception{0,0}) {" + " '_st2*;" + "}", "[in code:try]"); }
testTryWithoutCatch
291,732
void () { doTest("/* one/two (3|4|5) */", "[in comments:one][in comments:two][in comments:3][in comments:4][in comments:5]"); }
testComment
291,733
void () { doTest("@Modifier(\"packageLocal\") '_FieldType '_Field = '_Init?;", ""); }
testPackageLocal
291,734
void () { doTest("assert '_exp != null && true: \"'_exp is null\";", "[in literals:null][in literals:is][in code:assert][in code:null][in code:true]"); }
testLiterals
291,735
void () { doTest("\"asd fasdf\\\\n\"", "[in literals:fasdf][in literals:asd]"); }
testStringLiteral
291,736
void () { doTest("pkg.Tmp3.id()", "[in code:id]"); }
testFullyQualifiedMethodName
291,737
void () { doTest("ArrayUtil.toObjectArray($var$, $class$.class)", "[in code:toObjectArray][in code:class]"); }
testClassObjectAccessExpression
291,738
void () { doTest("java.util.List.of($x$)", "[in code:of][in code:List][in code:util][in code:java]"); }
testStaticCall
291,739
void (String query, String plan) { assertEquals(plan, getSearchPlan(query, JavaFileType.INSTANCE)); }
doTest
291,740
void (String message, String query, String plan) { assertEquals(message, plan, getSearchPlan(query, JavaFileType.INSTANCE)); }
doTest
291,741
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")); }
testSearchExpressions
291,742
void () { matchContext.getSink().matchingFinished(); }
run
291,743
void () { String s = System.getProperty(a); }
b
291,744
void () { // do stuff }
run
291,745
void () { // do stuff }
run
291,746
void (BigDecimal[] array) { // do nothing }
prorate
291,747
void (java.math.BigDecimal[] array) { // do nothing }
prorate2
291,748
void (BigDecimal bd) { // do nothing }
prorate
291,749
void (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]); }
main
291,750
void () { String s9 = """ class X {{ int a[] = new int[] { 1,2,3,4}; int b[] = { 2,3,4,5 }; Object[] c = new Object[] { "", null}; Object[] d = {null, null}; Object[] e = {}; Object[] f = new Object[]{}; String[] g = new String[]{}; String[] h = new String[]{new String()}; }}"""; assertEquals("Find new array expressions, but no array initializer expressions", 5, findMatchesCount(s9, "new '_ []{ '_* }")); assertEquals("Find new int array expressions, including array initializer expressions", 2, findMatchesCount(s9, "new int []{ '_* }")); assertEquals("Find new int array expressions, including array initializer expressions using variable ", 2, findMatchesCount(s9, "new '_a?:int [] { '_* }")); assertEquals("Find all new array expressions, including array initializers", 8, findMatchesCount(s9, "new '_? []{ '_* }")); assertEquals("Find new Object array expressions, including array initializer expressions", 4, findMatchesCount(s9, "new Object[] { '_* }")); assertEquals("Find only array initializer expressions", 3, findMatchesCount(s9, "new '_{0,0}[] { '_* }")); assertEquals("Find only int array initializer expressions", 1, findMatchesCount(s9, "new '_{0,0}:int [] { '_* }")); assertEquals("Try to find String array initializer expressions", 0, findMatchesCount(s9, "new '_{0,0}:String [] { '_* }")); assertEquals("Find empty array initializers", 2, findMatchesCount(s9, "new Object[] {}")); String arrays = """ class X {{ int[] a = new int[20]; byte[] b = new @Q byte[30]; }}"""; assertEquals("Improper array search", 1, findMatchesCount(arrays, "new int['_a]")); assertEquals("Find array of primitives", 2, findMatchesCount(arrays, "new '_X['_a]")); String multiDimensional = """ class X {{ String[] s1 = {}; String[] s2 = new String[]{}; String[][] s3 = new String[][]{}; }}"""; assertEquals("Find 2 dimensional array", 1, findMatchesCount(multiDimensional, "new String[][]{}")); assertEquals("Find 1 dimensional arrays", 2, findMatchesCount(multiDimensional, "new String[]{}")); assertEquals("Find empty 1 dimensional arrays", 2, findMatchesCount(multiDimensional, "new String[0]")); String singleDimensional = """ class X {{ String[] ss1 = new String[0]; String[][] ss2 = new String[0][]; }}"""; assertEquals("Find empty array", 1, findMatchesCount(singleDimensional, "new String[0]")); }
testNewArrayExpressions
291,751
void () { String s = """ class A { static String a = 1; static String s = "aaa"; static String s2; }"""; assertEquals("Literal", 2, findMatchesCount(s, "static String '_FieldName = '_Init?:[!regex( \".*\" )];")); assertEquals("Literal, 2", 1, findMatchesCount(s, "static String '_FieldName = '_Init:[!regex( \".*\" )];")); assertEquals("String literal", 1, findMatchesCount(s, "\"'String\"")); String source = """ @SuppressWarnings("test") class A { @SuppressWarnings({"other", "test"}) String field; }"""; assertEquals("String literal in annotation", 2, findMatchesCount(source, "\"test\"")); String s2 = """ class A { String a = "Alpha"; String b = "Bravo"; String c = "Charlie"; }"""; assertEquals("match literal contents", 1, findMatchesCount(s2, "\"'String:[regex( alpha )]\"")); assertEquals("negate match literal contents", 2, findMatchesCount(s2, "\"'String:[!regex( alpha )]\"")); assertEquals("match literal contents combined with other constraint", 1, findMatchesCount(s2, "\"'String:[regex( alpha ) && script( true )]\"")); String s3 = """ class A { int i = 0x20; char c = 'a'; char d = 'A'; char e = 'z'; }"""; assertEquals("match literal by value", 1, findMatchesCount(s3, "32")); assertEquals("match char with substitution", 3, findMatchesCount(s3, "\\''_x\\'")); assertEquals("string literal should not match char", 0, findMatchesCount(s3, "\"a\"")); String s4 = """ class X { String s = "\\n"; String t = " "; String u = " "; String v = ""; }"""; assertEquals("match empty string", 1, findMatchesCount(s4, "\"\"")); assertEquals("match space", 2, findMatchesCount(s4, "\" \"")); }
testLiteral
291,752
void () { String s1 = "class X {{ String[] argv; }}"; String s2 = "class X {{ String argv; }}"; assertEquals("Find array types", 0, findMatchesCount(s1, "String argv;")); assertEquals("Find array types, 2", 0, findMatchesCount(s2, "String[] argv;")); assertEquals("Find array types, 3", 0, findMatchesCount(s2, "'T[] argv;")); assertEquals("Find array types, 3", 1, findMatchesCount(s1, "'T:*Object [] argv;")); String s11 = """ class A { void main(String[] argv); void main(String argv[]); void main(String argv); }"""; assertEquals("Find array covariant types", 2, findMatchesCount(s11, "'_t:[regex( *Object\\[\\] ) ] '_t2;")); assertEquals("Find array covariant types, 2", 2, findMatchesCount(s11, "'_t:[regex( *Object ) ] '_t2 [];")); assertEquals("Find array covariant types, 3", 1, findMatchesCount(s11, "'_t:[regex( *Object ) ] '_t2;")); }
testCovariantArraySearch
291,753
void () { String source = """ class A { String ss[][]; int f()[] { return null; } }"""; String target = "String[][] '_s;"; assertEquals("should find multi-dimensional c-style array declarations", 1, findMatchesCount(source, target)); String target2 = "class '_A { int[] 'f(); }"; assertEquals("should find c-style method return type declarations", 1, findMatchesCount(source, target2)); String target3 = "class '_A { int 'f(); }"; assertEquals("should not find methods with array return types",0, findMatchesCount(source, target3)); String source2 = """ class A { void y(int... i) {} void y(String... ss) {} void y(boolean b) {} }"""; assertEquals("find ellipsis type 1", 1, findMatchesCount(source2, "String[] '_a;")); assertEquals("find ellipsis type 2", 1, findMatchesCount(source2, "int[] '_a;")); assertEquals("find ellipsis type 3", 1, findMatchesCount(source2, "class '_X { void '_m(int... '_a); }")); assertEquals("find ellipsis type 4", 2, findMatchesCount(source2, "'_T[] '_a;")); String source3 = """ class A { private int[] is; }"""; assertEquals("find primitive array 1", 1, findMatchesCount(source3, "int[] '_a;")); assertEquals("find primitive array 2", 1, findMatchesCount(source3, "'_T[] '_a;")); assertEquals("find primitive array 3", 1, findMatchesCount(source3, "'_T:[regex( int )][] '_a;")); assertEquals("find primitive array 4", 1, findMatchesCount(source3, "'_T:[regex( int\\[\\] )] '_a;")); }
testFindArrayDeclarations
291,754
void () { final String s = """ class X {{ debug("In action performed:"+event); project = (Project)event.getDataContext().getData(DataConstants.PROJECT); CodeEditorManager.getInstance(project).commitAllToPsiFile(); file = (PsiFile) event.getDataContext().getData("psi.File"); ((dialog==null)? (dialog = new SearchDialog()): dialog ).show(); }}"""; assertEquals("statement search", 1, findMatchesCount(s, "((dialog==null)? (dialog = new SearchDialog()): dialog).show();")); final String s5 = """ class X { { System.out.println(); while(false) { do { pattern = pattern.getNextSibling(); } while (pattern!=null && filterLexicalNodes(pattern)); } do { pattern = pattern.getNextSibling(); } while (pattern!=null && filterLexicalNodes(pattern)); { { do { pattern = pattern.getNextSibling(); } while (pattern!=null && filterLexicalNodes(pattern)); } } } }"""; assertEquals("several constructions match", 3, findMatchesCount(s5, """ do { pattern = pattern.getNextSibling(); } while (pattern!=null && filterLexicalNodes(pattern));""")); assertEquals("several constructions 2", 0, findMatchesCount(s5, """ do { pattern.getNextSibling(); } while (pattern!=null && filterLexicalNodes(pattern));""")); final String s7 = """ class X {{ if (true) throw new UnsupportedPatternException(statement.toString()); if (true) { throw new UnsupportedPatternException(statement.toString()); } }}"""; assertEquals("several constructions 3", 2, findMatchesCount(s7, """ if (true) { throw new UnsupportedPatternException(statement.toString()); }""")); assertEquals("several constructions 4", 2, findMatchesCount(s7, "if (true) throw new UnsupportedPatternException(statement.toString());")); final String s1000 = """ class X { { lastTest = "search for parameterized pattern"; matches = testMatcher.findMatches(s14_1,s15, options); if (matches.size()!=2 ) return false; lastTest = "search for parameterized pattern"; matches = testMatcher.findMatches(s14_1,s15, options); if (matches.size()!=2 ) return false; } }"""; final String s1001 = """ lastTest = '_Descr; matches = testMatcher.findMatches('_In,'_Pattern, options); if (matches.size()!='_Number ) return false;"""; assertEquals("several operators 5", 2, findMatchesCount(s1000, s1001)); final String s85 = "class X {{ int a; a=1; a=1; return a; }}"; assertEquals("two the same statements search", 1, findMatchesCount(s85, "'T; 'T;")); assertEquals("simple statement search (ignoring whitespace)", 4, findMatchesCount(s85, "'T ;")); final String s87 = "class X {{ getSomething(\"2\"); getSomething(\"1\"); a.call(); }}"; assertEquals("search for simple call", 1, findMatchesCount(s87, " '_Instance.'Call('_*); ")); assertEquals("search for simple call 2", 3, findMatchesCount(s87, " 'Call('_*); ")); assertEquals("search for simple call 3", 3, findMatchesCount(s87, " '_Instance?.'Call('_*); ")); assertEquals("search for simple call 4", 2, findMatchesCount(s87, " '_Instance{0,0}.'Call('_*); ")); String s10015 = "class X {{ DocumentListener[] listeners = getCachedListeners(); }}"; assertEquals("search for definition with init", 1, findMatchesCount(s10015, "'_Type 'Var = '_Call();")); String s10017 = "class X {{ a = b; b = c; a=a; c=c; }}"; assertEquals("search silly assignments", 2, findMatchesCount(s10017, "'_a = '_a;")); String s10019 = "class X {{ a.b(); a.b(null); a.b(null, 1); }}"; assertEquals("search parameter", 1, findMatchesCount(s10019, "a.b(null);")); String s1008 = "class X {{ int a, b, c, d; int a,b,c; int c,d; int e; }}"; assertEquals("search many declarations", 2, findMatchesCount(s1008, "int '_a{3,4};")); String s1 = "class X {{ super(1,1); call(1,1); call(2,2); }}"; assertEquals("search super", 1, findMatchesCount(s1, "super('_t*);")); String s10021 = """ class X {{ short a = 1; short b = 2; short c = a.b(); }}"""; assertEquals("search def init bug", 1, findMatchesCount(s10021, "short '_a = '_b.b();")); String s10023 = """ class X {{ abstract class A { public abstract short getType(); } A a; switch(a.getType()) { default: return 0; } switch(a.getType()) { case 1: { return 0; } } }}"""; assertEquals("finding switch", 2, findMatchesCount(s10023, "switch('_a:[exprtype( short )]) { '_statement*; }")); String s10025 = """ class X {{ A[] a; A b[]; A c; }}"""; assertEquals("array types in dcl", 2, findMatchesCount(s10025, "A[] 'a;")); assertEquals("array types in dcl 2", 2, findMatchesCount(s10025, "A 'a[];")); String s10027 = """ class X {{ try { a(); } catch(Exception ex) {} try { a(); } finally {} try { a(); } catch(Exception ex) {} finally {} }}"""; assertEquals("finally matching", 2, findMatchesCount(s10027, "try { a(); } finally {}\n")); String s10029 = "class X {{ for(String a:b) { System.out.println(a); }}}"; assertEquals("for each matching", 1, findMatchesCount(s10029, "for(String a:b) { '_a; }")); String s10031 = """ class X {{ try { a(); } catch(Exception ex) {} catch(Error error) { 1=1; } try { a(); } catch(Exception ex) {} }}"""; assertEquals("catch parameter matching", 3, findMatchesCount(s10031, "try { a(); } catch('_Type 'Arg) { '_Statements*; }\n")); String s10033 = """ class X {{ return x; return !x; return (x); return (x); return !(x); }}"""; assertEquals("Find statement with parenthesized expr",2,findMatchesCount(s10033, "return ('a);")); assertEquals("Find statement ignoring parentheses expr 2", 3, findMatchesCount(s10033, "return 'a:[regex( x )];")); String in = """ class X {{ if (true) { System.out.println(); } else { System.out.println(); } if (true) System.out.println(); }}"""; assertEquals("Find if statement with else", 2, findMatchesCount(in, "if ('_exp) { '_statement*; }")); assertEquals("Find if statement without else", 1, findMatchesCount(in, "if ('_exp) { '_statement*; } else { '_statement2{0,0}; }")); String in2 = """ /** * javadoc */ class A { /* comment */ void a() { System.out.println(); // comment } }"""; assertEquals("Should find statements and comments in statement context only", 2, findMatchesCount(in2, "'_statement;")); String in3 = """ class X {{ new Object().hashCode(); new Object().toString(); }}"""; assertEquals("Find typed expression statements", 1, findMatchesCount(in3, "'_expr:[exprtype( int )];")); String in4 = """ class X { void x() { System.out.println(); {} // comment switch (1) { case 1: {} } } }"""; assertEquals("block statement is a statement too", 1, findMatchesCount(in4, """ void '_x() { '_st*; }""")); String in5 = """ class X { void x() { while (true) {} } }"""; assertEquals("match block statement with statement variable", 1, findMatchesCount(in5, "while (true) '_st;")); }
testSearchStatements
291,755
void () { final String s43 = """ interface A extends B { int B = 1; } interface D { public final static double e = 1; } interface E { final static ind d = 2; } interface F { }"""; assertEquals("no modifier for interface vars", 3, findMatchesCount(s43, "interface '_ { '_T 'T2 = '_T3; } ")); final String s45 = """ class A extends B { private static final int B = 1; } class C extends D { int B = 1; } class E { }"""; assertEquals("different order of access modifiers", 1, findMatchesCount(s45, "class '_ { final static private '_T 'T2 = '_T3; } ")); assertEquals("no access modifier", 2, findMatchesCount(s45, "class '_ { '_T 'T2 = '_T3; } ")); final String s47 = "class C { java.lang.String t; } class B { BufferedString t2;} class A { String p;} "; assertEquals("type differs with package", 2, findMatchesCount(s47, "class '_ { String '_; }")); final String s49 = "class C { void a() throws java.lang.RuntimeException {} } class B { BufferedString t2;}"; assertEquals("reference could differ in package", 1, findMatchesCount(s49, "class '_ { '_ '_() throws RuntimeException; }")); String s51 = "class C extends java.awt.List {} class A extends java.util.List {} class B extends java.awt.List {} "; assertEquals("reference could differ in package 2", 2, findMatchesCount(s51, "class 'B extends '_C:java\\.awt\\.List {}")); final String s93 = """ class A { private int field; public void b() {} }"""; assertEquals("method access modifier", 0, findMatchesCount(s93, " class '_ {private void b() {}}")); assertEquals("method access modifier 2", 1, findMatchesCount(s93, " class '_ {public void b() {}}")); assertEquals("field access modifier", 0, findMatchesCount(s93, " class '_ {protected int field;}")); assertEquals("field access modifier 2", 1, findMatchesCount(s93, " class '_ {private int field;}")); final String s127 = "class a { void b() { new c() {}; } }"; assertEquals("class finds anonymous class", 2, findMatchesCount(s127, "class 't {}")); final String s129 = """ class a { public void run() {} } class a2 { public void run() { run(); } } class a3 { public void run() { run(); } } class a4 { public void run(); }"""; assertEquals("empty method finds empty method only", 1, findMatchesCount(s129, "class 'a { public void run() {} }")); assertEquals("nonempty method finds nonempty method", 2, findMatchesCount(s129, "class 'a { public void run() { '_statement; } }")); assertEquals("nonempty method finds nonempty method", 4, findMatchesCount(s129, "class 'a { public void run(); }")); final String s133 = """ class S { void cc() { new Runnable() { public void run() { f(); } private void f() { //To change body of created methods use File | Settings | File Templates. } }; new Runnable() { public void run() { f(); } private void g() { //To change body of created methods use File | Settings | File Templates. } }; new Runnable() { public void run() { f(); } }; } private void f() { //To change body of created methods use File | Settings | File Templates. } }"""; final String s134 = """ new Runnable() { public void run() { '_f (); } private void '_f (); }"""; assertEquals( "complex expr matching", 1, findMatchesCount(s133,s134) ); final String s135 = """ abstract class My { abstract void f(); } abstract class My2 { abstract void f(); void fg() {} }"""; final String s136 = """ class 'm { void f(); '_type '_method{0,0} ('_paramtype '_paramname* ); }"""; assertEquals("reject method with 0 max occurence", 1, findMatchesCount(s135,s136)); final String s137 = """ abstract class My { int a; } abstract class My2 { Project b; } abstract class My3 { Class clazz; Project b = null; } abstract class My { int a = 1; } """; assertEquals("reject field with 0 max occurence", 2, findMatchesCount(s137, "class 'm { Project '_f{0,0} = '_t?; }")); final String s139 = "class My { boolean equals(Object o); int hashCode(); }"; final String s140 = "class 'A { boolean equals(Object '_o ); int '_hashCode{0,0}:hashCode (); }"; assertEquals("reject method with constraint", 0, findMatchesCount(s139,s140)); final String s139_2 = "class My { boolean equals(Object o); }"; assertEquals("reject field with 0 max occurence", 1, findMatchesCount(s139_2,s140)); final String s141 = """ class A { static { a = 10; } } class B { { a = 10; } } class C { { a = 10; } }"""; assertEquals("static block search", 1, findMatchesCount(s141, "class '_ { static { a = 10; } } ")); final String in = "class D<T> {}\n" + "class T {}"; assertEquals("search for class should not find type parameters", 2, findMatchesCount(in, "class 'A {}")); }
testSearchClass
291,756
void () {}
b
291,757
void () { f(); }
run
291,758
void () { //To change body of created methods use File | Settings | File Templates. }
f
291,759
void () { f(); }
run
291,760
void () { //To change body of created methods use File | Settings | File Templates. }
g
291,761
void () { f(); }
run
291,762
void () { //To change body of created methods use File | Settings | File Templates. }
f
291,763
void () { '_f (); }
run
291,764
void () { final String s143 = """ class A { A() {} }; class B { B(int a) {} }; class C { C() {} C(int a) {} }; class D { void method() {} } class E {}"""; assertEquals("parameterless constructor search", 3, findMatchesCount(s143, "class '_a { '_d{0,0}:[ script( \"__context__.constructor\" ) ]('_b '_c+); }")); assertEquals("parameterless constructor search 2", 2, findMatchesCount(s143, "'_Constructor() { '_st*; }")); assertEquals("method & constructor search", 5, findMatchesCount(s143, "'_T? '_identifier('_PT '_p*);")); }
testParameterlessConstructorSearch
291,765
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 {}")); }
testScriptSearch
291,766
void () { try { findMatchesCount("", "'_b:[script( \"^^^\" )]"); fail("Validation does not work"); } catch (MalformedPatternException ignored) {} }
testCheckScriptValidation
291,767
void () { String s1 = """ import java.util.*; class A { void b() { Map map = new HashMap(); class AppPreferences {} String key = "key"; AppPreferences value = new AppPreferences(); map.put(key, value ); map.put(value, value ); map.put("key", value ); map.put("key", new AppPreferences()); } }"""; String s2 = "'_map:[exprtype( *java\\.util\\.Map )].put('_key:[ exprtype( *Object ) ], '_value:[ exprtype( *AppPreferences ) ]);"; assertEquals("expr type with object", 4, findMatchesCount(s1, s2)); }
testExprTypeWithObject
291,768
void () { String in = """ class A implements Cloneable {} class B implements Serializable {} class C implements Cloneable,Serializable {} class C2 implements Serializable,Cloneable {} class E extends B implements Cloneable {} class F extends A implements Serializable {} class D extends C {}"""; assertEquals("search interface within hierarchy", 5, findMatchesCount(in, "class 'A implements '_B:*Serializable , '_C:*Cloneable {}")); }
testInterfaceImplementationsSearch
291,769
void () { final String s89 = "class X {{ a = 1; b = 2; c=3; }}"; assertEquals("backtracking greedy regexp", 1, findMatchesCount(s89, "{ '_T*; '_T2*; }")); assertEquals("backtracking greedy regexp 2", 1, findMatchesCount(s89, " { '_T*; '_T2*; '_T3+; } ")); assertEquals("backtracking greedy regexp 3", 0, findMatchesCount(s89, " { '_T+; '_T2+; '_T3+; '_T4+; } ")); assertEquals("counted regexp (with back tracking)", 1, findMatchesCount(s89, " { '_T{1,3}; '_T2{2,}; } ")); assertEquals("nongreedy regexp (counted, with back tracking)", 1, findMatchesCount(s89, " { '_T{1,}?; '_T2*?; '_T3+?; } ")); assertEquals("nongreedy regexp (counted, with back tracking) 2", 0, findMatchesCount(s89, " { '_T{1,}?; '_T2{1,2}?; '_T3+?; '_T4+?; } ")); String s1000 = """ class A { void _() {} void a(String in, String pattern) {} }"""; String s1001 = """ class '_Class { '_ReturnType 'MethodName ('_ParameterType '_Parameter* ); }"""; assertEquals("handling of no match", 2, findMatchesCount(s1000,s1001)); }
testSearchBacktracking
291,770
void () { final String s131 = "class X {{ a.b(); c.d = 1; }}"; assertEquals("symbol match", 2, findMatchesCount(s131, "'T:b|d")); options.setCaseSensitiveMatch(true); final String s129 = "class X {{ A a = new A(); }}"; assertEquals("case sensitive match", 2, findMatchesCount(s129, "'Sym:A")); final String s133 = "class C { int a; int A() { a = 1; } void c(int a) { a = 2; } }"; final String s134 = "a"; assertEquals("find sym finds declaration", 4, findMatchesCount(s133, s134)); final String s133_2 = "class C { int a() {} int A() { a(1); }}"; assertEquals("find sym finds declaration", 2, findMatchesCount(s133_2, s134)); final String source = """ class A { static A a() {}; void m() { A a = A.a(); } }"""; assertEquals("No duplicate results", 4, findMatchesCount(source, "A")); }
testSearchSymbol
291,771
A () {}
a
291,772
void () { final String s81 = """ class Pair<First,Second> { <C,F> void a(B<C> b, D<F> e) throws C { P<Q> r = (S<T>)null; Q q = null; if (r instanceof S<T>) {} } } class Q { void b() {} }"""; assertEquals("parameterized class match", 2, findMatchesCount(s81, "class '_<'T> {}")); assertEquals("parameterized instanceof match", 1, findMatchesCount(s81, "'_Expr instanceof '_Type<'_Parameter+>")); assertEquals("parameterized cast match", 1, findMatchesCount(s81, "( '_Type<'_Parameter+> ) '_Expr")); assertEquals("parameterized symbol without variables matching", 2, findMatchesCount(s81, "S<T>")); assertEquals("parameterized definition match", 3, findMatchesCount(s81, "'_Type<'_Parameter+> 'a = '_Init?;")); assertEquals("parameterized method match", 1, findMatchesCount(s81, "class '_ { <'_+> '_Type 'Method('_ '_*); }")); final String s81_2 = "class Double<T> {} class T {} class Single<First extends A & B> {}"; assertEquals("parameterized constraint match", 2, findMatchesCount(s81_2, "class '_<'_+ extends 'res> {}")); String s82_7 = "'Type"; assertEquals("symbol matches parameterization", 29, findMatchesCount(s81, s82_7)); assertEquals("symbol matches parameterization 2", 7, findMatchesCount(s81_2, s82_7)); String s81_3 = """ class A { public static <T> Collection<T> unmodifiableCollection(int c) { return new d<T>(c); } static class d<E> implements Collection<E>, Serializable { public <T> T[] toArray(T[] a) {return c.toArray(a);} } }"""; assertEquals("typed symbol symbol", 2, findMatchesCount(s81_3, "class '_ { <'_+> '_Type 'Method('_ '_*); }")); String s81_4= """ class A<B> { static <C> void c(D<E> f) throws R<S> { if ( f instanceof G<H>) { ((I<G<K>>)l).a(); throw new P<Q>(); } } } class C { void d(E f) throws Q { if (g instanceof H) { a.c(); b.d(new A() {}); throw new Exception(((I)k)); } } }"""; assertEquals("typed symbol", 8, findMatchesCount(s81_4, "'T<'_Subst+>")); String s81_5 = "class A { HashMap<String, Integer> variable = new HashMap<String, Integer>(\"aaa\");}"; String s82_9 = "'_Type<'_GType, '_GType2> '_instance = new '_Type<'_GType, '_GType2>('_Param);"; assertEquals("generic vars in new", 1, findMatchesCount(s81_5,s82_9)); assertEquals("no exception on searching for diamond operator", 0, findMatchesCount(s81_5, "new 'Type<>('_Param)")); assertEquals("order of parameters matters", 0, findMatchesCount(s81_5, "HashMap<Integer, String>")); assertEquals("order of parameters matters 2", 2, findMatchesCount(s81_5, "HashMap<String, Integer>")); String source1 = "class Comparator<T> { private Comparator<String> c; private Comparator d; private Comparator e; }"; assertEquals("qualified type should not match 1", 0, findMatchesCount(source1, "java.util.Comparator 'a;")); assertEquals("qualified type should not match 2", 0, findMatchesCount(source1, "java.util.Comparator<String> 'a;")); assertEquals("unparameterized type query should match", 3, findMatchesCount(source1, "Comparator 'a;")); assertEquals("parameterized type query should only match parameterized", 1, findMatchesCount(source1, "Comparator<'_a> 'b;")); assertEquals("should find unparameterized only", 2, findMatchesCount(source1, "Comparator<'_a{0,0}> 'b;")); String source2 = "class A<@Q T> {}\n" + "class B<T> {}"; assertEquals("find annotated type parameter", 1, findMatchesCount(source2, "class '_A<@Q '_T> {}")); // @todo typed vars constrains (super), // @todo generic method invocation //String s83 = "class A {} List<A> a; List b;"; //String s84 = "'a:List 'c;"; //String s84_2 = "'a:List\\<'_\\> 'c;"; //String s84_3 = "'a:List(?>\\<'_\\>) 'c;"; // //assertEquals( // "finding list", // findMatchesCount(s83,s84), // 2 //); // //assertEquals( // "finding list 2", // findMatchesCount(s83,s84_2), // 1 //); // //assertEquals( // "finding list 3", // findMatchesCount(s83,s84_3), // 1 //); }
testSearchGenerics
291,773
void () { final String s15 = "'T;"; final String s1 = "class X {{ if (true) { aaa(var); }}}"; assertEquals("search for parameterized pattern", 3, findMatchesCount(s1, s15)); final String s2 = "class X {{ if (true) { aaa(var); bbb(var2); }\n if(1==1) { system.out.println('o'); }}}"; assertEquals("search for parameterized pattern 2", 7, findMatchesCount(s2, s15)); options.setRecursiveSearch(false); assertEquals("search for parameterized pattern-non-recursive", 1, findMatchesCount(s1, s15)); assertEquals("search for parameterized pattern 2-non-recursive", 2, findMatchesCount(s2, s15)); final String s23 = "class X {{ a[i] = 1; b[a[i]] = f(); if (a[i]==1) return b[c[i]]; }}"; final String s24_2 = "'T['_T2:.*i.* ]"; // typed vars with arrays assertEquals("typed pattern with array 2-non-recursive", 4, findMatchesCount(s23, s24_2)); options.setRecursiveSearch(true); assertEquals("search for parameterized pattern 3", 1, findMatchesCount(s2, "if('_T) { '_T2; }")); final String s17 = """ class X {{ token.getText().equals(token2.getText()); token.getText().equals(token2.getText2()); token.a.equals(token2.b); token.a.equals(token2.a); }}"""; assertEquals("search for parameterized pattern in field selection", 1, findMatchesCount(s17, "'_T1.'_T2.equals('_T3.'_T2);")); assertEquals("search for parameterized pattern with method call", 1, findMatchesCount(s17, "'_T1.'_T2().equals('_T3.'_T2());")); assertEquals("search for parameterized pattern with method call ep.2", 4, findMatchesCount(s17, "'_T1.'_T2")); final String s19 = "class X {{ Aaa a = (Aaa)b; Aaa c = (Bbb)d; }}"; assertEquals("search for same var constraint", 1, findMatchesCount(s19, "'_T1 'T2 = ('_T1)'_T3;")); assertEquals("search for same var constraint for semi anonymous typed vars", 1, findMatchesCount(s19, "'_T1 '_T2 = ('_T1)'_T3;")); final String s22 = "class X {{ Aaa a = (Aaa)b; Bbb c = (Bbb)d; }}"; assertEquals("search for typed var constraint", 1, findMatchesCount(s22, "'_T1:Aa* 'T2 = ('_T1)'_T3;")); try { findMatchesCount(s22, "'_T1:A* 'T2 = ( '_T1:A+ )'_T3;"); fail("search for noncompatible typed var constraint"); } catch(MalformedPatternException ignored) {} assertEquals("search for same typed var constraint", 1, findMatchesCount(s22, "'_T1:Aa* 'T2 = ( '_T1 )'_T3;")); final String s65 = "class X {{ if (A instanceof B) {} else if (B instanceof C) {}}}"; assertEquals("typed instanceof", 1, findMatchesCount(s65, " '_T instanceof '_T2:B")); try { findMatchesCount(s65, "'_T instanceof"); fail("warn on incomplete instanceof"); } catch (MalformedPatternException e) { assertEquals("Type expected", e.getMessage()); } assertEquals("typed pattern with array", 2, findMatchesCount(s23, "'T['_T2:.*i.* ] = '_T3;")); assertEquals("typed pattern with array 2", 6, findMatchesCount(s23, s24_2)); final String s25 = """ class MatcherImpl { void doMatch(int a) {} } class Matcher { abstract void doMatch(int a);} class Matcher2Impl { void doMatch(int a, int b) {} }"""; assertEquals("typed pattern in class name, method name, return type, parameter type and name", 1, findMatchesCount(s25, "class 'T:.*Impl { '_T2 '_T3('_T4 '_T5) {\n\n} } ")); final String s27 = "class A {} interface B {}"; assertEquals("finding interface", 1, findMatchesCount(s27, "interface 'T {}")); final String s29 = "class A { void B(int C) {} } class D { void E(double e) {} }"; assertEquals("anonymous typed vars", 1, findMatchesCount(s29, "class '_ { void '_('_:int '_); } ")); final String s31 = "class A extends B { } class D extends B { } class C extends C {}"; assertEquals("finding class descendants", 2, findMatchesCount(s31, "class '_ extends B { } ")); final String s33 = "class A implements B,C { } class D implements B,D { } class C2 implements C,B {}"; assertEquals("interface implementation", 2, findMatchesCount(s33, "class '_ implements B,C { } ")); final String s35 = "class A { int b; double c; void d() {} int e() {} } \n" + "class A2 { int b; void d() {} }"; assertEquals("different order of fields and methods", 1, findMatchesCount(s35, "class '_ { double '_; int '_; int '_() {} void '_() {} } ")); final String s37 = "class A { void d() throws B,C,D {} } class A2 { void d() throws B,C {} }"; assertEquals("different order in throws", 1, findMatchesCount(s37, "class 'T { '_ '_() throws D,C {} } ")); final String s39 = "class A extends B { } class A2 { }"; assertEquals("match of class without extends to class with it", 2, findMatchesCount(s39, "class 'T { } ")); final String s41 = "class A extends B { int a = 1; } class B { int[] c= new int[2]; } \n" + "class D { double e; } class E { int d; } "; assertEquals("match of class without extends to class with it, ep. 2", 2, findMatchesCount(s41, "class '_ { '_T '_T2 = '_T3; } ")); assertEquals("match of class without extends to class with it, ep 3", 4, findMatchesCount(s41, "class '_ { '_T '_T2; } ")); assertEquals("match class with fields without initializers", 2, findMatchesCount(s41, "class '_ { '_T '_T2 = '_T3{0,0}; } ")); final String s51 = "class C extends B { } class B extends A { } class E {}"; assertEquals("typed reference element", 2, findMatchesCount(s51, "class '_ extends '_ { }")); final String s59 = "interface A { void B(); }"; assertEquals("empty name for typed var", 1, findMatchesCount(s59, "interface '_ { void '_(); }")); final String s63 = " class A { A() {} } class B { public void run() {} }"; final String s64 = " class 'T { public void '_T2:run () {} }"; assertEquals("comparing method with constructor", 1, findMatchesCount(s63, s64)); final String s63_2 = """ class A { A() {} class B { public void run() {} } class D { public void run() {} } } class C {}"""; assertEquals("finding nested class", 2, findMatchesCount(s63_2, s64)); assertEquals("find nested class by special pattern", 2, findMatchesCount(s63_2, "class '_ { class 'T { public void '_T2:run () {} } }")); final String s61 = "class X {{ a=b; c=d; return; } { e=f; } {}}"; assertEquals("+ regexp for typed var", 4, findMatchesCount(s61, "{ 'T; }")); assertEquals("? regexp for typed var", 2, findMatchesCount(s61, "{ '_T?; }")); assertEquals("* regexp for anonymous typed var", 3, findMatchesCount(s61, "{ '_*; }")); assertEquals("+ regexp for anonymous typed var", 2, findMatchesCount(s61, "{ '_+; }")); assertEquals("? regexp for anonymous typed var", 2, findMatchesCount(s61, "{ '_?; }")); final String s67 = "class X {{ buf.append((VirtualFile)a); }}"; assertEquals("cast in method arguments", 1, findMatchesCount(s67, " (VirtualFile)'T")); final String s69 = """ class X {{ System.getProperties(); System.out.println(); java.lang.System.out.println(); some.other.System.out.println(); }}"""; assertEquals("searching for static field in static call", 2, findMatchesCount(s69, " System.out ")); assertEquals("searching for static field in static call, 2", 2, findMatchesCount(s69, " java.lang.System.out ")); final String s71 = """ class A { class D { D() { c(); } } void a() { c(); new MouseListener() { void b() { c(); } }; } }"""; assertEquals("statement inside anonymous class", 3, findMatchesCount(s71, " c(); ")); final String s91 = """ class a { void b() { int c; c = 1; b(); a a1; } }"""; assertEquals("clever regexp match", 2, findMatchesCount(s91, "'T:a")); assertEquals("clever regexp match 2", 2, findMatchesCount(s91, "'T:b")); assertEquals("clever regexp match 3", 2, findMatchesCount(s91, "'T:c")); }
testSearchSubstitutions
291,774
void () { final String s = """ class A { void m() { /** tool */ class Local {} } }"""; assertEquals("dangling javadoc followed by a local class", 1, findMatchesCount(s, "{\n/** tool */\nclass 'A {}\n}")); assertEquals("class with javadoc shouldn't find dangling javadoc and local class", 0, findMatchesCount(s, "/** tool */\nclass 'A {}")); final String s57 = """ /** @author Maxim */ class C { private int value; } class D { /** @serial */ private int value; private int value2; /** @since 1.4 */ void a() {} } class F { /** @since 1.4 */ void a() {} /** @serial */ private int value2; } class G { /** @param a*/ void a() {} }"""; assertEquals("java doc comment in class in file", 1, findMatchesCount(s57, "/** @'T '_T2 */ class '_ { }")); assertEquals("javadoc comment for field", 2, findMatchesCount(s57, "class '_ { /** @serial '_* */ '_ '_; }")); assertEquals("javadoc comment for method", 2, findMatchesCount(s57, "class '_ { /** @'T 1.4 */ '_ '_() {} }")); assertEquals("javadoc comment for method 2", 2, findMatchesCount(s57, "/** @'T 1.4 */ '_t '_m();")); assertEquals("just javadoc comment search", 4, findMatchesCount(s57, "/** @'T '_T2 */")); assertEquals("optional tag value match", 6, findMatchesCount(s57, "/** @'T '_T2? */")); assertEquals("no infinite loop on javadoc matching", 1, findMatchesCount(s57, "/** 'Text */ class '_ { }")); final String s83 = """ /** * @hibernate.class * table="CATS" */ public class Cat { private Long id; // identifier private Date birthdate; /** * @hibernate.id * generator-class="native" * column="CAT_ID" */ public Long getId() { return id; } private void setId(Long id) { this.id=id; } /** * @hibernate.property * column="BIRTH_DATE" */ public Date getBirthdate() { return birthdate; } void setBirthdate(Date date) { birthdate = date; } /** * @hibernate.property * column="SEX" * not-null="true" * update="false" */ public char getSex() { return sex; } void setSex(char sex) { this.sex=sex; } }"""; assertEquals("XDoclet metadata", 2, findMatchesCount(s83, """ /** * @hibernate.property * 'Property */ """)); assertEquals("XDoclet metadata 2", 1, findMatchesCount(s83, """ /** * @hibernate.property * update="fa.se" */ """)); final String s75 = """ /** @class aClass @author the author */ class A {} /** */ class B {} /** @class aClass */ class C {}"""; assertEquals("multiple tags match +", 2, findMatchesCount(s75, " /** @'_tag+ '_value+ */")); assertEquals("multiple tags match *", 3, findMatchesCount(s75, " /** @'_tag* '_value* */")); assertEquals("multiple tags match ?", 3, findMatchesCount(s75, " /** @'_tag? '_value? */ class 't {}")); final String source = """ class outer { /** bla */ class One {} class Two {} }"""; final String pattern = "class '_A { /** '_Text */class 'B {}}"; assertEquals("match inner class with javadoc", 1, findMatchesCount(source, pattern)); final String in = """ class X { /** * cool */ void x1() {} /** * cool * @since 1.1 */ void x2() {} /** * uncool * @since 2.1 */ void x3() {} }"""; assertEquals("match text & tag", 1, findMatchesCount(in, """ /** '_c:cool * @since '_x */""")); assertEquals("match text & tag 2", 1, findMatchesCount(in, """ /** * cool * @since '_x */""")); assertEquals("match tag, ignore text", 1, findMatchesCount(in, "/** '_c\n" + " * @since 2.1 */")); final String in2 = """ class X { /** * */ void empty() {} /** * @since 1.1 */ void x() {} /** * text */ void y() {} }"""; assertEquals("match empty javadoc", 1, findMatchesCount(in2, """ /** * @'_tag{0,0} */""")); }
testSearchJavaDoc
291,775
Long () { return id; }
getId
291,776
void (Long id) { this.id=id; }
setId
291,777
Date () { return birthdate; }
getBirthdate
291,778
char () { return sex; }
getSex
291,779
void () { String s133 = """ class String1 implements java.io.Serializable { private static final long serialVersionUID = -6849794470754667710L; private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; } class StringBuilder1 implements java.io.Serializable { private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); } static final long serialVersionUID = 4383685877147921099L; }"""; String s134 = """ class '_ implements '_:*Serializable { static final long 'VersionField:serialVersionUID = '_?; private static final ObjectStreamField[] '_?:serialPersistentFields = '_?; private void '_SerializationWriteHandler?:writeObject (ObjectOutputStream s) throws IOException; private void '_SerializationReadHandler?:readObject (ObjectInputStream s) throws IOException, ClassNotFoundException; Object '_SpecialSerializationReadHandler?:readResolve () throws ObjectStreamException; Object '_SpecialSerializationWriteHandler?:writeReplace () throws ObjectStreamException; }"""; assertEquals("serialization match", 2, findMatchesCount(s133, s134)); String s135 = """ class SimpleStudentEventActionImpl extends Action { public ActionForward execute(ActionMapping mapping, ActionForm _form, HttpServletRequest _request, HttpServletResponse _response) throws Exception {} } public class DoEnrollStudent extends SimpleStudentEventActionImpl { } public class DoCancelStudent extends SimpleStudentEventActionImpl { }"""; String s136 = """ public class 'StrutsActionClass extends '_:*Action { public ActionForward '_AnActionMethod:*execute (ActionMapping '_, ActionForm '_, HttpServletRequest '_, HttpServletResponse '_); }"""; assertEquals("Struts actions", 2, findMatchesCount(s135, s136)); final String s123 = """ class NodeFilter {} public class MethodFilter extends NodeFilter { private MethodFilter() {} public static NodeFilter getInstance() { if (instance==null) instance = new MethodFilter(); return instance; } private static NodeFilter instance; }"""; final String s124 = """ class 'Class { private 'Class('_Type '_param*) { '_st*; } private static '_Class2 '_Instance; static '_Class2 '_GetInstance() { '_st2*; return '_Instance; } }"""; assertEquals("singleton search", 1, findMatchesCount(s123,s124)); String s1111 = """ class X {{ if (true) { a=1; b=1; } else { a=1; } if(true) { a=1; } else { a=1; b=1; } if(true) { a=1; b=2; } else { a = 1; b=2; } }}"""; assertEquals("same multiple name pattern", 1, findMatchesCount(s1111, "if (true) { '_a{1,2}; } else { '_a; }")); }
testNamedPatterns
291,780
NodeFilter () { if (instance==null) instance = new MethodFilter(); return instance; }
getInstance
291,781
void () { final String s105 = "class B {} class A extends B { } class C {} class D extends C {}"; assertEquals("extends match", 1, findMatchesCount(s105, "class '_ extends '_:[ref( \"class B {}\" )] {}")); final String s107 = """ interface IA {} interface IB extends IA {} interface IC extends IB {} interface ID extends IC {} class A implements IA {} class B extends A {} class C extends B implements IC {} class D extends C {}"""; assertEquals("extends navigation match", 2, findMatchesCount(s107, "class '_ extends 'Type:+A {}")); assertEquals("extends navigation match 2", 3, findMatchesCount(s107, "interface '_ extends 'Type:*IA {}")); assertEquals("extends navigation match 3", 2, findMatchesCount(s107, "interface '_ extends 'Type:+IA {}")); assertEquals("implements navigation match", 3, findMatchesCount(s107, "class '_ implements 'Type:+IA {}")); assertEquals("without hierarchy finds only direct implements", 1, findMatchesCount(s107, "class '_ implements '_T:IA {}")); final String s109 = """ interface I {} interface I2 extends I {} class A implements I2 {} class B extends A {} class C extends B {} class D { void e() { D d; C c; B b; A a; } }"""; assertEquals("extends navigation match in definition", 3, findMatchesCount(s109, "'_:*A '_;")); assertEquals("implements navigation match in definition 2", 3, findMatchesCount(s109, "'_:*I '_;")); assertEquals("implements navigation match in definition 2 with nested conditions", 3, findMatchesCount(s109, "'_:[ref( \"class '_A:*[regex( I )] {}\" )] '_;")); try { findMatchesCount(s109, "'_:*[regex( I ) && ref2('T)] '_;"); fail("implements navigation match in definition 2 with nested conditions - incorrect cond"); } catch (MalformedPatternException ignored) {} final String s111 = "interface E {} class A implements E {} class B extends A { int f = 0; } class C extends B {} class D { void e() { C c; B b; A a;} }"; final String s112 = "'_"; assertEquals("symbol match", 17, findMatchesCount(s111,s112)); final String s115 = "class B {} public class C {}"; assertEquals("public modifier for class", 1, findMatchesCount(s115, "public class '_ {}")); final String s117 = """ class A { int b; void c() { int e; b=1; this.b=1; e=5; System.out.println(e); System.out.println(b); System.out.println(this.b); } }"""; assertEquals("fields of class", 4, findMatchesCount(s117, "this.'Field")); final String s119 = "class X {{ try { a.b(); } catch(IOException e) { c(); } catch(Exception ex) { d(); }}}"; assertEquals("catches loose matching", 1, findMatchesCount(s119, "try { '_; } catch('_ '_) { '_; }")); assertEquals("catches loose matching 2", 0, findMatchesCount(s119, "try { '_; } catch(Throwable '_) { '_; }")); final String s121 = "class A { private int a; class Inner {} } \n" + "class B extends A { private int a; class Inner2 {} }"; assertEquals("hierarchical matching", 2, findMatchesCount(s121, "class '_ { int '_:* ; }")); assertEquals("hierarchical matching 2", 4, findMatchesCount(s121, "class '_ { int '_:+hashCode (); }")); assertEquals("hierarchical matching 3", 2, findMatchesCount(s121, "class '_ { class '_:* {} }")); }
testHierarchy
291,782
void () { String s1 = """ class X {{ // This is some comment /* This is another comment*/ // Some garbage /** And now third comment*/ /** Some garbage*/ }}"""; assertEquals("Comment matching", 3, findMatchesCount(s1, "// 'Comment:[regex( .*(?:comment).* )]")); assertEquals("Comment matching, 2", 3, findMatchesCount(s1, "/* 'Comment:[regex( .*(?:comment).* )] */")); assertEquals("Java doc matching", 1, findMatchesCount(s1, "/** 'Comment:[regex( .*(?:comment).* )] */")); assertEquals("Comment matching with negate", 2, findMatchesCount(s1, "// 'not_comment:[!regex( .*(?:comment).* )]")); assertEquals("Multi line", 1, findMatchesCount(s1, "//'_comment:[regex( .*another.* )]")); assertEquals("Multi line negated", 4, findMatchesCount(s1, "//'_comment:[!regex( .*another.* )]")); String s4 = "class X {{ java.util.Arrays.asList(\"'test\", \"another test\", \"garbage\"); }}"; assertEquals("Literal content", 2, findMatchesCount(s4, "\"'test:[regex( .*test.* )]\"")); assertEquals("Literal content with escaping", 1, findMatchesCount(s4, "\"''test\"")); String s7 = "class X {{ String s = \"aaa\"; }}"; assertEquals("Simple literal content", 1, findMatchesCount(s7, "\"'test:[regex( aaa )]\"")); String s9 = "class X {{ java.util.Arrays.asList(\" aaa \",\" bbb \",\" ccc ccc aaa\"); }}"; assertEquals("Whole word literal content with alternations", 2, findMatchesCount(s9, "\"'test:[regexw( aaa|ccc )]\"")); assertEquals("Whole word literal content", 1, findMatchesCount(s9, "\"'test:[regexw( bbb )]\"")); String s12 = """ class X {{ assert agentInfo != null : "agentInfo is null"; assert addresses != null : "addresses is null"; }}"""; assertEquals("reference to substitution in comment", 2, findMatchesCount(s12, "assert '_exp != null : \"'_exp is null\";")); String s14 = """ class X {{ java.util.Arrays.asList( "(some text with special chars)", " some", "(some)" ); }}"""; assertEquals("meta char in literal", 2, findMatchesCount(s14, "\"('a:[regexw( some )])\"")); String s16 = """ /** * Created by IntelliJ IDEA. * User: cdr * Date: Nov 15, 2005 * Time: 4:23:29 PM * To change this template use File | Settings | File Templates. */ public class Y { }"""; String s17 = """ /** * Created by IntelliJ IDEA. * User: '_USER * Date: '_DATE * Time: '_TIME * To change this template use File | Settings | File Templates. */ class 'c { }"""; assertEquals("complete comment match", 1, findMatchesCount(s16, s17)); assertEquals("complete comment match case insensitive", 1, findMatchesCount(s16, s17.toLowerCase())); String s18 = """ public class A { private void f(int i) { int g=0; //sss } }"""; String s19 = """ class '_c { '_type '_f('_t '_p){ '_s; // sss } }"""; assertEquals("statement match with comment", 1, findMatchesCount(s18,s19)); String s20 = """ class X { /* H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ */ }"""; assertEquals("match comments ignoring accents and differences in whitespace", 1, findMatchesCount(s20, "/*he\ncomes*/")); }
testSearchInCommentsAndLiterals
291,783
void (int i) { int g=0; //sss }
f
291,784
void () { final String s73 = " class A { int A; static int B=5; public abstract void a(int c); void q() { ind d=7; } }"; assertEquals("optional init match in definition", 4, findMatchesCount(s73, " '_Type 'Var = '_Init?; ")); final String s77 = """ class X {{ new ActionListener() {}; }}"""; assertEquals("null match", 0, findMatchesCount(s77, " class 'T:.*aaa {} ")); final String s79 = " class A { static { int c; } void a() { int b; b=1; }} "; assertEquals("body of method by block search", 2, findMatchesCount(s79, " { '_T 'T3 = '_T2?; '_*; } ")); final String s95 = """ class Clazz { private int field; private int field2; private int fielxd2; }"""; assertEquals("first matches, next not", 2, findMatchesCount(s95, " class '_ {private int 'T:field.* ;}")); final String s97 = """ class A { int c; int d; void b(){} void x(){ C d; } } class C { C() { A a; A z; z.b(); a.x(); z.c=1; a.d=2; } }"""; assertEquals("method predicate match", 1, findMatchesCount(s97, "'_.'_:[ref( \"void b(){}\" )] ()")); assertEquals("field predicate match", 1, findMatchesCount(s97, "'_.'_:[ref( \"int c;\" )]")); assertEquals("dcl predicate match", 1, findMatchesCount(s97, "'_:[ref( \"A a;\" )].'_ ();")); final String s99 = "class X {{ char s = '\\u1111'; char s1 = '\\n'; }}"; assertEquals("char constants in pattern", 1, findMatchesCount(s99, " char 'var = '\\u1111'; ")); assertEquals("char constants in pattern 2", 1, findMatchesCount(s99, " char 'var = '\\n'; ")); assertEquals("class predicate match (from definition)", 3, findMatchesCount(s97, "'_:[ref( \"class '_A {}\" )] '_;")); String s107 = """ class A { /* */ void a() { } /* */ int b = 1; /* * */ class C {} }"""; String s108 = """ /* * */\ """; assertEquals("finding comments without typed var", 1, findMatchesCount(s107,s108)); String s109 = """ class X {{ class A { void b(); int b(int c); char d(char e); } A a; a.b(1); a.b(2); a.b(); a.d('e'); a.d('f'); a.d('g'); }}"""; assertEquals("caring about method return type", 2, findMatchesCount(s109, "'_a.'_b:[exprtype( int ) ]('_c*);")); String s111 = "class A { void getManager() { getManager(); } };\n" + "class B { void getManager() { getManager(); getManager(); } };"; assertEquals("caring about missing qualifier type", 2, findMatchesCount(s111, "'_Instance?:[exprtype( B )].getManager()")); assertEquals("static query should not match instance method", 0, findMatchesCount(s111, "'_Instance?:[regex( B )].getManager()")); assertEquals("static query should not match instance method 2", 0, findMatchesCount(s111, "B.getManager()")); String s113 = """ class A { static void a() { a(); }} class B { static void a() { a(); a(); }} """; assertEquals("should care about implicit class qualifier", 2, findMatchesCount(s113, "'_Q?:[regex( B )].a()")); assertEquals("should match simple implicit class qualifier query", 2, findMatchesCount(s113, "B.a()")); assertEquals("instance query should not match static method", 0, findMatchesCount(s113, "'_Q?:[exprtype( B )].a()")); String s115 = """ class A { int a; int f() { return a; }} class B { int a; int g() { return a + a; }} """; assertEquals("should care about implicit instance qualifier", 2, findMatchesCount(s115, "'_Instance?:[exprtype( B )].a")); assertEquals("should not match instance method", 0, findMatchesCount(s115, "A.a")); String s117 = """ class A { static int a; static int f() { return a; }} class B { static int a; static int g() { return a + a; }} """; assertEquals("should care about implicit class qualifier for field", 2, findMatchesCount(s117, "'_Q?:[regex( B )].a")); // b) hierarchy navigation support // c) or search support // e) xml search (down-up, nested query), navigation from xml representation <-> java code // f) impl data conversion (jdk 1.5 style) <-> other from (replace support) // Directions: // @todo different navigation on sub/supertyping relation (fixed depth), methods implementing interface, // g. like predicates // i. performance // more context for top level classes, difference with interface, etc // global issues: // @todo matches out of context // @todo proper regexp support // @todo define strict equality of the matches // @todo search for field selection retrieves packages also }
testOther
291,785
void () { String s1 = "import java.awt.List;\n" + "class A { List l; }"; String s2 = "class '_ { 'Type:java\\.util\\.List '_Field; }"; assertEquals("No matches for qualified class", 0, findMatchesCount(s1, s2)); String s1_2 = "import java.util.List;\n" + "class A { List l; }"; assertEquals("Matches for qualified class", 1, findMatchesCount(s1_2, s2)); String s3 = "import java.util.ArrayList;\n" + "class A { ArrayList l; }"; assertEquals("Matches for qualified class in hierarchy", 1, findMatchesCount(s3, "class '_ { 'Type:*java\\.util\\.Collection '_Field; }")); String s5 = "import java.util.List;\n" + "class A { { List l = new List(); l.add(\"1\"); } }"; assertEquals("Matches for qualified expr type in hierarchy", 2, findMatchesCount(s5, "'a:[exprtype( *java\\.util\\.Collection )]")); String s6 = "'a:[exprtype( java\\.util\\.List )]"; assertEquals("Matches for qualified expr type", 2, findMatchesCount(s5, s6)); String s5_2 = "import java.awt.List;\n" + "class A { { List l = new List(); l.add(\"1\"); } }"; assertEquals("No matches for qualified expr type", 0, findMatchesCount(s5_2, s6)); String s6_3 = "java.util.List '_a = '_b?;"; assertEquals("Matches for qualified var type in pattern", 1, findMatchesCount(s5, s6_3)); assertEquals("No matches for qualified var type in pattern", 0, findMatchesCount(s5_2, s6_3)); String s7 = "import java.util.List;\n" + "class A extends List { }"; String s8 = "class 'a extends java.util.List {}"; assertEquals("Matches for qualified type in pattern", 1, findMatchesCount(s7, s8)); String s7_2 = "import java.awt.List;\n" + "class A extends List {}"; assertEquals("No matches for qualified type in pattern", 0, findMatchesCount(s7_2, s8)); String s9 = """ class X {{ String.intern("1"); java.util.Collections.sort(null); java.util.Collections.sort(null); }}"""; assertEquals("FQN in class name",1, findMatchesCount(s9, "java.lang.String.'_method ( '_params* )")); }
testFQNInPatternAndVariableConstraints
291,786
void () { String s1 = """ @MyBean("") @MyBean2("") public class TestBean {} @MyBean2("") @MyBean(value="") public class TestBean2 {} public class TestBean3 {} @MyBean("a") @MyBean2("a") public class TestBean4{}"""; String s2 = """ @MyBean("") @MyBean2("") public class '_a {} """; assertEquals("Simple find annotated class", 2, findMatchesCount(s1, s2)); assertEquals("Match value of anonymous name value pair 1", 1, findMatchesCount(s1, "@MyBean(\"a\") class '_a {}")); assertEquals("Match value of anonymous name value pair 2", 2, findMatchesCount(s1, "@MyBean(\"\") class '_a {}")); String s3 = """ @VisualBean("????????? ?????????? ? ??") public class TestBean { @VisualBeanField( name = "??? ????????????", initialValue = "?????????????" ) public String user; @VisualBeanField( name = "??????", initialValue = "", fieldType = FieldTypeEnum.PASSWORD_FIELD ) public String password; @VisualBeanField( initialValue = "User", name = "????? ???????", name = "Second name", fieldType = FieldTypeEnum.COMBOBOX_FIELD, comboValues = { @ComboFieldValue("Administrator"), @ComboFieldValue("User"), @ComboFieldValue("Guest")} ) public String accessRights; public String otherField; }"""; String s4 = """ class '_a { @'_Annotation+ ( 'AnnotationMember:name = '_AnnotationValue ) String '_field* ; }"""; assertEquals("Find annotation members of annotated field class", 4, findMatchesCount(s3, s4)); String s4_2 = """ class '_a { @'_Annotation+ () String 'field ; }"""; assertEquals("Find annotation fields", 3, findMatchesCount(s3, s4_2)); String s5 = """ class A { @NotNull private static Collection<PsiElement> resolveElements(final PsiReference reference, final Project project) {} @NotNull private static Collection resolveElements2(final PsiReference reference, final Project project) {} }"""; assertEquals("Find annotated methods", 2, findMatchesCount(s5, "class '_c {@NotNull '_rt 'method ('_pt '_p*){ '_inst*; } }")); assertEquals("Find annotated methods, 2", 2, findMatchesCount(s5, "class '_c {@'_:NotNull '_rt 'method ('_pt '_p*){ '_inst*; } }")); String s7 = """ class A { void message(@NonNls String msg); } class B { void message2(String msg); } class C { void message2(String msg); }"""; assertEquals("Find not annotated methods", 2, findMatchesCount(s7, "class '_A { void 'b( @'_Ann{0,0}:NonNls String '_); }")); String s9 = """ class A { Object[] method1() {} Object method1_2() {} Object method1_3() {} Object method1_4() {} @MyAnnotation Object[] method2(int a) {} @NonNls Object[] method3() {} }"""; assertEquals("Find not annotated methods, 2", 2, findMatchesCount(s9, "class '_A { @'_Ann{0,0}:NonNls '_Type:Object\\[\\] 'b( '_pt '_p* ); }")); assertEquals("Find not annotated methods, 2", 2, findMatchesCount(s9, "class '_A { @'_Ann{0,0}:NonNls '_Type [] 'b( '_pt '_p* ); }")); assertEquals("Find not annotated methods, 2", 2, findMatchesCount(s9, "class '_A { @'_Ann{0,0}:NonNls '_Type:Object [] 'b( '_pt '_p* ); }")); String s11 = """ class A { @Foo(value=baz) int a; @Foo(value=baz2) int a2; @Foo(baz2) int a3; @Foo(value2=baz2) int a4; @Foo(value2=baz2) int a5; @Foo(value2=baz3) int a6; @Foo(value2=baz3) int a7; @Foo(value2=baz3) int a8; @Foo(value2=baz4) int a9; @Foo int a10; }"""; assertEquals("Find anno parameter value 1", 1, findMatchesCount(s11, "@Foo(value=baz) int 'a;")); assertEquals("Find anno parameter value 2", 2, findMatchesCount(s11, "@Foo(value='_value:baz2 ) int '_a;")); assertEquals("Find anno parameter value 3", 2, findMatchesCount(s11, "@Foo('_name:value ='_value:baz2 ) int '_a;")); assertEquals("Find anno parameter value 4", 3, findMatchesCount(s11, "@Foo('_name:value2 = baz3 ) int '_a;")); assertEquals("Find anno parameter value 5", 3, findMatchesCount(s11, "@Foo('_name:value2 = '_value:baz3 ) int '_a;")); assertEquals("Find anno parameter value 6", 0, findMatchesCount(s11, "@Foo('_name:value2 = '_value:baz ) int '_a;")); assertEquals("Find anno parameter value 7", 6, findMatchesCount(s11, "@Foo('_name:value2 = '_value ) int '_a;")); assertEquals("Find anno parameter value 8", 9, findMatchesCount(s11, "@Foo('_name = '_value ) int '_a;")); try { findMatchesCount(s11, "@Foo('_name:value2 = ) int '_a;"); fail("should report missing value"); } catch (MalformedPatternException ignored) {} assertEquals("Match anno parameter name", 6, findMatchesCount(s11, "@Foo(value2='_value)")); assertEquals("Match anno parameter name 2", 3, findMatchesCount(s11, "@Foo(value='_value)")); assertEquals("Match value anno parameter only", 2, findMatchesCount(s11, "@Foo(baz2)")); assertEquals("Match value anno parameters", 3, findMatchesCount(s11, "@Foo('_value)")); assertEquals("Match all annotations", 10, findMatchesCount(s11, "@Foo('_value?)")); assertEquals("Match all annotations 2", 10, findMatchesCount(s11, "@Foo")); assertEquals("Match annotations without parameters", 1, findMatchesCount(s11, "@Foo('_name{0,0}='_v)")); String s12 = "@X(value=1, x=2) @Y(1) @Z(x=0, y=0, z=0) @W(2) @V(x=0, y=0, z=0) class One {}"; assertEquals("Match annotations with two parameters", 1, findMatchesCount(s12, "@'_anno('_name{2,2}='_v)")); String source1 = """ class A { void m() { new @B Object(); } }"""; assertEquals("Find annotated new expression", 1, findMatchesCount(source1, "new Object()")); assertEquals("Find annotated new expression", 1, findMatchesCount(source1, "new @B Object()")); assertEquals("Find annotated new expression", 0, findMatchesCount(source1, "new @C Object()")); String source2 = """ @X class A { @Y int value; @Y int m(@Z int i) { return 1; } } """; assertEquals("Find all annotations", 4, findMatchesCount(source2, "@'_Annotation")); String source3 = """ class A<@HH T> extends @HH Object { @HH final String s = (@HH String) new @HH Object(); final String t = (String) new Object(); Map<@HH String, @HH List<@HH String>> map; } """; assertEquals("Find annotated casts", 1, findMatchesCount(source3, "(@'_A 'Cast) '_Expression")); assertEquals("Find annotated new expressions", 1, findMatchesCount(source3, "new @'_A 'Type()")); assertEquals("Find all annotations 2", 8, findMatchesCount(source3, "@'_Annotation")); // package-info.java final String source4 = """ /** * documentation */ @Deprecated package one.two;"""; assertEquals("Find annotation on package statement", 1, findMatchesCount(source4, "@'_Annotation")); final String source5 = """ class A { boolean a(Object o) { return o instanceof @HH String; } }"""; assertEquals("Find annotation on instanceof expression", 1, findMatchesCount(source5, "'_a instanceof @HH String")); assertEquals("Match annotation correctly on instanceof expression", 0, findMatchesCount(source5, "'_a instanceof @GG String")); String source6 = """ @SuppressWarnings({"WeakerAccess", "unused", "UnnecessaryInterfaceModifier"}) class A { @SuppressWarnings({"unused"}) @NotNull int i; }"""; assertEquals("Find SuppressWarnings annotations", 2, findMatchesCount(source6, "@SuppressWarnings")); assertEquals("Find SuppressWarnings annotations", 2, findMatchesCount(source6, "@SuppressWarnings(value='_any)")); assertEquals("Find annotation with 3 value array initializer", 1, findMatchesCount(source6, "@SuppressWarnings({'_value{3,3} })")); String source7 = """ class X { @interface Annotation { String[] value() default {}; } @Annotation("Hello") static void singleValue() {} @Annotation({"Hello"}) static void multiValue() {} @Annotation(value = "Hello") static void explicitSingleValue() {} @Annotation(value = {"Hello"}) static void explicitMultiValue() {} @Annotation({"Hello", "World"}) static void different() {} @Annotation("Bye!") static void end() {} }"""; assertEquals("Find all equivalent annotations", 4, findMatchesCount(source7, "@Annotation(\"Hello\")")); assertEquals("Find all annotations with a specific value", 5, findMatchesCount(source7, "@Annotation({\"Hello\", '_O*})")); assertEquals("Find all annotations", 6, findMatchesCount(source7, "@Annotation('_V)")); String source8 = """ @java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface NotNull {} class X { boolean a(@NotNull String s, @NotNull String t, String u) { java.util.Objects.equals(s, t); return java.util.Objects.equals(t, u); } }"""; assertEquals("Call with @NotNull annotated argument", 1, findMatchesCount(source8, "Objects.equals('_a:[exprtype( ~@NotNull.* )], '_b:[exprtype( ~@NotNull.* )])")); assertEquals("Call with arguments with @NotNull String type", 1, findMatchesCount(source8, "Objects.equals('_a:[exprtype( @NotNull String )], '_b:[exprtype( @NotNull String )])")); }
testAnnotations
291,787
Collection<PsiElement> (final PsiReference reference, final Project project) {}
resolveElements
291,788
Collection (final PsiReference reference, final Project project) {}
resolveElements2
291,789
void () {}
singleValue
291,790
void () {}
multiValue
291,791
void () {}
explicitSingleValue
291,792
void () {}
explicitMultiValue
291,793
void () {}
different
291,794
void () {}
end
291,795
void () { String s1 = """ class X {{ class A { void b(Integer i); void b2(int i); void c(int d); void c2(Integer d); } A a; a.b2(1) ; a.b2(1) ; a.b(1) ; a.b( new Integer(0) ) ; a.b( new Integer(0) ) ; a.c(new Integer(2)); a.c(new Integer(3)); a.c2(new Integer(3)); a.c(3); Integer i = 4; int j = Integer.valueOf(4); }}"""; assertEquals("Find boxing in method call", 1, findMatchesCount(s1, "a.'b('_Params:[formal( Integer ) && exprtype( int ) ])")); assertEquals("Find unboxing in method call", 2, findMatchesCount(s1, "a.c('_Params:[formal( int ) && exprtype( Integer ) ])")); assertEquals("Find any boxing", 2, findMatchesCount(s1, "'_a:[formal( Integer ) && exprtype( int ) ]")); assertEquals("Find any unboxing", 3, findMatchesCount(s1, "'_a:[formal( int ) && exprtype( Integer ) ]")); }
testBoxingAndUnboxing
291,796
void () { String s1 = """ class A { int a; // comment char b; int c; // comment2 }"""; assertEquals("Find field by dcl with comment", 2, findMatchesCount(s1, "'_Type '_Variable = '_Value?; //'Comment")); String s2 = """ class A { // comment int a; char b; // comment2 int c; }"""; assertEquals("Find field by dcl with comment 2", 2, findMatchesCount(s2, "//'Comment\n'_Type '_Variable = '_Value?;")); String s3 = """ // comment class A {} class B {} class C {}"""; assertEquals("Find class with comment", 1, findMatchesCount(s3, "//'_comment\nclass '_X {}")); }
testCommentsInDclSearch
291,797
void () { String s1 = """ class A { int a; private char b; private char b2; public int c; public int c2; }"""; assertEquals("Finding package-private dcls",1, findMatchesCount(s1, "@Modifier(\"packageLocal\") '_Type '_Variable = '_Value?;")); assertEquals("Finding package-private dcls",3, findMatchesCount(s1, "@Modifier({\"packageLocal\",\"private\"}) '_Type '_Variable = '_Value?;")); try { findMatchesCount(s1, "@Modifier({\"PackageLocal\",\"private\"}) '_Type '_Variable = '_Value?;"); fail("Finding package-private dcls"); } catch(MalformedPatternException ignored) {} String s3 = """ class A { int a; static char b; static char b2; }"""; assertEquals("Finding instance fields",1, findMatchesCount(s3, "@Modifier(\"Instance\") '_Type '_Variable = '_Value?;")); assertEquals("Finding all fields",3, findMatchesCount(s3, "@Modifier({\"static\",\"Instance\"}) '_Type '_Variable = '_Value?;")); String s5 = """ class A {} abstract class B {} final class C {} class D {}"""; assertEquals("Finding instance classes",3,findMatchesCount(s5, "@Modifier(\"Instance\") class 'Type {}")); assertEquals("Finding all classes",4, findMatchesCount(s5, "@Modifier({\"abstract\",\"final\",\"Instance\"}) class 'Type {}")); }
testSearchingEmptyModifiers
291,798
void () { String source = """ public class TestClass { transient private String field1; transient String field2; String field3; }"""; assertEquals("Finding package-private transient fields", 1, findMatchesCount(source, "transient @Modifier(\"packageLocal\") '_Type '_Variable = '_Value?;")); }
testSearchTransientFieldsWithModifier
291,799
void () { String s1 = """ class X {{ if (LOG.isDebugEnabled()) { int a = 1; int a = 1; } }}"""; String pattern = """ if ('_Log.isDebugEnabled()) { '_ThenStatement; '_ThenStatement; }"""; assertEquals("Comparing declarations", 1, findMatchesCount(s1, pattern)); }
test