visibility inspection: do not suggest private for constants used in class modifier lists and for inner classes used in extends/implements (IDEA-81161)

This commit is contained in:
anna
2012-02-10 16:49:45 +01:00
parent b902b14fc4
commit cb907f4128
4 changed files with 59 additions and 0 deletions
@@ -334,6 +334,9 @@ public class VisibilityInspection extends GlobalJavaInspectionTool {
if (accessModifier == PsiModifier.PRIVATE) {
if (SUGGEST_PRIVATE_FOR_INNERS) {
if (isInExtendsList(to, fromTopLevel.getElement().getExtendsList())) return false;
if (isInExtendsList(to, fromTopLevel.getElement().getImplementsList())) return false;
if (isInAnnotations(to, fromTopLevel)) return false;
return fromTopLevel == toOwner || fromOwner == toTopLevel || toOwner != null && refUtil.getOwnerClass(toOwner) == from;
}
@@ -354,6 +357,24 @@ public class VisibilityInspection extends GlobalJavaInspectionTool {
return false;
}
private static boolean isInAnnotations(final RefJavaElement to, final RefClass fromTopLevel) {
final PsiModifierList modifierList = fromTopLevel.getElement().getModifierList();
if (modifierList == null) return false;
final PsiElement toElement = to.getElement();
final boolean [] resolved = new boolean[] {false};
modifierList.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
if (resolved[0]) return;
super.visitReferenceExpression(expression);
if (expression.resolve() == toElement) {
resolved[0] = true;
}
}
});
return resolved[0];
}
private static boolean isInExtendsList(final RefJavaElement to, final PsiReferenceList extendsList) {
if (extendsList != null) {
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>ThisClass.java</file>
<line>5</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Declaration access can be weaker</problem_class>
<hints>
<hint value="packageLocal" />
</hints>
<description>Can be package local</description>
</problem>
<problem>
<file>ThisClass.java</file>
<line>4</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Declaration access can be weaker</problem_class>
<hints>
<hint value="packageLocal" />
</hints>
<description>Can be package local</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
import java.util.ArrayList;
@SuppressWarnings(ThisClass.PUBLICFINALNAME)
public class ThisClass extends ArrayList<ThisClass.FF> {
public static final String PUBLICFINALNAME = "stuff";
public static class FF {}
public static void main(String[] args) {
}
}
@@ -102,4 +102,11 @@ public class VisibilityInspectionTest extends InspectionTestCase {
myTool.SUGGEST_PRIVATE_FOR_INNERS = false;
doTest("visibility/typeArguments", myTool, false, true);
}
public void testUsedFromAnnotationsExtendsList() throws Exception {
myTool.SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = true;
myTool.SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = true;
myTool.SUGGEST_PRIVATE_FOR_INNERS = true;
doTest("visibility/usedFromAnnotationsExtendsList", myTool, false, true);
}
}