From eea03864da29e20dc15dbd4c1c65cd24cfd7c63c Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 21 Dec 2017 12:36:07 +0100 Subject: [PATCH] IG: add option to ignore accesses from inner classes (IDEA-184055) --- ...therObjectsPrivateFieldInspectionBase.java | 46 +++++++++++-------- .../UseOfAnotherObjectsPrivateField.html | 1 + ...fAnotherObjectsPrivateFieldInspection.java | 16 ------- .../UseOfAnotherObjectsPrivateField.java | 33 +++++++++++++ ...therObjectsPrivateFieldInspectionTest.java | 27 +++++++++++ 5 files changed, 87 insertions(+), 36 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/UseOfAnotherObjectsPrivateFieldInspection.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/use_of_another_objects_private_field/UseOfAnotherObjectsPrivateField.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionBase.java index 62e57feacb00..9efd2dcd79d1 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2017 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,21 +16,26 @@ package com.siyeh.ig.encapsulation; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; +import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.psiutils.MethodUtils; +import org.jdom.Element; import org.jetbrains.annotations.NotNull; import javax.swing.*; -public class UseOfAnotherObjectsPrivateFieldInspectionBase - extends BaseInspection { +public class UseOfAnotherObjectsPrivateFieldInspectionBase extends BaseInspection { @SuppressWarnings({"PublicField"}) public boolean ignoreSameClass = false; + + @SuppressWarnings({"PublicField"}) + public boolean ignoreInnerClasses = false; + @SuppressWarnings({"PublicField"}) public boolean ignoreEquals = false; @@ -56,34 +61,36 @@ public class UseOfAnotherObjectsPrivateFieldInspectionBase @Override public JComponent createOptionsPanel() { - final MultipleCheckboxOptionsPanel panel = - new MultipleCheckboxOptionsPanel(this); - panel.addCheckbox(InspectionGadgetsBundle.message( - "ignore.accesses.from.the.same.class"), "ignoreSameClass"); - panel.addCheckbox(InspectionGadgetsBundle.message( - "ignore.accesses.from.equals.method"), "ignoreEquals"); + final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + final JCheckBox box = panel.addCheckboxEx(InspectionGadgetsBundle.message("ignore.accesses.from.the.same.class"), "ignoreSameClass"); + panel.addDependentCheckBox("Ignore accesses from inner classes", "ignoreInnerClasses", box); + panel.addCheckbox(InspectionGadgetsBundle.message("ignore.accesses.from.equals.method"), "ignoreEquals"); return panel; } + @Override + public void writeSettings(@NotNull Element node) throws WriteExternalException { + // this inspection uses old style serialization, make sure newly introduced setting field does not change profile. + defaultWriteSettings(node, "ignoreInnerClasses"); + writeBooleanOption(node, "ignoreInnerClasses", false); + } + @Override public BaseInspectionVisitor buildVisitor() { return new UseOfAnotherObjectsPrivateFieldVisitor(); } - private class UseOfAnotherObjectsPrivateFieldVisitor - extends BaseInspectionVisitor { + private class UseOfAnotherObjectsPrivateFieldVisitor extends BaseInspectionVisitor { @Override - public void visitReferenceExpression( - @NotNull PsiReferenceExpression expression) { + public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final PsiExpression qualifier = expression.getQualifierExpression(); if (qualifier == null || qualifier instanceof PsiThisExpression) { return; } if (ignoreEquals) { - final PsiMethod method = - PsiTreeUtil.getParentOfType(expression, PsiMethod.class); + final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class); if (MethodUtils.isEquals(method)) { return; } @@ -94,10 +101,10 @@ public class UseOfAnotherObjectsPrivateFieldInspectionBase } final PsiField field = (PsiField)referent; if (ignoreSameClass) { - final PsiClass parent = - PsiTreeUtil.getParentOfType(expression, PsiClass.class); + final PsiClass parent = PsiTreeUtil.getParentOfType(expression, PsiClass.class); final PsiClass containingClass = field.getContainingClass(); - if (parent != null && parent.equals(containingClass)) { + if (parent != null && (parent.equals(containingClass) || + ignoreInnerClasses && PsiTreeUtil.isAncestor(containingClass, parent, true))) { return; } } @@ -108,8 +115,7 @@ public class UseOfAnotherObjectsPrivateFieldInspectionBase if (field.hasModifierProperty(PsiModifier.STATIC)) { return; } - final PsiElement fieldNameElement = - expression.getReferenceNameElement(); + final PsiElement fieldNameElement = expression.getReferenceNameElement(); if (fieldNameElement == null) { return; } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UseOfAnotherObjectsPrivateField.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UseOfAnotherObjectsPrivateField.html index 6e532ba64010..4b721669eab7 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UseOfAnotherObjectsPrivateField.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UseOfAnotherObjectsPrivateField.html @@ -10,6 +10,7 @@ using proxies.

Use the first checkbox below to ignore accesses from the same class and only report accesses from inner or outer classes. + To ignore accesses from inner classes also, use the nested checkbox.

Use the second checkbox below to ignore accesses from an equals() method.

diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/UseOfAnotherObjectsPrivateFieldInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/UseOfAnotherObjectsPrivateFieldInspection.java deleted file mode 100644 index fa2ef949c267..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/UseOfAnotherObjectsPrivateFieldInspection.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.siyeh.igtest.encapsulation; - -public class UseOfAnotherObjectsPrivateFieldInspection { - public int foo; - protected int bar; - private int baz; - - public void fooBar(UseOfAnotherObjectsPrivateFieldInspection copy) - { - foo = copy.foo; - bar = copy.bar; - baz = copy.baz; - foo = this.baz; - foo = baz; - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/use_of_another_objects_private_field/UseOfAnotherObjectsPrivateField.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/use_of_another_objects_private_field/UseOfAnotherObjectsPrivateField.java new file mode 100644 index 000000000000..5c0f02e005a3 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/encapsulation/use_of_another_objects_private_field/UseOfAnotherObjectsPrivateField.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.siyeh.igtest.encapsulation; + +public class UseOfAnotherObjectsPrivateField { + public int foo; + protected int bar; + private int baz; + + public void fooBar(UseOfAnotherObjectsPrivateField copy) + { + foo = copy.foo; + bar = copy.bar; + baz = copy.baz; + foo = this.baz; + foo = baz; + } + + class Inside { + + int f(UseOfAnotherObjectsPrivateField pugnacious) { + return pugnacious.baz; + } + } +} +class SomewhereElse { + + void m(UseOfAnotherObjectsPrivateField tenacious) { + tenacious.bar = 1; + tenacious.foo = 2; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionTest.java new file mode 100644 index 000000000000..7e74532e3b37 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/encapsulation/UseOfAnotherObjectsPrivateFieldInspectionTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.siyeh.ig.encapsulation; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class UseOfAnotherObjectsPrivateFieldInspectionTest extends LightInspectionTestCase { + + public void testUseOfAnotherObjectsPrivateField() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + final UseOfAnotherObjectsPrivateFieldInspection inspection = new UseOfAnotherObjectsPrivateFieldInspection(); + inspection.ignoreSameClass = true; + inspection.ignoreInnerClasses = true; + return inspection; + } +}