implicit to string search: check method before search

This commit is contained in:
Dmitry Batkovich
2018-04-25 13:29:08 +03:00
parent c0ba114576
commit 2ed09b7cec
3 changed files with 17 additions and 18 deletions
@@ -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)));
}
}
@@ -68,21 +68,6 @@ public class ImplicitToStringSearcher extends QueryExecutorBase<PsiExpression, I
}
}
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);
}
private static boolean processFile(VirtualFile file,
int[] offsets,
PsiManager manager,
@@ -1,8 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.search.searches;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.*;
import com.intellij.psi.search.SearchScope;
import com.intellij.util.Query;
import org.jetbrains.annotations.NotNull;
@@ -34,4 +33,19 @@ public class ImplicitToStringSearch extends ExtensibleQueryFactory<PsiExpression
public static Query<PsiExpression> 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);
}
}