From 2ed09b7cec8b94cdd8709d1e01b7e8835f2a11fa Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 6 Feb 2018 16:36:03 +0300 Subject: [PATCH] implicit to string search: check method before search --- .../find/findUsages/JavaFindUsagesHelper.java | 2 +- .../impl/search/ImplicitToStringSearcher.java | 15 --------------- .../searches/ImplicitToStringSearch.java | 18 ++++++++++++++++-- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/find/findUsages/JavaFindUsagesHelper.java b/java/java-analysis-impl/src/com/intellij/find/findUsages/JavaFindUsagesHelper.java index 784d7564699d..75fef6429a53 100644 --- a/java/java-analysis-impl/src/com/intellij/find/findUsages/JavaFindUsagesHelper.java +++ b/java/java-analysis-impl/src/com/intellij/find/findUsages/JavaFindUsagesHelper.java @@ -182,7 +182,7 @@ public class JavaFindUsagesHelper { FunctionalExpressionSearch.search(psiMethod, methodOptions.searchScope).forEach(new PsiElementProcessorAdapter<>( expression -> addResult(expression, options, processor))); } - if (methodOptions.isImplicitToString) { + if (ImplicitToStringSearch.isToStringMethod(psiMethod) && methodOptions.isImplicitToString) { ImplicitToStringSearch.search(psiMethod, methodOptions.searchScope).forEach(new PsiElementProcessorAdapter<>(ref -> addResult(ref, options, processor))); } } diff --git a/java/java-impl/src/com/intellij/psi/impl/search/ImplicitToStringSearcher.java b/java/java-impl/src/com/intellij/psi/impl/search/ImplicitToStringSearcher.java index 68342994347f..bad1b3dd53f5 100644 --- a/java/java-impl/src/com/intellij/psi/impl/search/ImplicitToStringSearcher.java +++ b/java/java-impl/src/com/intellij/psi/impl/search/ImplicitToStringSearcher.java @@ -68,21 +68,6 @@ public class ImplicitToStringSearcher extends QueryExecutorBase search(@NotNull PsiMethod targetMethod, @NotNull SearchScope scope) { return INSTANCE.createUniqueResultsQuery(new SearchParameters(targetMethod, scope)); } + + public static boolean isToStringMethod(@NotNull PsiElement element) { + if (!(element instanceof PsiMethod)) { + return false; + } + PsiMethod method = (PsiMethod)element; + if (!"toString".equals(method.getName())) { + return false; + } + if (method.getParameters().length != 0) { + return false; + } + PsiType returnType = method.getReturnType(); + return returnType != null && returnType.equalsToText(CommonClassNames.JAVA_LANG_STRING); + } }