diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionBase.java index 148ac0b10ca2..f7b88003a695 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2015 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. @@ -16,26 +16,43 @@ package com.siyeh.ig.visibility; import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiField; +import com.intellij.psi.PsiModifier; import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.psiutils.ClassUtils; +import org.jdom.Element; import org.jetbrains.annotations.NotNull; import javax.swing.*; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class FieldHidesSuperclassFieldInspectionBase extends BaseInspection { - /** - * @noinspection PublicField - */ + @SuppressWarnings("PublicField") public boolean m_ignoreInvisibleFields = true; + @SuppressWarnings("PublicField") + public boolean ignoreStaticFields = true; + + @Override + public void writeSettings(@NotNull Element node) throws WriteExternalException { + super.writeSettings(node); + for (Element child : new ArrayList(node.getChildren())) { + final String name = child.getAttributeValue("name"); + final String value = child.getAttributeValue("value"); + if ("ignoreStaticFields".equals(name) && "true".equals(value)) { + node.removeContent(child); + } + } + } + @Override @NotNull public String getID() { @@ -73,8 +90,7 @@ public class FieldHidesSuperclassFieldInspectionBase extends BaseInspection { return new FieldHidesSuperclassFieldVisitor(); } - private class FieldHidesSuperclassFieldVisitor - extends BaseInspectionVisitor { + private class FieldHidesSuperclassFieldVisitor extends BaseInspectionVisitor { @Override public void visitField(@NotNull PsiField field) { @@ -92,16 +108,20 @@ public class FieldHidesSuperclassFieldInspectionBase extends BaseInspection { if (!visitedClasses.add(ancestorClass)) { return; } - final PsiField ancestorField = - ancestorClass.findFieldByName(fieldName, false); - if (ancestorField != null) { - if (!m_ignoreInvisibleFields || - ClassUtils.isFieldVisible(ancestorField, aClass)) { - registerFieldError(field); - return; - } - } + final PsiField ancestorField = ancestorClass.findFieldByName(fieldName, false); ancestorClass = ancestorClass.getSuperClass(); + if (ancestorField == null) { + continue; + } + if (m_ignoreInvisibleFields && !ClassUtils.isFieldVisible(ancestorField, aClass)) { + continue; + } + if (ignoreStaticFields && field.hasModifierProperty(PsiModifier.STATIC) && + ancestorField.hasModifierProperty(PsiModifier.STATIC)) { + continue; + } + registerFieldError(field); + return; } } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/FieldHidesSuperclassField.html b/plugins/InspectionGadgets/src/inspectionDescriptions/FieldHidesSuperclassField.html index b0eeae140fbe..2e7b859838d9 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/FieldHidesSuperclassField.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/FieldHidesSuperclassField.html @@ -4,9 +4,11 @@ Reports fields with the same name as a field in an ancestor class. Such field names may be confusing, and can be bug-prone.

-Use the checkbox below the indicate whether this inspection should report all name clashes, or only +Use the first checkbox below to indicate whether this inspection should report all name clashes, or only clashes with fields which are visible from the subclass.

+Use the second checkbox below to ignore static fields which hide static fields in super classes. +

\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/field_hides_superclass_field/FieldHidesSuperclassField.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/field_hides_superclass_field/FieldHidesSuperclassField.java new file mode 100644 index 000000000000..5320e6746d44 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/visibility/field_hides_superclass_field/FieldHidesSuperclassField.java @@ -0,0 +1,13 @@ +class FieldHidesSuperclassField { + String s; + static String S; + +} +class Sub extends FieldHidesSuperclassField { + static String s; + static String S; +} +class Sub2 extends FieldHidesSuperclassField { + String a; + String s; +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionTest.java new file mode 100644 index 000000000000..830fb5260bb9 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/visibility/FieldHidesSuperclassFieldInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2015 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.visibility; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class FieldHidesSuperclassFieldInspectionTest extends LightInspectionTestCase { + + public void testFieldHidesSuperclassField() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new FieldHidesSuperclassFieldInspection(); + } +}