diff --git a/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java b/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java index 7c5e9de9d3e2..228e4b513682 100644 --- a/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java +++ b/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java @@ -58,7 +58,7 @@ public class SuperMethodWarningUtil { } @NotNull - public static PsiMethod[] getTargetMethodCandidates(@NotNull PsiMethod method, @NotNull Collection ignore) { + public static PsiMethod[] getTargetMethodCandidates(@NotNull PsiMethod method, @NotNull Collection 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 ignore) { + public static PsiMethod[] checkSuperMethods(@NotNull PsiMethod method, @NotNull String actionString, @NotNull Collection 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 getSuperMethods(@NotNull PsiMethod method, PsiClass aClass, @NotNull Collection ignore) { + static Collection getSuperMethods(@NotNull PsiMethod method, PsiClass aClass, @NotNull Collection ignore) { ApplicationManager.getApplication().assertIsDispatchThread(); assert !ApplicationManager.getApplication().isWriteAccessAllowed(); final Collection 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); diff --git a/java/java-tests/testData/psi/search/findUsages/siblingFindUsages/A.java b/java/java-tests/testData/psi/search/findUsages/siblingFindUsages/A.java new file mode 100644 index 000000000000..03f40a362371 --- /dev/null +++ b/java/java-tests/testData/psi/search/findUsages/siblingFindUsages/A.java @@ -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(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/psi/search/FindUsagesTest.java b/java/java-tests/testSrc/com/intellij/java/psi/search/FindUsagesTest.java index 5f00b59a722f..a9e06b887db5 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/search/FindUsagesTest.java +++ b/java/java-tests/testSrc/com/intellij/java/psi/search/FindUsagesTest.java @@ -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());