diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java index 578ef91c59..2d7a6b263b 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java @@ -21,11 +21,14 @@ final class NullRules { private NullRules() {} - /** Prefer the {@code ==} operator over {@link Objects#isNull(Object)}. */ + /** + * Prefer the {@code ==} operator (with {@code null} as the second operand) over {@link + * Objects#isNull(Object)}. + */ static final class IsNull { @BeforeTemplate boolean before(@Nullable Object object) { - return Objects.isNull(object); + return Refaster.anyOf(null == object, Objects.isNull(object)); } @AfterTemplate @@ -34,11 +37,14 @@ boolean after(@Nullable Object object) { } } - /** Prefer the {@code !=} operator over {@link Objects#nonNull(Object)}. */ + /** + * Prefer the {@code !=} operator (with {@code null} as the second operand) over {@link + * Objects#nonNull(Object)}. + */ static final class IsNotNull { @BeforeTemplate boolean before(@Nullable Object object) { - return Objects.nonNull(object); + return Refaster.anyOf(null != object, Objects.nonNull(object)); } @AfterTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java index a117aea41c..3ab863d1e6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java @@ -6,11 +6,13 @@ import static com.google.common.base.Preconditions.checkPositionIndex; import static com.google.common.base.Preconditions.checkState; import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static java.util.Objects.requireNonNull; import com.google.common.base.Preconditions; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.Objects; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** Refaster templates related to statements dealing with {@link Preconditions}. */ @@ -72,8 +74,22 @@ void after(int index, int size, String message) { } } - /** Prefer {@link Preconditions#checkNotNull(Object)} over more verbose alternatives. */ - static final class CheckNotNull { + /** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */ + static final class RequireNonNull { + @BeforeTemplate + T before(T object) { + return checkNotNull(object); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(T object) { + return requireNonNull(object); + } + } + + /** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */ + static final class RequireNonNullStatement { @BeforeTemplate void before(T object) { if (object == null) { @@ -84,12 +100,26 @@ void before(T object) { @AfterTemplate @UseImportPolicy(STATIC_IMPORT_ALWAYS) void after(T object) { - checkNotNull(object); + requireNonNull(object); + } + } + + /** Prefer {@link Objects#requireNonNull(Object, String)} over non-JDK alternatives. */ + static final class RequireNonNullWithMessage { + @BeforeTemplate + T before(T object, String message) { + return checkNotNull(object, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(T object, String message) { + return requireNonNull(object, message); } } - /** Prefer {@link Preconditions#checkNotNull(Object, Object)} over more verbose alternatives. */ - static final class CheckNotNullWithMessage { + /** Prefer {@link Objects#requireNonNull(Object, String)} over more verbose alternatives. */ + static final class RequireNonNullWithMessageStatement { @BeforeTemplate void before(T object, String message) { if (object == null) { @@ -100,7 +130,7 @@ void before(T object, String message) { @AfterTemplate @UseImportPolicy(STATIC_IMPORT_ALWAYS) void after(T object, String message) { - checkNotNull(object, message); + requireNonNull(object, message); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestInput.java index a84a12a27e..745f19bf8e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestInput.java @@ -13,12 +13,12 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(MoreObjects.class, Optional.class); } - boolean testIsNull() { - return Objects.isNull("foo"); + ImmutableSet testIsNull() { + return ImmutableSet.of(null == "foo", Objects.isNull("bar")); } - boolean testIsNotNull() { - return Objects.nonNull("foo"); + ImmutableSet testIsNotNull() { + return ImmutableSet.of(null != "foo", Objects.nonNull("bar")); } ImmutableSet testRequireNonNullElse() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestOutput.java index 6fa2228d91..f5e15e0fed 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/NullRulesTestOutput.java @@ -16,12 +16,12 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(MoreObjects.class, Optional.class); } - boolean testIsNull() { - return "foo" == null; + ImmutableSet testIsNull() { + return ImmutableSet.of("foo" == null, "bar" == null); } - boolean testIsNotNull() { - return "foo" != null; + ImmutableSet testIsNotNull() { + return ImmutableSet.of("foo" != null, "bar" != null); } ImmutableSet testRequireNonNullElse() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java index 4979486d02..56a3a41e77 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java @@ -1,8 +1,17 @@ package tech.picnic.errorprone.refasterrules; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.collect.ImmutableSet; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase { + @Override + @SuppressWarnings("RequireNonNull") + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(checkNotNull(null)); + } + void testCheckArgument() { if ("foo".isEmpty()) { throw new IllegalArgumentException(); @@ -21,13 +30,21 @@ void testCheckElementIndexWithMessage() { } } - void testCheckNotNull() { + String testRequireNonNull() { + return checkNotNull("foo"); + } + + void testRequireNonNullStatement() { if ("foo" == null) { throw new NullPointerException(); } } - void testCheckNotNullWithMessage() { + String testRequireNonNullWithMessage() { + return checkNotNull("foo", "The string is null"); + } + + void testRequireNonNullWithMessageStatement() { if ("foo" == null) { throw new NullPointerException("The string is null"); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java index 965dab5989..53a045fd12 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java @@ -5,10 +5,18 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkPositionIndex; import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; +import com.google.common.collect.ImmutableSet; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase { + @Override + @SuppressWarnings("RequireNonNull") + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(checkNotNull(null)); + } + void testCheckArgument() { checkArgument(!"foo".isEmpty()); } @@ -21,12 +29,20 @@ void testCheckElementIndexWithMessage() { checkElementIndex(1, 2, "My index"); } - void testCheckNotNull() { - checkNotNull("foo"); + String testRequireNonNull() { + return requireNonNull("foo"); + } + + void testRequireNonNullStatement() { + requireNonNull("foo"); + } + + String testRequireNonNullWithMessage() { + return requireNonNull("foo", "The string is null"); } - void testCheckNotNullWithMessage() { - checkNotNull("foo", "The string is null"); + void testRequireNonNullWithMessageStatement() { + requireNonNull("foo", "The string is null"); } void testCheckPositionIndex() {