From 95e928eeff55163786b62e3421aa1219b9b2d971 Mon Sep 17 00:00:00 2001 From: Vladimir Danilov Date: Mon, 11 Apr 2022 13:49:27 +0300 Subject: [PATCH] Add option to allow returning nullable from methods in Jsr305AnnotationCheck #887 --- .../checks/coding/Jsr305AnnotationsCheck.java | 16 +++++++++- .../coding/Jsr305AnnotationsCheckTest.java | 30 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.java index cbff217ddb..0d589e20fd 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.java @@ -329,6 +329,8 @@ private enum NullnessAnnotation { private boolean allowOverridingReturnValue; /** Parameter: overriding parameter annotations allowed. */ private boolean allowOverridingParameter; + /** Parameter: allow nullable return value. */ + private boolean allowNullableReturnValue; /** State, is a package excluded. */ private boolean packageExcluded; @@ -415,6 +417,16 @@ public void setAllowOverridingParameter(final boolean newAllowOverridingParamete allowOverridingParameter = newAllowOverridingParameter; } + /** + * Sets the property for allowing nullable return values. + * + * @param newAllowNullableReturnValue + * true if yes + */ + public void setAllowNullableReturnValue(final boolean newAllowNullableReturnValue) { + allowNullableReturnValue = newAllowNullableReturnValue; + } + /** * Maps annotations to their respective names. * @@ -771,8 +783,10 @@ protected MethodJsr305Handler(final DetailAST ast) { protected void runReturnAnnotationHandler() { checkContainsAny(MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT, NullnessAnnotation.RETURN_VALUES_ARE_NONNULL_BY_DEFAULT); - checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE, + if (!allowNullableReturnValue) { + checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE, NullnessAnnotation.NULLABLE); + } checkContainsAll(MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, NullnessAnnotation.NONNULL, NullnessAnnotation.CHECK_FOR_NULL); checkContainsAll(MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE, diff --git a/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheckTest.java b/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheckTest.java index 6bbe1c01a8..6b4ee747b5 100644 --- a/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheckTest.java +++ b/sevntu-checks/src/test/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheckTest.java @@ -407,4 +407,34 @@ public void testNestedAnnotations() throws Exception { expected); } + @Test + public void testAllowNullableReturn() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(Jsr305AnnotationsCheck.class); + checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); + checkConfig.addAttribute("allowNullableReturnValue", "true"); + + final String[] expected = { + "51:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, "e"), + "69:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), + "75:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), + "92:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"), + "95:26: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), + "95:48: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), + "99:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE, "e"), + "105:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_VOID_WITH_CHECK_RETURN_VALUE_ANNOTATION, "e"), + "110:5: " + getCheckMessage( + Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), + }; + + verify(checkConfig, getPath("InputJsr305AnnotationsCheckWithReturnValue.java"), expected); + } + }