From d327e0de3e9ce35ab7ee0f324b19122846204aa4 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 1 Jun 2018 21:31:53 +0200 Subject: [PATCH] SSR: fix matching method parameters in super classes (IDEA-193171) --- .../impl/matcher/JavaMatchingVisitor.java | 11 ++++++++++- .../JavaPredefinedConfigurationsTest.java | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) 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 997811ef1595..0f7455272fae 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 @@ -957,7 +957,16 @@ public class JavaMatchingVisitor extends JavaElementVisitor { myMatchingVisitor.setResult(myMatchingVisitor.matchOptionally(initializer, var2Initializer)); } finally { - myMatchingVisitor.scopeMatch(nameIdentifier, isTypedVar, var2.getNameIdentifier()); + final PsiIdentifier identifier = var2.getNameIdentifier(); + final String name; + if (identifier == null && (name = var2.getName()) != null) { + // when matching a stub or compiled code + final PsiIdentifier fakeIdentifier = JavaPsiFacade.getElementFactory(var2.getProject()).createIdentifier(name); + myMatchingVisitor.scopeMatch(nameIdentifier, isTypedVar, fakeIdentifier); + } + else { + myMatchingVisitor.scopeMatch(nameIdentifier, isTypedVar, identifier); + } } } diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/JavaPredefinedConfigurationsTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/JavaPredefinedConfigurationsTest.java index f2cf27867050..257cdf6f4854 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/JavaPredefinedConfigurationsTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/JavaPredefinedConfigurationsTest.java @@ -2,11 +2,13 @@ package com.intellij.structuralsearch; import com.intellij.openapi.fileTypes.StdFileTypes; +import com.intellij.psi.PsiElement; import com.intellij.structuralsearch.plugin.ui.Configuration; import com.intellij.structuralsearch.plugin.ui.SearchConfiguration; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -142,18 +144,26 @@ public class JavaPredefinedConfigurationsTest extends StructuralSearchTestCase { " X(int i) {}" + "}", " X(String s) {}", " X(int i) {}"); + doTest(configurationMap.remove(SSRBundle.message("predefined.configuration.all.methods.of.the.class.within.hierarchy")), + "class X {}", + PsiElement::getText, + "registerNatives", "getClass", "hashCode", "equals", "clone", "toString", "notify", "notifyAll", "wait", "wait", "wait", "finalize"); //assertTrue("untested configurations: " + configurationMap.keySet(), configurationMap.isEmpty()); } private void doTest(Configuration template, String source, String... results) { + doTest(template, source, e -> StructuralSearchUtil.getPresentableElement(e).getText(), results); + } + + private void doTest(Configuration template, String source, Function resultConverter, String... expectedResults) { if (!(template instanceof SearchConfiguration)) fail(); final SearchConfiguration searchConfiguration = (SearchConfiguration)template; options = searchConfiguration.getMatchOptions(); final List matches = testMatcher.testFindMatches(source, options, true, StdFileTypes.JAVA, null, false); - assertEquals(template.getName(), results.length, matches.size()); - for (int i = 0; i < matches.size(); i++) { - final String matchText = StructuralSearchUtil.getPresentableElement(matches.get(i).getMatch()).getText(); - assertEquals(template.getName(), results[i], matchText); + assertEquals(template.getName(), expectedResults.length, matches.size()); + String[] actualResults = matches.stream().map(MatchResult::getMatch).map(resultConverter).toArray(String[]::new); + for (int i = 0; i < actualResults.length; i++) { + assertEquals(template.getName(), expectedResults[i], actualResults[i]); } } } \ No newline at end of file