access visibility inspection: fix global inspection in case of inner classes IDEA-175921

This commit is contained in:
Dmitry Batkovich
2017-07-18 12:19:02 +03:00
parent 531c5d36da
commit 86977a23ec
10 changed files with 88 additions and 7 deletions
@@ -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) {
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Foo.java</file>
<line>3</line>
<description>Can be package-private</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class Foo {
class Bar {
public void baz() {
}
}
}
@@ -0,0 +1,5 @@
class FooImpl extends Foo {
void m(Bar b) {
b.baz();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Foo.java</file>
<line>3</line>
<description>Can be package-private</description>
</problem>
</problems>
@@ -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();
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Foo.java</file>
<line>2</line>
<description>Can be package-private</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
class Foo {
public void boo() {
}
}
@@ -0,0 +1,7 @@
class FooImpl extends Foo {
private class Bar {
void m() {
boo();
}
}
}
@@ -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