diff --git a/sevntu-checks/sevntu-checks.xml b/sevntu-checks/sevntu-checks.xml index 95188a700..527deaba9 100644 --- a/sevntu-checks/sevntu-checks.xml +++ b/sevntu-checks/sevntu-checks.xml @@ -160,6 +160,7 @@ + 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 c3c84685b..c13c76153 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 @@ -34,34 +34,53 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; /** - * Jsr305AnnotationsCheck - a check to enforce use of nullness anotations. - * The standalone version of this plugin resides under - * - * https://github.com/mbert/JSR305CheckstylePlugin *

- * Copyright (C) 2008 Marcus Thiesen (initial version) Copyright (C) 2008-2019 Jan Burkhardt - * (maintainer) - *
- * Thanks to Mattias Nissler, Thorsten Ehlers, Fabian Loewner, Ole Langbehn for contributions. + * The Jsr305 annotations (annotations for + * software defect detection) contain a subset of "nullness" annotations that can be used to mark + * parameters as possibly null ({@code @Nullable}) or always non-null ({@code @Nonnull}), function + * return values as to be checked for null ({@code @CheckForNull}) or always non-null + * ({@code @Nonnull}) including defaults on class level ({@code @ParametersAreNonnullByDefault}, + * {@code @ParametersAreNullableByDefault}, {@code @ReturnValuesAreNonnullByDefault}). *

*

- * This check is activated by setting the package(s) to check: - *
- * packages = org.package1,com.package2 + * Using these annotations a programmer can declare how the code is meant to behave, and static code + * analysis (like e.g. FindBugs) can be used to verify that this is actually true. Also these + * annotations help others understanding code more easily, e.g. if confrontend with an annotated + * interface the necessity for null checks can easily be deducted from the annotations. *

*

- * Finetuning can be performed using the following settings: - *
- * Exclude some packages from checking: - *
- * excludePackages = org.package1.generated,com.package2.external - *
- * Allow overriding return values and/or parameters (useful for upgrading): - *
- * allowOverridingReturnValue = true - *
- * allowOverridingParameters = true + * The Jsr305AnnotationsCheck supports enforcing the following code style: *

+ *
    + *
  • Every method declaration, implementation or lambda requires nullness annotations for all + * parameters and return values except for primitive types (because a void or an int can never be + * null anyway).
  • + *
  • The annotation can be made directly or applied through either inheritance from an already + * annotated class or a annotation for class-wide defaults.
  • + *
  • Annotations need to make sense. For instance, a class-scope annotation cannot be used for a + * method, and a method annotation cannot be used for a class.
  • + *
  • In overridden methods, the following rule applies (regardless of what was annotated in the + * parent method): For parameter definitions {@code @Nonnull} annotation is always illegal because + * being less "nullable" cannot be assumed for a parameter in an inherited method. Conversely + * {@code @Nullable} is always allowed. For return values it is the other way round: + * {@code @CheckForNull} is always illegal while {@code @Nonnull} is always legal.
  • + *
+ *

+ * The following configuration properties are supported: + *

+ *
+ *
{@code packages = com.github.sevntu.pkg1,com.github.sevntu.pkg2}
+ *
Activate this check for a list of parent packages and their children.
+ *
{@code excludePackages = com.github.sevntu.pkg1.sub1,com.github.sevntu.pkg1.sub2}
+ *
Set packages excluded from checking. This setting can be useful if under the parent package + * set with "packages" there are subpackages which should not be checked.
+ *
{@code allowOverridingReturnValue = true}
+ *
Annotating return values "@CheckForNull" in overridden methods is flagged as an error. When + * setting the this property to true, this error will be ignored (useful for upgrading).
+ *
{@code allowOverridingParameters = true}
+ *
Annotating parameters "@Nonnull" in overridden methods is flagged as an error. When setting + * this property to true, this error will be ignored (useful for upgrading).
+ *
*/ public class Jsr305AnnotationsCheck extends AbstractCheck { diff --git a/sevntu-checks/src/main/resources/com/github/sevntu/checkstyle/checks/coding/messages.properties b/sevntu-checks/src/main/resources/com/github/sevntu/checkstyle/checks/coding/messages.properties index 97d740296..ceead8b75 100644 --- a/sevntu-checks/src/main/resources/com/github/sevntu/checkstyle/checks/coding/messages.properties +++ b/sevntu-checks/src/main/resources/com/github/sevntu/checkstyle/checks/coding/messages.properties @@ -60,27 +60,27 @@ return.null.Boolean=Method declares to return Boolean and returns null. return.boolean.ternary=Returning explicit boolean from ternary operator. whitespace.before.array.initializer=Array initializer should have whitespace before. jsr305.illegal.class.level.annotation=@CheckForNull, @Nullable, @Nonnull and @CheckReturnValue are not allowed on class level. Use @ParametersAreNonnullByDefault, @ParametersAreNullableByDefault and @ReturnValuesAreNonnullByDefault. -jsr305.contradicting.class.level.annotations=@ParametersAreNullableByDefault and @ParametersAreNonnullByDefault are not allowed together! +jsr305.contradicting.class.level.annotations=@ParametersAreNullableByDefault and @ParametersAreNonnullByDefault are not allowed together. jsr305.param.definitions.with.check.annotation=Parameter definitions don't need checking, use @Nullable or @Nonnull. -jsr305.param.definition.with.override.annotation=@Override is not allowed on parameter definition! -jsr305.param.definition.with.nonnull.by.default.annotation=@ParametersAreNonnullByDefault is not allowed on parameter definition! -jsr305.param.definition.with.nullable.by.default.annotation=@ParametersAreNullableByDefault is not allowed on parameter definition! -jsr305.param.definition.with.return.values.default.annotation=@ReturnValuesAreNonnullByDefault is not allowed on parameter definition! -jsr305.param.nonnull.and.nullable.annotation=@Nonnull and @Nullable are not allowed together! -jsr305.primitives.with.nullness.annotation=Primitives must not have any nullness annotations! -jsr305.overridden.definitions.with.increased.param.constraint=It is not allowed to increase nullness constraint for overriden method parameter definitions! +jsr305.param.definition.with.override.annotation=@Override is not allowed on parameter definition. +jsr305.param.definition.with.nonnull.by.default.annotation=@ParametersAreNonnullByDefault is not allowed on parameter definition. +jsr305.param.definition.with.nullable.by.default.annotation=@ParametersAreNullableByDefault is not allowed on parameter definition. +jsr305.param.definition.with.return.values.default.annotation=@ReturnValuesAreNonnullByDefault is not allowed on parameter definition. +jsr305.param.nonnull.and.nullable.annotation=@Nonnull and @Nullable are not allowed together. +jsr305.primitives.with.nullness.annotation=Primitives must not have any nullness annotations. +jsr305.overridden.definitions.with.increased.param.constraint=It is not allowed to increase nullness constraint for overriden method parameter definitions. jsr305.redundant.nonnull.param.annotation=It is not necessary to annotate @Nonnull if you annotated the method or class with @ParametersAreNonnullByDefault. jsr305.redundant.nullable.param.annotation=It is not necessary to annotate @Nullable if you annoted the method or class with @ParametersAreNullableByDefault. -jsr305.parameter.without.nullness.annotation=No nullness Annotation for parameter definition found! -jsr305.return.value.with.nonnull.by.default.annotation=@ReturnValuesAreNonnullByDefault is not allowed on method return values! -jsr305.return.value.with.nullable.annotation=@Nullable is not allowed on method return values! -jsr305.contradicting.return.value.annotations=@Nonnull and @CheckForNull are not allowed together! -jsr305.overridden.method.with.check.return.value.annotation=@CheckReturnValue is not allowed on overriden methods, annotate the interface or superclass! -jsr305.redundant.nonnull.by.default.annotation=Redundant @ParametersAreNonnullByDefault, the class is annotated with the same annotation! -jsr305.redundant.nullable.by.default.annotation=Redundant @ParametersAreNullableByDefault, the class is annotated with the same annotation! -jsr305.void.with.check.return.value.annotation=There is nothing to check on void return methods, remove @CheckReturnValue! +jsr305.parameter.without.nullness.annotation=No nullness Annotation for parameter definition found. +jsr305.return.value.with.nonnull.by.default.annotation=@ReturnValuesAreNonnullByDefault is not allowed on method return values. +jsr305.return.value.with.nullable.annotation=@Nullable is not allowed on method return values. +jsr305.contradicting.return.value.annotations=@Nonnull and @CheckForNull are not allowed together. +jsr305.overridden.method.with.check.return.value.annotation=@CheckReturnValue is not allowed on overriden methods, annotate the interface or superclass. +jsr305.redundant.nonnull.by.default.annotation=Redundant @ParametersAreNonnullByDefault, the class is annotated with the same annotation. +jsr305.redundant.nullable.by.default.annotation=Redundant @ParametersAreNullableByDefault, the class is annotated with the same annotation. +jsr305.void.with.check.return.value.annotation=There is nothing to check on void return methods, remove @CheckReturnValue. jsr305.redundant.nonnull.return.annotation=It is not necessary to annotate @Nonnull if you annoted the class with @ReturnValuesAreNonnullByDefault. -jsr305.return.without.nullness.annotation=Return value must have nullness Annotation (@Nonnull or @CheckForNull)! +jsr305.return.without.nullness.annotation=Return value must have nullness Annotation (@Nonnull or @CheckForNull). jsr305.overridden.methods.allow.only.nonnull=Overriden methods allow only @Nonnull. -jsr305.need.to.inherit.param.annotations=You have to inherit parameter annotations! -jsr305.constructor.with.return.annotation=Constructors have no return value and must not be annotated! +jsr305.need.to.inherit.param.annotations=You have to inherit parameter annotations. +jsr305.constructor.with.return.annotation=Constructors have no return value and must not be annotated. 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 783dee2a1..998d1bf78 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 @@ -37,17 +37,17 @@ public void testParameters() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "37:44: " + getCheckMessage( + "36:44: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "42:45: " + getCheckMessage( + "40:45: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "42:64: " + getCheckMessage( + "40:64: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "75:41: " + "68:41: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_DEFINITIONS_WITH_CHECK, "e"), - "81:27: " + getCheckMessage( + "73:27: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_OVERRIDDEN_WITH_INCREASED_CONSTRAINT, "e"), - "99:35: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), + "88:35: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), }; verify(checkConfig, getPath("InputJsr305AnnotationsCheckWithParameter.java"), expected); @@ -59,22 +59,22 @@ public void testPrimitives() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "29:37: " + getCheckMessage( + "28:37: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "34:38: " + getCheckMessage( + "32:38: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "39:42: " + "36:42: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_DEFINITIONS_WITH_CHECK, "e"), - "44:5: " + getCheckMessage( + "40:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "50:5: " + getCheckMessage( + "45:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "56:5: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NULLABLE, "e"), - "62:5: " + getCheckMessage( + "50:5: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NULLABLE, "e"), + "55:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "69:5: " + getCheckMessage( + "61:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "116:34: " + getCheckMessage( + "99:34: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), }; @@ -87,24 +87,24 @@ public void testReturnValues() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "49:5: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NULLABLE, "e"), - "55:5: " + getCheckMessage( + "46:5: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NULLABLE, "e"), + "51:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, "e"), - "76:5: " + getCheckMessage( + "69:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "83:5: " + getCheckMessage( + "75:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), - "103:5: " + getCheckMessage( + "92:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"), - "106:26: " + getCheckMessage( + "95:26: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "106:48: " + getCheckMessage( + "95:48: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "111:5: " + getCheckMessage( + "99:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE, "e"), - "118:5: " + getCheckMessage( + "105:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_VOID_WITH_CHECK_RETURN_VALUE_ANNOTATION, "e"), - "124:5: " + getCheckMessage( + "110:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"), }; @@ -117,12 +117,12 @@ public void testConstructors() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "38:55: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), - "44:55: " + getCheckMessage( + "37:55: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), + "42:55: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "58:55: " + getCheckMessage( + "54:55: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_PARAM_ANNOTATION, "e"), - "72:75: " + getCheckMessage( + "66:75: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NULLABLE_PARAM_ANNOTATION, "e"), }; @@ -135,19 +135,19 @@ public void testArrays() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "43:43: " + getCheckMessage( + "40:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "58:43: " + getCheckMessage( + "52:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "73:43: " + getCheckMessage( + "64:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "88:43: " + getCheckMessage( + "76:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "102:8: " + getCheckMessage( + "90:8: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_WITHOUT_NULLNESS_ANNOTATION, "e"), - "117:32: " + getCheckMessage( + "102:32: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "132:34: " + getCheckMessage( + "114:34: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), }; @@ -160,27 +160,27 @@ public void testClasses() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "33:5: " + getCheckMessage( + "32:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"), - "47:26: " + getCheckMessage( + "43:26: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NULLABLE_PARAM_ANNOTATION, "e"), - "57:29: " + getCheckMessage( + "51:29: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "79:32: " + getCheckMessage( + "68:32: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_PARAM_ANNOTATION, "e"), - "87:36: " + getCheckMessage( + "75:36: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "141:43: " + getCheckMessage( + "119:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "148:9: " + getCheckMessage( + "125:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_RETURN_ANNOTATION, "e"), - "152:9: " + getCheckMessage( + "128:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT, "e"), - "163:9: " + getCheckMessage( + "137:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_RETURN_ANNOTATION, "e"), - "169:9: " + getCheckMessage( + "142:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT, "e"), - "187:9: " + getCheckMessage( + "157:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT, "e"), }; @@ -193,15 +193,15 @@ public void testEnums() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "45:32: " + getCheckMessage( + "42:32: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_PARAM_ANNOTATION, "e"), - "54:36: " + getCheckMessage( + "50:36: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "71:9: " + getCheckMessage( + "64:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_RETURN_ANNOTATION, "e"), - "77:9: " + getCheckMessage( + "69:9: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT, "e"), - "90:5: " + getCheckMessage( + "80:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"), }; @@ -214,9 +214,9 @@ public void testInheritance() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "44:47: " + getCheckMessage( + "43:47: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "81:43: " + getCheckMessage( + "75:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), }; @@ -229,7 +229,7 @@ public void testReturnValueDefaults() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "37:5: " + getCheckMessage( + "35:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_REDUNDANT_NONNULL_RETURN_ANNOTATION, "e"), }; @@ -257,9 +257,9 @@ public void testLambdas() throws Exception { checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding"); final String[] expected = { - "35:10: " + getCheckMessage( + "33:10: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_OVERRIDDEN_WITH_INCREASED_CONSTRAINT, "e"), - "45:5: " + getCheckMessage( + "42:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_WITHOUT_NULLNESS_ANNOTATION, "e"), }; @@ -274,15 +274,15 @@ public void testAllowOverridingParameters() throws Exception { checkConfig.addAttribute("allowOverridingParameter", "true"); final String[] expected = { - "37:44: " + getCheckMessage( + "36:44: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "42:45: " + getCheckMessage( + "40:45: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "42:64: " + getCheckMessage( + "40:64: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "75:41: " + "68:41: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_DEFINITIONS_WITH_CHECK, "e"), - "99:35: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), + "88:35: " + getCheckMessage(Jsr305AnnotationsCheck.MSG_PARAM_NONNULL_AND_NULLABLE, "e"), }; verify(checkConfig, getPath("InputJsr305AnnotationsCheckWithParameter.java"), expected); @@ -296,7 +296,7 @@ public void testAllowOverridingLambdas() throws Exception { checkConfig.addAttribute("allowOverridingParameter", "true"); final String[] expected = { - "45:5: " + getCheckMessage( + "42:5: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_RETURN_WITHOUT_NULLNESS_ANNOTATION, "e"), }; @@ -311,9 +311,9 @@ public void testAllowOverridingInheritance() throws Exception { checkConfig.addAttribute("allowOverridingParameter", "true"); final String[] expected = { - "44:47: " + getCheckMessage( + "43:47: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), - "81:43: " + getCheckMessage( + "75:43: " + getCheckMessage( Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"), }; diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithArrays.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithArrays.java index 421082fb3..5d3506ac5 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithArrays.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithArrays.java @@ -29,112 +29,94 @@ public class InputJsr305AnnotationsCheckWithArrays { private final Object obj; - // ok - InputJsr305AnnotationsCheckWithArrays(@Nonnull final int[] nonnull) { + InputJsr305AnnotationsCheckWithArrays(@Nonnull final int[] nonnull) { // ok obj = nonnull; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nullable final long[] nullable) { + InputJsr305AnnotationsCheckWithArrays(@Nullable final long[] nullable) { // ok obj = nullable; } - // error - InputJsr305AnnotationsCheckWithArrays(final short[] array) { + InputJsr305AnnotationsCheckWithArrays(final short[] array) { // error obj = array; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nonnull final byte... varargs) { + InputJsr305AnnotationsCheckWithArrays(@Nonnull final byte... varargs) { // ok obj = varargs; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nullable final float... varargs) { + InputJsr305AnnotationsCheckWithArrays(@Nullable final float... varargs) { // ok obj = varargs; } - // error - InputJsr305AnnotationsCheckWithArrays(final double... varargs) { + InputJsr305AnnotationsCheckWithArrays(final double... varargs) { // error obj = varargs; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nonnull final String... varargs) { + InputJsr305AnnotationsCheckWithArrays(@Nonnull final String... varargs) { // ok obj = varargs; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nullable final Object... varargs) { + InputJsr305AnnotationsCheckWithArrays(@Nullable final Object... varargs) { // ok obj = varargs; } - // error - InputJsr305AnnotationsCheckWithArrays(final Serializable... varargs) { + InputJsr305AnnotationsCheckWithArrays(final Serializable... varargs) { // error obj = varargs; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nonnull final Class[] clazz) { + InputJsr305AnnotationsCheckWithArrays(@Nonnull final Class[] clazz) { // ok obj = clazz; } - // ok - InputJsr305AnnotationsCheckWithArrays(@Nullable final Iterable[] iterable) { + InputJsr305AnnotationsCheckWithArrays(@Nullable final Iterable[] iterable) { // ok obj = iterable; } - // error - InputJsr305AnnotationsCheckWithArrays(final Comparable comparable) { + InputJsr305AnnotationsCheckWithArrays(final Comparable comparable) { // error obj = comparable; } @Nonnull - int[] retNonnul() { + int[] retNonnul() { // ok return new int[] {}; } @CheckForNull - int[] retCheckForNull() { + int[] retCheckForNull() { // ok return null; } - int[] retNoAnnotation() { + int[] retNoAnnotation() { // error return new int[] {}; } - // ok - void testNonnullArray(@Nonnull final int[] array) { + void testNonnullArray(@Nonnull final int[] array) { // ok array[1] = 0; } - // ok - void testNullableArray(@Nullable final int[] array) { + void testNullableArray(@Nullable final int[] array) { // ok array[1] = 0; } - // error - void testNoAnnotationArray(final int[] array) { + void testNoAnnotationArray(final int[] array) { // error array[1] = 0; } - // ok - void testNonnullVarargs(@Nonnull final int... varargs) { + void testNonnullVarargs(@Nonnull final int... varargs) { // ok varargs[1] = 0; } - // ok - void testNullableVarargs(@Nullable final int... varargs) { + void testNullableVarargs(@Nullable final int... varargs) { // ok varargs[1] = 0; } - // error - void testNoAnnotationVarargs(final int... varargs) { + void testNoAnnotationVarargs(final int... varargs) { // error varargs[1] = 0; } @Override - public String toString() { + public String toString() { // ok return String.valueOf(obj); } } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithClass.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithClass.java index 8aad0697b..e4204dc0e 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithClass.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithClass.java @@ -29,70 +29,56 @@ public class InputJsr305AnnotationsCheckWithClass { - // error, double annotation - @ParametersAreNonnullByDefault + @ParametersAreNonnullByDefault // error, double annotation @ParametersAreNullableByDefault class BothParameterAnnotationsClass { } - // no error - @ParametersAreNullableByDefault + @ParametersAreNullableByDefault // ok interface ParameterAnnotationsInterface { - // no error - void setUnannotated(String unannotated); + void setUnannotated(String unannotated); // ok - // error, redundant - void setNullable(@Nullable String nullable); + void setNullable(@Nullable String nullable); // error, redundant - // no error - void setNonnull(@Nonnull String nonnull); + void setNonnull(@Nonnull String nonnull); // ok } interface NoParameterAnnotationsInterface { - // error, missing - void setUnannotated(String unannotated); + void setUnannotated(String unannotated); // error, missing - // no error - void setNullable(@Nonnull String nullable); + void setNullable(@Nonnull String nullable); // ok - // no error - void setNonnull(@Nullable String nonnull); + void setNonnull(@Nullable String nonnull); // ok } @ParametersAreNonnullByDefault class ParameterAnnotationsClass { - // no error - public void setUnannotated(final String unannotated) { + public void setUnannotated(final String unannotated) { // ok } - // no error - public void setNullable(@Nullable final String nullable) { + public void setNullable(@Nullable final String nullable) { // ok } - // error, redundant - public void setNonnull(@Nonnull final String nonnull) { + public void setNonnull(@Nonnull final String nonnull) { // error, redundant } } class NoParameterAnnotationsClass { - // error, missing - public void setUnannotated(final String unannotated) { + public void setUnannotated(final String unannotated) { // error, missing } - // no error - public void setNullable(@Nullable final String nullable) { + public void setNullable(@Nullable final String nullable) { // ok } - // no error - public void setNonnull(@Nonnull final String nonnull) { + public void setNonnull(@Nonnull final String nonnull) { // ok } } @@ -101,94 +87,77 @@ public void setNonnull(@Nonnull final String nonnull) { static class DefaultNullableParameterInheritingClass implements ParameterAnnotationsInterface { @Override - // no error - public void setUnannotated(final String unannotated) { + public void setUnannotated(final String unannotated) { // ok } @Override - // no error - public void setNullable(final String nullable) { + public void setNullable(final String nullable) { // ok } @Override - // no error - public void setNonnull(final String nonnull) { + public void setNonnull(final String nonnull) { // ok } - // no error - public void setAnotherUnannotated(final String unannotated) { + public void setAnotherUnannotated(final String unannotated) { // ok } } static class NullableParameterInheritingClass implements ParameterAnnotationsInterface { @Override - // no error - public void setUnannotated(final String unannotated) { + public void setUnannotated(final String unannotated) { // ok } @Override - // no error - public void setNullable(final String nullable) { + public void setNullable(final String nullable) { // ok } @Override - // no error - public void setNonnull(final String nonnull) { + public void setNonnull(final String nonnull) { // ok } - // error - public void setAnotherUnannotated(final String unannotated) { + public void setAnotherUnannotated(final String unannotated) { // error } } @ReturnValuesAreNonnullByDefault interface ReturnValueAnnotationInterface { - // error, redundant - @Nonnull + @Nonnull // error, redundant String getNonnull(); - // error, disallowed - @ReturnValuesAreNonnullByDefault + @ReturnValuesAreNonnullByDefault // error, disallowed String getNonnullByDefault(); - // no error - @CheckForNull + @CheckForNull // ok String getCheckForNull(); } @ReturnValuesAreNonnullByDefault static class ReturnValueAnnotationClass { - // error, redundant - @Nonnull + @Nonnull // error, redundant public String getNonnull() { return ""; } - // error, disallowed - @ReturnValuesAreNonnullByDefault + @ReturnValuesAreNonnullByDefault // error, disallowed public String getNonnullByDefault() { return ""; } - // no error - @CheckForNull + @CheckForNull // ok public String getCheckForNull() { return ""; } } interface NoReturnValueAnnotationInterface { - // no error - @Nonnull + @Nonnull // ok String getNonnull(); - // error, disallowed - @ReturnValuesAreNonnullByDefault + @ReturnValuesAreNonnullByDefault // error, disallowed String getNonnullByDefault(); - // no error - @CheckForNull + @CheckForNull // ok String getCheckForNull(); } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithConstructor.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithConstructor.java index 584146eba..7f84cdac1 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithConstructor.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithConstructor.java @@ -34,42 +34,36 @@ public class InputJsr305AnnotationsCheckWithConstructor { @Nullable private final Object nullable; - // error - public InputJsr305AnnotationsCheckWithConstructor(@Nonnull @Nullable final Enum value) { + public InputJsr305AnnotationsCheckWithConstructor(@Nonnull @Nullable final Enum value) { // error nullable = value; nonnull = new Object(); } - // error - public InputJsr305AnnotationsCheckWithConstructor(final Class clz) { + public InputJsr305AnnotationsCheckWithConstructor(final Class clz) { // error nonnull = new Object(); nullable = clz; } - // ok @ParametersAreNonnullByDefault - public InputJsr305AnnotationsCheckWithConstructor(final String string1, @Nullable final String string2) { + public InputJsr305AnnotationsCheckWithConstructor(final String string1, @Nullable final String string2) { // ok nonnull = string1; nullable = string2; } - // error @ParametersAreNonnullByDefault - public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final String string1, final Integer string2) { + public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final String string1, final Integer string2) { // error nonnull = string1; nullable = string2; } - // ok @ParametersAreNullableByDefault - public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final Integer int1, final Integer int2) { + public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final Integer int1, final Integer int2) { // ok nonnull = int1; nullable = int2; } - // error @ParametersAreNullableByDefault - public InputJsr305AnnotationsCheckWithConstructor(final Integer int1, @Nullable final String int2) { + public InputJsr305AnnotationsCheckWithConstructor(final Integer int1, @Nullable final String int2) { // error if (int1 != null) { nonnull = int1; } @@ -79,28 +73,24 @@ public InputJsr305AnnotationsCheckWithConstructor(final Integer int1, @Nullable nullable = int2; } - // ok - public InputJsr305AnnotationsCheckWithConstructor(@Nullable final String obj) { + public InputJsr305AnnotationsCheckWithConstructor(@Nullable final String obj) { // ok nonnull = new Object(); nullable = obj; } - // ok - public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final Integer obj) { + public InputJsr305AnnotationsCheckWithConstructor(@Nonnull final Integer obj) { // ok nonnull = obj; nullable = null; } - // ok @CheckForNull - public Object getNullable() { + public Object getNullable() { // ok return nullable; } - // ok @CheckReturnValue @Nonnull - public Object getNonnull() { + public Object getNonnull() { // ok return nonnull; } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefaultReturnValues.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefaultReturnValues.java index 481efcb5f..9e9649ec4 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefaultReturnValues.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefaultReturnValues.java @@ -26,15 +26,13 @@ @ReturnValuesAreNonnullByDefault public class InputJsr305AnnotationsCheckWithDefaultReturnValues { - // ok @Override - @Nonnull + @Nonnull // ok public String toString() { return super.toString(); } - // error - @Nonnull + @Nonnull // error public Object bar() { return new Object(); } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefectConstructor.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefectConstructor.java deleted file mode 100644 index fcdd25da8..000000000 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithDefectConstructor.java +++ /dev/null @@ -1,37 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2019 the original author or authors. -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//////////////////////////////////////////////////////////////////////////////// - -package com.github.sevntu.checkstyle.checks.coding; - -import javax.annotation.Nonnull; - -public class InputJsr305AnnotationsCheckWithDefectConstructor { - - private final String theString; - - public InputJsr305AnnotationsCheckWithDefectConstructor(@Nonnull final String string) { - this.theString = string; - } - - @Override - public String toString() { - return theString; - } - -} diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithEnum.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithEnum.java index 00039fd4c..7bf0a9a05 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithEnum.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithEnum.java @@ -33,16 +33,13 @@ public class InputJsr305AnnotationsCheckWithEnum { enum ParameterAnnotationsEnum { TEST; - // no error - public void setUnannotated(final String foo) { + public void setUnannotated(final String foo) { // ok } - // no error - public void setNullable(@Nullable final String foo) { + public void setNullable(@Nullable final String foo) { // ok } - // error, redundant - public void setNonnull(@Nonnull final String foo) { + public void setNonnull(@Nonnull final String foo) { // error, redundant } } @@ -50,16 +47,13 @@ public void setNonnull(@Nonnull final String foo) { enum NoParameterAnnotationsEnum { TEST; - // error, missing - public void setUnannotated(final String foo) { + public void setUnannotated(final String foo) { // error, missing } - // no error - public void setNullable(@Nullable final String foo) { + public void setNullable(@Nullable final String foo) { // ok } - // no error - public void setNonnull(@Nonnull final String foo) { + public void setNonnull(@Nonnull final String foo) { // ok } } @@ -67,27 +61,23 @@ public void setNonnull(@Nonnull final String foo) { @ReturnValuesAreNonnullByDefault enum ReturnValueAnnotationEnum { TEST; - // error, redundant - @Nonnull + @Nonnull // error, redundant public String getNonnull() { return ""; } - // error, disallowed - @ReturnValuesAreNonnullByDefault + @ReturnValuesAreNonnullByDefault // error, disallowed public String getNonnnullByDefault() { return ""; } - // no error - @CheckForNull + @CheckForNull // ok public String getCheckForNull() { return ""; } } - // error - @ParametersAreNonnullByDefault + @ParametersAreNonnullByDefault // error @ParametersAreNullableByDefault enum BothAnnotations { TEST; diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithInheritance.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithInheritance.java index 4bb0596bc..9d4ebc0e2 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithInheritance.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithInheritance.java @@ -40,8 +40,7 @@ public void getUnannotated(final String unannotated) { public void getNullable(final String nullable) { } - // error - public void getAnotherUnannotated(final String getAnotherUnannotated) { + public void getAnotherUnannotated(final String getAnotherUnannotated) { // error } @Override @@ -54,31 +53,26 @@ public Object nonnull() { @ParametersAreNonnullByDefault interface Inherited { - // no error - void getUnannotated(String unannotated); + void getUnannotated(String unannotated); // ok - // no error - void getNullable(@Nullable String nullable); + void getNullable(@Nullable String nullable); // ok @Nonnull Object nonnull(); } - // no error + // ok static class UnannotatedInheritor implements Inherited { - // no error, since we're overriding @NonnullByDefault from the interface - @Override + @Override // ok, since we're overriding @NonnullByDefault from the interface public void getUnannotated(@Nullable final String unannotated) { } - // no error - @Override + @Override // ok public void getNullable(final String nullable) { } - // error - public void getAnotherUnannotated(final String unannotated) { + public void getAnotherUnannotated(final String unannotated) { // error } @Override @@ -88,8 +82,7 @@ public Object nonnull() { } - // no error - @ParametersAreNullableByDefault + @ParametersAreNullableByDefault // ok static class AnnotatedInheritor implements Inherited { // this should not be an error, we are inheriting via the @Override @@ -98,17 +91,15 @@ static class AnnotatedInheritor implements Inherited { public void getUnannotated(final String unannotated) { } - // no error - @Override + @Override // ok public void getNullable(final String nullable) { } - // no error - public void getAnotherUnannotated(final String unannotated) { + public void getAnotherUnannotated(final String unannotated) { // ok } @Override - @Nonnull + @Nonnull // ok public Object nonnull() { return ""; } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithLambda.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithLambda.java index 93e1feeb5..b67f9fa03 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithLambda.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithLambda.java @@ -26,13 +26,11 @@ public final class InputJsr305AnnotationsCheckWithLambda { - // ok public static final Function TO_STRING = - object -> object.toString(); + object -> object.toString(); // ok - // error public static final ToIntFunction HASH_CODE = - (@Nonnull Object object) -> object.hashCode(); + (@Nonnull Object object) -> object.hashCode(); // error /** * Private util class constructor. @@ -41,8 +39,7 @@ private InputJsr305AnnotationsCheckWithLambda() { // empty } - // error - public String error() { + public String error() { // error return ""; } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithParameter.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithParameter.java index 1b3ae42e7..cc412f2c8 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithParameter.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithParameter.java @@ -32,78 +32,66 @@ public class InputJsr305AnnotationsCheckWithParameter private static final long serialVersionUID = 1L; - // error @Nonnull - public String missingNullnessParameter(final Object obj) { + public String missingNullnessParameter(final Object obj) { // error return obj.toString(); } - // 2x error - public boolean missingNullnessParameter(final Object obj1, final Object obj2) { + public boolean missingNullnessParameter(final Object obj1, final Object obj2) { // 2x error return obj1.equals(obj2); } - // ok @Nonnull - public String nonnullParameter(@Nonnull final Object obj) { + public String nonnullParameter(@Nonnull final Object obj) { // ok return obj.toString(); } - // ok public boolean nonnullParameter(@Nonnull final Object obj1, - @Nonnull final Object obj2) { + @Nonnull final Object obj2) { // ok return obj1.equals(obj2); } - // ok @Nonnull - public String nullableParameter(@Nullable final Object obj) { + public String nullableParameter(@Nullable final Object obj) { // ok return String.valueOf(obj); } - // ok public boolean nullableParameter(@Nullable final Object obj1, - @Nullable final Object obj2) { + @Nullable final Object obj2) { // ok if (obj1 != null) { System.out.println("bonk"); } return obj2 == null; } - // error @Nonnull - public String checkForNullParameter(@CheckForNull final Object obj) { + public String checkForNullParameter(@CheckForNull final Object obj) { // error return String.valueOf(obj); } - // error @Override - public boolean equals(@Nonnull final Object obj) { + public boolean equals(@Nonnull final Object obj) { // error return super.equals(obj); } - // ok @Override - public int hashCode() { + public int hashCode() { // ok return super.hashCode(); } - // ok @Override - public int compareTo(@Nullable final InputJsr305AnnotationsCheckWithParameter obj) { + public int compareTo(@Nullable final InputJsr305AnnotationsCheckWithParameter obj) { // ok return 0; } - // error @Nonnull - public String nonnullNullable(@Nonnull @Nullable final Object obj) { + public String nonnullNullable(@Nonnull @Nullable final Object obj) { // error return String.valueOf(obj); } - // ok @Nonnull @SuppressWarnings("unchecked") - public List typeCast() { + public List typeCast() { // ok final Object list = new ArrayList<>(); final List result = (List) list; return result; diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithPrimitives.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithPrimitives.java index a7b8d2fa2..40819aa6c 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithPrimitives.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithPrimitives.java @@ -25,95 +25,78 @@ public class InputJsr305AnnotationsCheckWithPrimitives { - // error - public boolean nonnullParameter(@Nonnull final int param) { + public boolean nonnullParameter(@Nonnull final int param) { // error return param == 0; } - // error - public boolean nullableParameter(@Nullable final int param) { + public boolean nullableParameter(@Nullable final int param) { // error return param == 0; } - // error - public boolean checkForNullParameter(@CheckForNull final int param) { + public boolean checkForNullParameter(@CheckForNull final int param) { // error return param == 0; } - // error - @Nonnull + @Nonnull // error public int returnNonnull() { return 0; } - // error - @CheckForNull + @CheckForNull // error public int returnCheckForNull() { return 0; } - // error - @Nullable + @Nullable // error public int returnNullable() { return 0; } - // error - @Override + @Override // error @Nonnull public boolean equals(Object object) { return super.equals(object); } - // error - @Override + @Override // error @Nonnull public int hashCode() { return super.hashCode(); } - // ok - public int primitivesInt(final int primitive) { + public int primitivesInt(final int primitive) { // ok return primitive + 1; } - // ok - public byte primitivesByte(final byte primitive) { + public byte primitivesByte(final byte primitive) { // ok return primitive; } - // ok - public boolean primitivesBoolean(final boolean primitive) { + public boolean primitivesBoolean(final boolean primitive) { // ok return !primitive; } - // ok - public float primitivesFloat(final float primitive) { + public float primitivesFloat(final float primitive) { // ok return primitive + 1.0F; } - // ok - public double primitivesDouble(final double primitive) { + public double primitivesDouble(final double primitive) { // ok return primitive + 1.0D; } - // ok - public long primitivesLong(final long primitive) { + public long primitivesLong(final long primitive) { // ok return primitive + 1L; } - // ok - public short primitivesShort(final short primitive) { + public short primitivesShort(final short primitive) { // ok return primitive; } - // ok - public char primitivesChar(final char primitive) { + public char primitivesChar(final char primitive) { // ok return primitive; } - // error - public int primitivesNonnull(@Nonnull final int primitive) { + public int primitivesNonnull(@Nonnull final int primitive) { // error return primitive + 1; } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithRedundantClassLevel.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithRedundantClassLevel.java index 49b69d4f2..ee33fd8c8 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithRedundantClassLevel.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithRedundantClassLevel.java @@ -25,14 +25,14 @@ @ParametersAreNullableByDefault public class InputJsr305AnnotationsCheckWithRedundantClassLevel { - @ParametersAreNullableByDefault + @ParametersAreNullableByDefault // error public void redundant1() { } - public void redundant2() { + public void redundant2() { // ok } - @ParametersAreNonnullByDefault + @ParametersAreNonnullByDefault // ok public void redundant3() { } } diff --git a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithReturnValue.java b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithReturnValue.java index b214e629b..b5dccef02 100644 --- a/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithReturnValue.java +++ b/sevntu-checks/src/test/resources/com/github/sevntu/checkstyle/checks/coding/InputJsr305AnnotationsCheckWithReturnValue.java @@ -33,95 +33,81 @@ public class InputJsr305AnnotationsCheckWithReturnValue private static final long serialVersionUID = 1L; - // ok - @Nonnull + @Nonnull // ok Object returnNonnull() { return new Object(); } - // ok - @CheckForNull + @CheckForNull // ok Object returnCheckForNull() { return null; } - // error - @Nullable + @Nullable // error Object returnNullable() { return null; } - // error - @Nonnull + @Nonnull // error @CheckForNull Object returnNonnullCheckForNull() { return new Object(); } - // ok - @CheckReturnValue + @CheckReturnValue // ok @CheckForNull Object returnCheckReturnValueCheckForNull() { return new Object(); } - // ok - @CheckReturnValue + @CheckReturnValue // ok @Nonnull Object returnCheckReturnValueNonnull() { return new Object(); } - // error - @Override + @Override // error @Nonnull public boolean equals(final Object obj) { return super.equals(obj); } - // error - @Override + @Override // error @CheckForNull public int hashCode() { return super.hashCode(); } - // ok - @Override + @Override // ok public void run() { hashCode(); } - // ok - @Override + @Override // ok @Nonnull public String toString() { return super.toString(); } - // error + 2x parameter error - @ParametersAreNonnullByDefault + @ParametersAreNonnullByDefault // error @ParametersAreNullableByDefault @Nonnull - public String concat(final String string1, final String string2) { + public String concat(final String string1, final String string2) { // error return string1 + string2; } - // error - @Override + @Override // error @CheckReturnValue public int compareTo(@Nullable final InputJsr305AnnotationsCheckWithReturnValue obj) { return 0; } - // error - @CheckReturnValue + @CheckReturnValue // error public void voidNoCheckReturnValue() { hashCode(); } - // error - @Nonnull + @Nonnull // error public void voidNoNonnull() { hashCode(); }