IDEA-119415 (Inspection for Static field/method referenced via subclass should ignore fields/methods in non accessible base class.)

This commit is contained in:
Bas Leijdekkers
2014-02-02 19:05:32 +01:00
parent 1c305437b6
commit d4ff7e2ed6
5 changed files with 64 additions and 47 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@ import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jetbrains.annotations.NotNull;
public class StaticCallOnSubclassInspection extends BaseInspection {
@@ -141,10 +140,8 @@ public class StaticCallOnSubclassInspection extends BaseInspection {
if (declaringClass.equals(referencedClass)) {
return;
}
final PsiClass containingClass =
ClassUtils.getContainingClass(call);
if (!ClassUtils.isClassVisibleFromClass(containingClass,
declaringClass)) {
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(call.getProject()).getResolveHelper();
if (!resolveHelper.isAccessible(declaringClass, call, null)) {
return;
}
registerMethodCallError(call, declaringClass, referencedClass);
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@ import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jetbrains.annotations.NotNull;
public class StaticFieldReferenceOnSubclassInspection
@@ -124,13 +123,14 @@ public class StaticFieldReferenceOnSubclassInspection
if (declaringClass.equals(referencedClass)) {
return;
}
final PsiClass containingClass =
ClassUtils.getContainingClass(expression);
if (!ClassUtils.isClassVisibleFromClass(containingClass,
declaringClass)) {
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper();
if (!resolveHelper.isAccessible(declaringClass, expression, null)) {
return;
}
final PsiElement identifier = expression.getReferenceNameElement();
if (identifier == null) {
return;
}
registerError(identifier, declaringClass, referencedClass);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -206,21 +206,4 @@ public class ClassUtils {
final PsiClass parentClass = (PsiClass)parent;
return !parentClass.isInterface();
}
public static boolean isClassVisibleFromClass(PsiClass baseClass,
PsiClass referencedClass) {
if (referencedClass.hasModifierProperty(PsiModifier.PUBLIC)) {
return true;
}
else if (referencedClass.hasModifierProperty(PsiModifier.PROTECTED)) {
return inSamePackage(baseClass, referencedClass);
}
else if (referencedClass.hasModifierProperty(PsiModifier.PRIVATE)) {
return PsiTreeUtil.findCommonParent(baseClass, referencedClass) !=
null;
}
else {
return inSamePackage(baseClass, referencedClass);
}
}
}
@@ -1,17 +0,0 @@
package com.siyeh.igtest.bugs;
import com.siyeh.igtest.abstraction.StaticCallOnSubclassChild;
public class StaticCallOnSubclass {
public void foo() throws InterruptedException {
Thread.sleep(1000L);
InnerThread.sleep(1000L, 1000);
final int priority = InnerThread.MAX_PRIORITY;
StaticCallOnSubclassChild.foo();
}
class InnerThread extends Thread
{
}
}
@@ -0,0 +1,54 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.bugs;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class StaticFieldReferenceOnSubclassInspectionTest extends LightInspectionTestCase {
@Override
protected InspectionProfileEntry getInspection() {
return new StaticFieldReferenceOnSubclassInspection();
}
@Override
protected String[] getEnvironmentClasses() {
return new String[] {
"package a;" +
"public class A {" +
" private static class AA {\n" +
" public static final int VALUE = 5;\n" +
" }" +
" public static class AB extends AA {\n" +
" }" +
" public static final String S = \"\";" +
"}",
"package a;" +
"public class B extends A {}"
};
}
public void testSimple() {
doStatementTest("System.out.println(a.B./*Static field 'S' declared in class 'a.A' but referenced via subclass 'a.B'*/S/**/);");
}
public void testNoWarn() {
doStatementTest("System.out.println(a.A.AB.VALUE);");
}
}