From afb11843f488b864d6f3e0e4316b34bc36396ae2 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 6 Feb 2015 15:51:47 +0100 Subject: [PATCH] =?UTF-8?q?IDEA-135004=20New=20=E2=80=9Cnon=20null=20field?= =?UTF-8?q?s=20must=20be=20initialised=E2=80=9D=20not=20helpful=20when=20D?= =?UTF-8?q?I=20is=20used?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nullable/NullableStuffInspectionBase.java | 18 +++++++++++------- .../nullable/NullableStuffInspection.java | 4 ++++ .../codeInspection/nullable/OptionsPanel.form | 16 ++++++++++++---- .../NotNullFieldNotInitializedSetting.java | 9 +++++++++ .../NullableStuffInspectionTest.java | 6 ++++++ 5 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 java/java-tests/testData/inspection/nullableProblems/NotNullFieldNotInitializedSetting.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 cb2c3fda0b68..bda9f4be9581 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 @@ -53,6 +53,7 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo @Deprecated @SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOT_ANNOTATED_PARAMETER_OVERRIDES_NOTNULL = true; @SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOT_ANNOTATED_GETTER = true; @SuppressWarnings({"WeakerAccess"}) public boolean IGNORE_EXTERNAL_SUPER_NOTNULL = false; + @SuppressWarnings({"WeakerAccess"}) public boolean REQUIRE_NOTNULL_FIELDS_INITIALIZED = true; @SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED = false; @Deprecated @SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOT_ANNOTATED_SETTER_PARAMETER = true; @Deprecated @SuppressWarnings({"WeakerAccess"}) public boolean REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = true; // remains for test @@ -67,7 +68,8 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo String name = child.getAttributeValue("name"); String value = child.getAttributeValue("value"); if ("IGNORE_EXTERNAL_SUPER_NOTNULL".equals(name) && "false".equals(value) || - "REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED".equals(name) && "false".equals(value)) { + "REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED".equals(name) && "false".equals(value) || + "REQUIRE_NOTNULL_FIELDS_INITIALIZED".equals(name) && "true".equals(value)) { node.removeContent(child); } } @@ -178,12 +180,14 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo } List 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); + if (REQUIRE_NOTNULL_FIELDS_INITIALIZED) { + 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); + } } } diff --git a/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java b/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java index b860a937d412..125bb6a2c11b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java @@ -40,6 +40,7 @@ public class NullableStuffInspection extends NullableStuffInspectionBase { private JButton myConfigureAnnotationsButton; private JCheckBox myIgnoreExternalSuperNotNull; private JCheckBox myNNParameterOverridesNA; + private JCheckBox myRequireNNFieldsInitialized; private OptionsPanel() { super(new BorderLayout()); @@ -56,6 +57,7 @@ public class NullableStuffInspection extends NullableStuffInspectionBase { myNNParameterOverridesNA.addActionListener(actionListener); myReportNotAnnotatedGetter.addActionListener(actionListener); myIgnoreExternalSuperNotNull.addActionListener(actionListener); + myRequireNNFieldsInitialized.addActionListener(actionListener); myConfigureAnnotationsButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -74,6 +76,7 @@ public class NullableStuffInspection extends NullableStuffInspectionBase { myReportNotAnnotatedGetter.setSelected(REPORT_NOT_ANNOTATED_GETTER); myIgnoreExternalSuperNotNull.setSelected(IGNORE_EXTERNAL_SUPER_NOTNULL); myNNParameterOverridesNA.setSelected(REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED); + myRequireNNFieldsInitialized.setSelected(REQUIRE_NOTNULL_FIELDS_INITIALIZED); myIgnoreExternalSuperNotNull.setEnabled(myNAMethodOverridesNN.isSelected()); } @@ -84,6 +87,7 @@ public class NullableStuffInspection extends NullableStuffInspectionBase { REPORT_NOT_ANNOTATED_GETTER = myReportNotAnnotatedGetter.isSelected(); IGNORE_EXTERNAL_SUPER_NOTNULL = myIgnoreExternalSuperNotNull.isSelected(); REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED = myNNParameterOverridesNA.isSelected(); + REQUIRE_NOTNULL_FIELDS_INITIALIZED = myRequireNNFieldsInitialized.isSelected(); REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = REPORT_NOT_ANNOTATED_METHOD_OVERRIDES_NOTNULL; myIgnoreExternalSuperNotNull.setEnabled(myNAMethodOverridesNN.isSelected()); diff --git a/java/java-impl/src/com/intellij/codeInspection/nullable/OptionsPanel.form b/java/java-impl/src/com/intellij/codeInspection/nullable/OptionsPanel.form index 34c5535fa6fd..c9d38f447b36 100644 --- a/java/java-impl/src/com/intellij/codeInspection/nullable/OptionsPanel.form +++ b/java/java-impl/src/com/intellij/codeInspection/nullable/OptionsPanel.form @@ -1,16 +1,16 @@
- + - + - + @@ -39,7 +39,7 @@ - + @@ -61,6 +61,14 @@ + + + + + + + + diff --git a/java/java-tests/testData/inspection/nullableProblems/NotNullFieldNotInitializedSetting.java b/java/java-tests/testData/inspection/nullableProblems/NotNullFieldNotInitializedSetting.java new file mode 100644 index 000000000000..5513b01408d9 --- /dev/null +++ b/java/java-tests/testData/inspection/nullableProblems/NotNullFieldNotInitializedSetting.java @@ -0,0 +1,9 @@ +import org.jetbrains.annotations.*; + +class Test { + @NotNull Object member; + + private void accessMember() { + member = new Object(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionTest.java index 0b8e6c086b16..40302703f045 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionTest.java @@ -48,7 +48,13 @@ 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 testNotNullFieldNotInitializedSetting() { + myInspection.REQUIRE_NOTNULL_FIELDS_INITIALIZED = false; + doTest(); + } + public void testNotNullAnnotationChecksInChildClassMethods() { doTest(); } public void testGetterSetterProblems() throws Exception{ doTest(); }