From a943715bd5094d7dc18fb570f5d543a5b30e41aa Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 23 Nov 2018 13:22:56 +0100 Subject: [PATCH] NullableStuffInspection: report only property accessors that refer to correctly named fields (IDEA-202754) --- .../nullable/NullableStuffInspectionBase.java | 4 +-- .../GetterSetterFieldMismatch.java | 34 +++++++++++++++++++ .../NullableStuffInspectionTest.java | 3 +- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/inspection/nullableProblems/GetterSetterFieldMismatch.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java index ba9679377547..9fbf41fb76f4 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java @@ -448,7 +448,7 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection final PsiMethod getter = PropertyUtilBase.findPropertyGetter(field.getContainingClass(), propName, isStatic, false); final PsiIdentifier nameIdentifier = getter == null ? null : getter.getNameIdentifier(); if (nameIdentifier != null && nameIdentifier.isPhysical()) { - if (PropertyUtil.isSimpleGetter(getter)) { + if (PropertyUtil.getFieldOfGetter(getter) == field) { AnnotateMethodFix getterAnnoFix = new AnnotateMethodFix(anno, ArrayUtil.toStringArray(annoToRemove)); if (REPORT_NOT_ANNOTATED_GETTER) { if (!manager.hasNullability(getter) && !TypeConversionUtil.isPrimitiveAndNotNull(getter.getReturnType())) { @@ -468,7 +468,7 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection final PsiClass containingClass = field.getContainingClass(); final PsiMethod setter = PropertyUtilBase.findPropertySetter(containingClass, propName, isStatic, false); - if (setter != null && setter.isPhysical() && PropertyUtil.isSimpleSetter(setter)) { + if (setter != null && setter.isPhysical() && PropertyUtil.getFieldOfSetter(setter) == field) { final PsiParameter[] parameters = setter.getParameterList().getParameters(); assert parameters.length == 1 : setter.getText(); final PsiParameter parameter = parameters[0]; diff --git a/java/java-tests/testData/inspection/nullableProblems/GetterSetterFieldMismatch.java b/java/java-tests/testData/inspection/nullableProblems/GetterSetterFieldMismatch.java new file mode 100644 index 000000000000..0b7a72ef3ea3 --- /dev/null +++ b/java/java-tests/testData/inspection/nullableProblems/GetterSetterFieldMismatch.java @@ -0,0 +1,34 @@ +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +class NotNullGetterTest { + + @NotNull + private final Object foo; + + @Nullable + private Object bar; + + public NotNullGetterTest(@NotNull Object foo, @Nullable Object bar) { + this.foo = foo; + this.bar = bar; + } + + @NotNull + public Object getFoo() { + return foo; + } + + @NotNull + public Object getBar() { + return foo; + } + + void setBar(@NotNull Object bar) { + this.bar = bar; + } + + void setFoo(@NotNull Object bar) { + this.bar = bar; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java index c1ef642f8e30..a5235b191d92 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java @@ -93,7 +93,8 @@ public class NullableStuffInspectionTest extends LightCodeInsightFixtureTestCase public void testGetterSetterProblems() { doTest(); } public void testNonTrivialGettersSetters() { doTest(); } - + public void testGetterSetterFieldMismatch() { doTest(); } + public void testOverriddenMethods() { myInspection.REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = true; doTest();