diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java index 9640351c40cc..af934b40b462 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java @@ -1478,20 +1478,20 @@ public class JavaMatchingVisitor extends JavaElementVisitor { new1.getArrayDimensions().length == 0 && new1.getArrayInitializer() != null ) { + final MatchContext matchContext = myMatchingVisitor.getMatchContext(); + final MatchingHandler handler = matchContext.getPattern().getHandler(classReference); final boolean looseMatching = myMatchingVisitor.getMatchContext().getOptions().isLooseMatching(); - final boolean typedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(classReference); - if ((typedVar || !looseMatching) && !allowsAbsenceOfMatch(classReference)) { + if ((handler instanceof SubstitutionHandler && ((SubstitutionHandler)handler).getMinOccurs() != 0) || !looseMatching) { myMatchingVisitor.setResult(false); return; } - final PsiElementFactory factory = JavaPsiFacade.getElementFactory(other.getProject()); - final PsiType otherType = ((PsiVariable)other.getParent()).getType(); - final PsiTypeElement otherTypeElement = factory.createTypeElement(otherType.getDeepComponentType()); - final MatchContext matchContext = myMatchingVisitor.getMatchContext(); - final MatchingHandler handler = matchContext.getPattern().getHandler(classReference); - if (handler instanceof SubstitutionHandler) { + final PsiType otherType = ((PsiArrayInitializerExpression)other).getType(); + if (handler instanceof SubstitutionHandler && otherType != null) { + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(other.getProject()); + final PsiTypeElement otherTypeElement = factory.createTypeElement(otherType.getDeepComponentType()); final SubstitutionHandler substitutionHandler = (SubstitutionHandler)handler; - myMatchingVisitor.setResult(substitutionHandler.handle(otherTypeElement, matchContext)); + final MatchPredicate predicate = substitutionHandler.getPredicate(); + myMatchingVisitor.setResult(predicate == null || predicate.match(null, otherTypeElement, matchContext)); } else { final PsiType type = new1.getType(); diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java index a67bfcdffa19..cec083fbb7fd 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java @@ -551,13 +551,36 @@ public class StructuralSearchTest extends StructuralSearchTestCase { String s9 = "int a[] = new int[] { 1,2,3,4};\n" + "int b[] = { 2,3,4,5 };\n" + - "Object[] c = new Object[] { \"\", null};"; + "Object[] c = new Object[] { \"\", null};\n" + + "Object[] d = {null, null};\n" + + "Object[] e = {};\n" + + "Object[] f = new Object[]{}\n" + + "String[] g = new String[]{}\n" + + "String[] h = new String[]{new String()}"; - assertEquals("Find array instantiation 1", 2, findMatchesCount(s9, "new '_ []{ '_* }")); - assertEquals("Find array instantiation 2", 2, findMatchesCount(s9, "new int []{ '_* }")); - assertEquals("Find array instantiation 3", 2, findMatchesCount(s9, "new 'a?:int [] { '_* }")); - assertEquals("Find array instantiation 4", 3, findMatchesCount(s9, "new '_? []{ '_* }")); - assertEquals("Find array instantiation 5", 1, findMatchesCount(s9, "new Object[] { '_* }")); + 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 [] { '_* }")); } public void testLiteral() {