From 86977a23ece4b99cab354d656677baf8b6d5bf87 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 18 Jul 2017 12:19:02 +0300 Subject: [PATCH] access visibility inspection: fix global inspection in case of inner classes IDEA-175921 --- .../visibility/VisibilityInspection.java | 10 +++------ .../expected.xml | 9 ++++++++ .../src/Foo.java | 6 +++++ .../src/FooImpl.java | 5 +++++ .../expected.xml | 9 ++++++++ .../src/Foo.java | 13 +++++++++++ .../expected.xml | 9 ++++++++ .../src/Foo.java | 5 +++++ .../src/FooImpl.java | 7 ++++++ .../VisibilityInspectionTest.java | 22 +++++++++++++++++++ 10 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/expected.xml create mode 100644 java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/Foo.java create mode 100644 java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/FooImpl.java create mode 100644 java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/expected.xml create mode 100644 java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/src/Foo.java create mode 100644 java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/expected.xml create mode 100644 java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/Foo.java create mode 100644 java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/FooImpl.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/visibility/VisibilityInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/visibility/VisibilityInspection.java index 6865cf828374..4a43d526e9da 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/visibility/VisibilityInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/visibility/VisibilityInspection.java @@ -406,13 +406,9 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool { if (to instanceof RefJavaElementImpl && ((RefJavaElementImpl)to).isUsedQualifiedOutsidePackage()) { return false; } - if (SUGGEST_PRIVATE_FOR_INNERS) { - return fromTopLevel != null && refUtil.isInheritor(fromTopLevel, toOwner) - || fromOwner != null && refUtil.isInheritor(fromOwner, toTopLevel) - || toOwner != null && refUtil.getOwnerClass(toOwner) == from; - } - - return fromTopLevel != null && refUtil.isInheritor(fromTopLevel, toOwner); + return fromTopLevel != null && refUtil.isInheritor(fromTopLevel, toOwner) + || fromOwner != null && refUtil.isInheritor(fromOwner, toTopLevel) + || toTopLevel != null && toTopLevel == fromTopLevel; } if (accessModifier == PsiModifier.PRIVATE) { diff --git a/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/expected.xml b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/expected.xml new file mode 100644 index 000000000000..2e7451e9f0b0 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/expected.xml @@ -0,0 +1,9 @@ + + + + Foo.java + 3 + Can be package-private + + + diff --git a/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/Foo.java b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/Foo.java new file mode 100644 index 000000000000..ff8db9b91736 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/Foo.java @@ -0,0 +1,6 @@ +class Foo { + class Bar { + public void baz() { + } + } +} diff --git a/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/FooImpl.java b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/FooImpl.java new file mode 100644 index 000000000000..089fd687e48a --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideInheritor/src/FooImpl.java @@ -0,0 +1,5 @@ +class FooImpl extends Foo { + void m(Bar b) { + b.baz(); + } +} diff --git a/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/expected.xml b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/expected.xml new file mode 100644 index 000000000000..2e7451e9f0b0 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/expected.xml @@ -0,0 +1,9 @@ + + + + Foo.java + 3 + Can be package-private + + + diff --git a/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/src/Foo.java b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/src/Foo.java new file mode 100644 index 000000000000..5fefef133e38 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/innerClassMethodUsedInsideOtherInnerClassInheritor/src/Foo.java @@ -0,0 +1,13 @@ +class Foo { + private abstract class A { + public int getValue() { + return 20173; + } + } + + private class B extends A { + private B() { + int a = this.getValue(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/expected.xml b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/expected.xml new file mode 100644 index 000000000000..a1ac769c18f3 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/expected.xml @@ -0,0 +1,9 @@ + + + + Foo.java + 2 + Can be package-private + + + diff --git a/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/Foo.java b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/Foo.java new file mode 100644 index 000000000000..20a595569e64 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/Foo.java @@ -0,0 +1,5 @@ +class Foo { + public void boo() { + + } +} diff --git a/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/FooImpl.java b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/FooImpl.java new file mode 100644 index 000000000000..a8747bb0f869 --- /dev/null +++ b/java/java-tests/testData/inspection/visibility/methodUsedInInheritorInnerClass/src/FooImpl.java @@ -0,0 +1,7 @@ +class FooImpl extends Foo { + private class Bar { + void m() { + boo(); + } + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/VisibilityInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/VisibilityInspectionTest.java index 969419664d48..af8d73c37973 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/VisibilityInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/VisibilityInspectionTest.java @@ -153,6 +153,28 @@ public class VisibilityInspectionTest extends InspectionTestCase { doTest("visibility/usedFromAnotherPackage", myTool, false, true); } + // IDEA-175921 + public void testInnerClassMethodUsedInsideOtherInnerClassInheritor() { + myTool.SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = true; + myTool.SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = true; + myTool.SUGGEST_PRIVATE_FOR_INNERS = true; + doTest("visibility/innerClassMethodUsedInsideOtherInnerClassInheritor", myTool, false, true); + } + + public void testMethodUsedInInheritorInnerClass() { + myTool.SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = true; + myTool.SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = true; + myTool.SUGGEST_PRIVATE_FOR_INNERS = true; + doTest("visibility/methodUsedInInheritorInnerClass", myTool, false, true); + } + + public void testInnerClassMethodUsedInsideInheritor() { + myTool.SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = true; + myTool.SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = true; + myTool.SUGGEST_PRIVATE_FOR_INNERS = true; + doTest("visibility/innerClassMethodUsedInsideInheritor", myTool, false, true); + } + public void testEntryPointWithPredefinedVisibility() throws Exception { PlatformTestUtil.registerExtension(Extensions.getRootArea(), ExtensionPointName.create(ToolExtensionPoints.DEAD_CODE_TOOL), new EntryPointWithVisibilityLevel() { @Override