IDEA-147534 Inspection "@NotNull/@Nullable problems" - "Require @NotNull fileds to be initialized explicitly", does not trigger when using @NonnullByDefault

This commit is contained in:
peter
2015-11-06 16:48:33 +01:00
parent cbbe838d32
commit 2b9e895827
3 changed files with 32 additions and 15 deletions
@@ -105,12 +105,12 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo
checkAccessors(field, annotated, project, manager, anno, annoToRemove, holder);
if (REQUIRE_NOTNULL_FIELDS_INITIALIZED) {
checkNotNullFieldsInitialized(field, annotated, manager, holder);
}
checkConstructorParameters(field, annotated, manager, anno, annoToRemove, holder);
}
if (REQUIRE_NOTNULL_FIELDS_INITIALIZED && !annotated.isDeclaredNullable) {
checkNotNullFieldsInitialized(field, manager, holder);
}
}
@Override
@@ -259,17 +259,14 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo
LOG.assertTrue(parameter.isPhysical(), setter.getText());
}
private static void checkNotNullFieldsInitialized(PsiField field,
Annotated annotated,
NullableNotNullManager manager, @NotNull ProblemsHolder holder) {
if (annotated.isDeclaredNotNull && !HighlightControlFlowUtil.isFieldInitializedAfterObjectConstruction(field)) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(field, manager.getNotNulls());
if (annotation != null) {
holder.registerProblem(annotation.isPhysical() ? annotation : field.getNameIdentifier(),
"Not-null fields must be initialized",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
private static void checkNotNullFieldsInitialized(PsiField field, NullableNotNullManager manager, @NotNull ProblemsHolder holder) {
PsiAnnotation annotation = manager.getNotNullAnnotation(field, false);
if (annotation == null || HighlightControlFlowUtil.isFieldInitializedAfterObjectConstruction(field)) return;
boolean byDefault = manager.isContainerAnnotation(annotation);
PsiJavaCodeReferenceElement name = annotation.getNameReferenceElement();
holder.registerProblem(annotation.isPhysical() && !byDefault ? annotation : field.getNameIdentifier(),
(byDefault && name != null ? "@" + name.getReferenceName() : "Not-null") + " fields must be initialized");
}
private void checkConstructorParameters(PsiField field,
@@ -0,0 +1,15 @@
import javax.annotation.*;
@NonnullByDefault
class Test {
Object <warning descr="@NonnullByDefault fields must be initialized">member</warning>;
private void accessMember() {
member = new Object();
}
}
@Nonnull
@javax.annotation.meta.TypeQualifierDefault(java.lang.annotation.ElementType.FIELD)
@interface NonnullByDefault {}
@@ -59,6 +59,11 @@ public class NullableStuffInspectionTest extends LightCodeInsightFixtureTestCase
doTest();
}
public void testNotNullByDefaultFieldNotInitialized() {
DataFlowInspectionTest.addJavaxNullabilityAnnotations(myFixture);
doTest();
}
public void testNotNullAnnotationChecksInChildClassMethods() { doTest(); }
public void testGetterSetterProblems() throws Exception{ doTest(); }