NullableStuffInspection: report only property accessors that refer to correctly named fields (IDEA-202754)

This commit is contained in:
peter
2018-11-23 13:23:30 +01:00
parent 2cd43e3a79
commit a943715bd5
3 changed files with 38 additions and 3 deletions
@@ -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];
@@ -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;
}
}
@@ -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();