respect "do not suggest private for inners" in case of anonymous class

This commit is contained in:
Alexey Kudravtsev
2015-12-23 16:57:38 +03:00
parent 0cc76b24ce
commit ec51643dff
3 changed files with 27 additions and 4 deletions
@@ -225,7 +225,8 @@ class AccessCanBeTightenedInspection extends BaseJavaBatchLocalInspectionTool {
if (annotation != null && annotation.getParent() instanceof PsiModifierList && annotation.getParent().getParent() == aClass) {
return suggestPackageLocal(member);
}
return myVisibilityInspection.SUGGEST_PRIVATE_FOR_INNERS || memberClass == aClass ? PsiUtil.ACCESS_LEVEL_PRIVATE : suggestPackageLocal(member);
return myVisibilityInspection.SUGGEST_PRIVATE_FOR_INNERS ||
memberClass == aClass && memberClass.getContainingClass() == null ? PsiUtil.ACCESS_LEVEL_PRIVATE : suggestPackageLocal(member);
}
//if (file == memberFile) {
// return PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL;
@@ -367,7 +367,9 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
if (fromTopLevelElement != null && isInExtendsList(to, fromTopLevelElement.getExtendsList())) return false;
if (fromTopLevelElement != null && isInExtendsList(to, fromTopLevelElement.getImplementsList())) return false;
if (fromTopLevelElement != null && isInAnnotations(to, fromTopLevelElement)) return false;
return fromTopLevel == toOwner || fromOwner == toTopLevel || toOwner != null && refUtil.getOwnerClass(toOwner) == from;
return fromTopLevel == toOwner || fromOwner == toTopLevel || toOwner != null && (
refUtil.getOwnerClass(toOwner) == from || from instanceof RefMethod && toOwner == ((RefMethod)from).getOwnerClass() ||
from instanceof RefField && toOwner == ((RefField)from).getOwnerClass());
}
if (fromOwner != null && fromOwner.isStatic() && !to.isStatic() && refUtil.isInheritor(fromOwner, toOwner)) return false;
@@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.visibility;
package com.intellij.codeInspection.visibility;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.visibility.VisibilityInspection;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.util.ReflectionUtil;
import com.siyeh.ig.LightInspectionTestCase;
public class AccessCanBeTightenedInspectionTest extends LightInspectionTestCase {
@@ -88,6 +90,24 @@ public class AccessCanBeTightenedInspectionTest extends LightInspectionTestCase
myFixture.checkHighlighting();
}
public void testDoNotSuggestPrivateInAnonymousClassIfPrivatesForInnersIsOff() {
InspectionProfileImpl profile = (InspectionProfileImpl)InspectionProjectProfileManager.getInstance(getProject()).getInspectionProfile();
AccessCanBeTightenedInspection inspection = (AccessCanBeTightenedInspection)profile.getInspectionTool(VisibilityInspection.SHORT_NAME, getProject()).getTool();
VisibilityInspection visibilityInspection =
ReflectionUtil.getField(inspection.getClass(), inspection, VisibilityInspection.class, "myVisibilityInspection");
visibilityInspection.SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = false;
doTest("class C {\n" +
" {\n" +
" new Runnable() {\n" +
" @Override\n" +
" public void run() {}\n"+
" boolean isVisible() { return true; }\n" +
" }.run();\n" +
" }\n"+
"}");
}
@Override
protected LocalInspectionTool getInspection() {
VisibilityInspection inspection = new VisibilityInspection();