When invoking Find Usages on a method which is sibling-inherited, include the method itself (along with found super-sibling) to the primary elements because otherwise it can't sometimes be found by super-sibling alone.

GitOrigin-RevId: 2be6da78182c7b52527eaa7df5f64b8522d94bee
This commit is contained in:
Alexey Kudravtsev
2019-04-28 14:16:24 +03:00
committed by intellij-monorepo-bot
parent 40b8168ff2
commit 28541d269e
3 changed files with 49 additions and 7 deletions
@@ -58,7 +58,7 @@ public class SuperMethodWarningUtil {
}
@NotNull
public static PsiMethod[] getTargetMethodCandidates(@NotNull PsiMethod method, @NotNull Collection<PsiElement> ignore) {
public static PsiMethod[] getTargetMethodCandidates(@NotNull PsiMethod method, @NotNull Collection<? extends PsiElement> ignore) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) return new PsiMethod[]{method};
@@ -68,7 +68,7 @@ public class SuperMethodWarningUtil {
}
@NotNull
public static PsiMethod[] checkSuperMethods(@NotNull PsiMethod method, @NotNull String actionString, @NotNull Collection<PsiElement> ignore) {
public static PsiMethod[] checkSuperMethods(@NotNull PsiMethod method, @NotNull String actionString, @NotNull Collection<? extends PsiElement> ignore) {
ApplicationManager.getApplication().assertIsDispatchThread();
PsiMethod[] methodTargetCandidates = getTargetMethodCandidates(method, ignore);
if (methodTargetCandidates.length == 1 && methodTargetCandidates[0] == method) return methodTargetCandidates;
@@ -100,7 +100,7 @@ public class SuperMethodWarningUtil {
}
@NotNull
static Collection<PsiMethod> getSuperMethods(@NotNull PsiMethod method, PsiClass aClass, @NotNull Collection<PsiElement> ignore) {
static Collection<PsiMethod> getSuperMethods(@NotNull PsiMethod method, PsiClass aClass, @NotNull Collection<? extends PsiElement> ignore) {
ApplicationManager.getApplication().assertIsDispatchThread();
assert !ApplicationManager.getApplication().isWriteAccessAllowed();
final Collection<PsiMethod> superMethods = DeepestSuperMethodsSearch.search(method).findAll();
@@ -112,11 +112,12 @@ public class SuperMethodWarningUtil {
PsiMethod[] siblingSuperMethod = new PsiMethod[1];
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(()->{
siblingSuperMethod[0] = ReadAction.compute(()->FindSuperElementsHelper.getSiblingInheritedViaSubClass(method));
}, "Searching for sub-classes", true, aClass.getProject())) {
}, "Searching for Sub-Classes", true, aClass.getProject())) {
throw new ProcessCanceledException();
}
if (siblingSuperMethod[0] != null) {
superMethods.add(siblingSuperMethod[0]);
superMethods.add(method); // add original method too because sometimes FindUsages can't find usages of this method by sibling super method
}
}
}
@@ -191,7 +192,7 @@ public class SuperMethodWarningUtil {
.setMovable(false)
.setResizable(false)
.setRequestFocus(true)
.setItemChosenCallback((value) -> {
.setItemChosenCallback(value -> {
if (value.equals(renameBase)) {
try {
methods[0].putUserData(SIBLINGS, superMethods);
@@ -0,0 +1,18 @@
interface WatchNode {
void setObsolete();
}
abstract class XValueContainerNode {
public void setObsolete() {
}
}
class WatchNodeImpl extends XValueContainerNode implements WatchNode {
}
class Use {
void fff(XValueContainerNode node) {
node.setObsolete();
}
}
@@ -2,8 +2,9 @@
package com.intellij.java.psi.search;
import com.intellij.JavaTestUtil;
import com.intellij.find.findUsages.JavaFindUsagesHandler;
import com.intellij.find.findUsages.JavaFindUsagesHandlerFactory;
import com.intellij.find.FindManager;
import com.intellij.find.findUsages.*;
import com.intellij.find.impl.FindManagerImpl;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.module.ModifiableModuleModel;
@@ -17,6 +18,7 @@ import com.intellij.psi.search.PsiReferenceProcessorAdapter;
import com.intellij.psi.search.searches.MethodReferencesSearch;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.PsiTestCase;
import com.intellij.testFramework.PsiTestUtil;
@@ -67,6 +69,27 @@ public class FindUsagesTest extends PsiTestCase{
assertEquals(1, overriders.size());
}
public void testSiblingFindUsages() {
PsiClass xv = myJavaFacade.findClass("XValueContainerNode", GlobalSearchScope.allScope(myProject));
PsiMethod method = xv.findMethodsByName("setObsolete", false)[0];
FindUsagesHandler handler = ((FindManagerImpl)FindManager.getInstance(getProject())).getFindUsagesManager()
.getFindUsagesHandler(method, FindUsagesHandlerFactory.OperationMode.USAGES_WITH_DEFAULT_OPTIONS);
PsiElement[] elements = handler.getPrimaryElements();
int[] count = {0};
for (PsiElement element : elements) {
handler.processElementUsages(element, info -> {
count[0]++;
PsiClass containing = PsiTreeUtil.getParentOfType(info.getElement(), PsiClass.class);
assertEquals("Use", containing.getName());
return true;
}, handler.getFindUsagesOptions());
}
assertEquals(1, count[0]);
}
public void testProtectedMethodInPackageLocalClass() {
PsiMethod method = myJavaFacade.findClass("foo.PackageLocal", GlobalSearchScope.allScope(myProject)).getMethods()[0];
assertEquals(1, OverridingMethodsSearch.search(method).findAll().size());