dfa: overriding @NotNull has precedence over super method @Nullable

This commit is contained in:
peter
2015-04-13 18:51:22 +03:00
parent 3583e28000
commit ecab324ae0
4 changed files with 40 additions and 6 deletions
@@ -169,13 +169,16 @@ public abstract class NullableNotNullManager implements PersistentStateComponent
@Nullable
private PsiAnnotation findNullabilityAnnotationWithDefault(@NotNull PsiModifierListOwner owner, boolean checkBases, boolean nullable) {
PsiAnnotation annotation = findPlainNullabilityAnnotation(owner, checkBases, nullable);
PsiAnnotation annotation = findPlainNullabilityAnnotation(owner, checkBases);
if (annotation != null) {
String qName = annotation.getQualifiedName();
if (qName == null) return null;
List<String> contradictory = nullable ? getNotNulls() : getNullables();
if (contradictory.contains(qName)) return null;
return annotation;
}
if (findPlainNullabilityAnnotation(owner, checkBases, !nullable) != null) {
return null;
}
PsiType type = getOwnerType(owner);
if (type == null || TypeConversionUtil.isPrimitiveAndNotNull(type)) return null;
@@ -192,8 +195,9 @@ public abstract class NullableNotNullManager implements PersistentStateComponent
return findNullabilityDefaultInHierarchy(owner, nullable);
}
private PsiAnnotation findPlainNullabilityAnnotation(@NotNull PsiModifierListOwner owner, boolean checkBases, boolean nullable) {
Set<String> qNames = ContainerUtil.newHashSet(nullable ? getNullables() : getNotNulls());
private PsiAnnotation findPlainNullabilityAnnotation(@NotNull PsiModifierListOwner owner, boolean checkBases) {
Set<String> qNames = ContainerUtil.newHashSet(getNullables());
qNames.addAll(getNotNulls());
return checkBases && owner instanceof PsiMethod
? AnnotationUtil.findAnnotationInHierarchy(owner, qNames)
: AnnotationUtil.findAnnotation(owner, qNames);
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.Object;
import java.lang.Override;
class Super {
@Nullable Object foo() {
return null;
}
}
class Main extends Super {
@NotNull
@Override
Object foo() {
return 2;
}
void bar(@NotNull Object o) {}
void goo() {
bar(foo());
}
}
@@ -279,6 +279,8 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
public void testSameComparisonTwice() { doTest(); }
public void testRootThrowableCause() { doTest(); }
public void testNotNullOverridesNullable() { doTest(); }
public void testOverridingInferredNotNullMethod() { doTest(); }
public void testUseInferredContracts() { doTest(); }
public void testContractWithNoArgs() { doTest(); }
@@ -37,6 +37,8 @@ public class DataFlowInspectionTestSuite {
suite.addTestSuite(SliceBackwardTest.class);
suite.addTestSuite(SmartTypeCompletionDfaTest.class);
suite.addTestSuite(NormalCompletionDfaTest.class);
suite.addTestSuite(NullableStuffInspectionTest.class);
suite.addTestSuite(NullableStuffInspection14Test.class);
return suite;
}
}