From 41ebb4f64778404bf6743b46a820347e8d7b44c4 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 8 Jan 2018 16:23:16 +0100 Subject: [PATCH] goto implementation: narrow scope to file for private methods()IDEA-184663 --- .../codeInsight/JavaTargetElementEvaluator.java | 3 +++ .../navigation/GotoImplementationHandlerTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/java/java-impl/src/com/intellij/codeInsight/JavaTargetElementEvaluator.java b/java/java-impl/src/com/intellij/codeInsight/JavaTargetElementEvaluator.java index 2c26949700fc..22d1caed2b19 100644 --- a/java/java-impl/src/com/intellij/codeInsight/JavaTargetElementEvaluator.java +++ b/java/java-impl/src/com/intellij/codeInsight/JavaTargetElementEvaluator.java @@ -361,6 +361,9 @@ public class JavaTargetElementEvaluator extends TargetElementEvaluatorEx2 implem public SearchScope getSearchScope(Editor editor, @NotNull final PsiElement element) { final PsiReferenceExpression referenceExpression = editor != null ? findReferenceExpression(editor) : null; if (referenceExpression != null && element instanceof PsiMethod) { + if (!PsiUtil.canBeOverridden((PsiMethod)element)) { + return new LocalSearchScope(element.getContainingFile()); + } final PsiClass[] memberClass = getClassesWithMember(referenceExpression, (PsiMember)element); if (memberClass != null && memberClass.length == 1) { return CachedValuesManager.getCachedValue(memberClass[0], () -> { diff --git a/java/java-tests/testSrc/com/intellij/java/navigation/GotoImplementationHandlerTest.java b/java/java-tests/testSrc/com/intellij/java/navigation/GotoImplementationHandlerTest.java index 92035244b961..9321bc00fb5e 100644 --- a/java/java-tests/testSrc/com/intellij/java/navigation/GotoImplementationHandlerTest.java +++ b/java/java-tests/testSrc/com/intellij/java/navigation/GotoImplementationHandlerTest.java @@ -16,10 +16,14 @@ package com.intellij.java.navigation; import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.TargetElementUtil; import com.intellij.codeInsight.navigation.GotoTargetHandler; +import com.intellij.ide.highlighter.JavaFileType; import com.intellij.openapi.roots.ModuleRootModificationUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; +import com.intellij.psi.search.SearchScope; +import com.intellij.psi.util.PsiUtilCore; import com.intellij.testFramework.IdeaTestUtil; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.fixtures.CodeInsightTestUtil; @@ -358,6 +362,16 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas assertSize(2, getTargets(file)); } + public void testScopeForPrivateMethod() { + PsiFile file = myFixture.configureByText(JavaFileType.INSTANCE, "class Foo {" + + " {foo();}" + + " private void foo() {}" + + "}"); + PsiClass inheritor = myFixture.addClass("class FooImpl extends Foo {}"); + SearchScope scope = TargetElementUtil.getInstance().getSearchScope(myFixture.getEditor(), ((PsiJavaFile)file).getClasses()[0].getMethods()[0]); + assertFalse(scope.contains(PsiUtilCore.getVirtualFile(inheritor))); + } + public void testAnonymousAndLocalClassesInLibrary() { ModuleRootModificationUtil.addModuleLibrary( myModule,