require that notnull fields be initialized (IDEA-114889)

This commit is contained in:
peter
2014-07-18 09:14:08 +02:00
parent 5a22fdcf73
commit df68af8efe
4 changed files with 23 additions and 3 deletions
@@ -154,7 +154,17 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo
}
}
for (PsiExpression rhs : DfaPsiUtil.findAllConstructorInitializers(field)) {
List<PsiExpression> initializers = DfaPsiUtil.findAllConstructorInitializers(field);
if (annotated.isDeclaredNotNull && initializers.isEmpty()) {
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);
}
}
for (PsiExpression rhs : initializers) {
if (rhs instanceof PsiReferenceExpression) {
PsiElement target = ((PsiReferenceExpression)rhs).resolve();
if (target instanceof PsiParameter) {
@@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull;
class B {
@NotNull
B b;
B b = new B();
public B <warning descr="Getter for @NotNull field might be annotated @NotNull itself">getB</warning>() {
return b;
@@ -39,7 +39,7 @@ class C {
this.c = c;
}
@NotNull C c1;
@NotNull C c1 = new C(null);
@org.jetbrains.annotations.Nullable
public C getC1() {
if (c1 != null) {
@@ -0,0 +1,9 @@
import org.jetbrains.annotations.*;
class Test {
<warning descr="Not-null fields must be initialized">@NotNull</warning> Object member;
private void accessMember() {
member = new Object();
}
}
@@ -31,6 +31,7 @@ public class NullableStuffInspectionTest extends LightCodeInsightFixtureTestCase
public void testNullableFieldNotnullParam() throws Exception{ doTest(); }
public void testNotNullFieldNullableParam() throws Exception{ doTest(); }
public void testNotNullCustomException() throws Exception{ doTest(); }
public void testNotNullFieldNotInitialized() throws Exception{ doTest(); }
public void testGetterSetterProblems() throws Exception{ doTest(); }
public void testOverriddenMethods() throws Exception{