mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-138531 (Inspection "Field name hides field in superclass" should optionally ignore static fields.)
This commit is contained in:
+35
-15
@@ -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<Element>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
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.
|
||||
<p>
|
||||
Use the second checkbox below to ignore <b>static</b> fields which hide <b>static</b> fields in super classes.
|
||||
<p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class FieldHidesSuperclassField {
|
||||
String s;
|
||||
static String S;
|
||||
|
||||
}
|
||||
class Sub extends FieldHidesSuperclassField {
|
||||
static String <warning descr="Field 's' hides field in superclass">s</warning>;
|
||||
static String S;
|
||||
}
|
||||
class Sub2 extends FieldHidesSuperclassField {
|
||||
String a;
|
||||
String <warning descr="Field 's' hides field in superclass">s</warning>;
|
||||
}
|
||||
+36
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user