Skip to content

Commit

Permalink
Add extra case to IsNullRules and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Mar 21, 2023
1 parent 942c604 commit 791ca72
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private NullRules() {}
static final class IsNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return null == object;
}

@BeforeTemplate
boolean before2(@Nullable Object object) {
return Objects.isNull(object);
}

Expand All @@ -38,6 +43,11 @@ boolean after(@Nullable Object object) {
static final class IsNotNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return null != object;
}

@BeforeTemplate
boolean before2(@Nullable Object object) {
return Objects.nonNull(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static java.util.Objects.requireNonNull;

import com.google.common.base.Preconditions;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
Expand Down Expand Up @@ -79,7 +78,7 @@ void after(int index, int size, String message) {
static final class RequireNonNull<T> {
@BeforeTemplate
void before(T object) {
if (Refaster.anyOf(object == null, null == object)) {
if (object == null) {
throw new NullPointerException();
}
}
Expand All @@ -91,7 +90,7 @@ void before2(T object) {

@BeforeTemplate
void before3(T object) {
checkArgument(Refaster.anyOf(object != null, null != object));
checkArgument(object != null);
}

@AfterTemplate
Expand All @@ -105,7 +104,7 @@ void after(T object) {
static final class RequireNonNullWithMessage<T> {
@BeforeTemplate
void before(T object, String message) {
if (Refaster.anyOf(object == null, null == object)) {
if (object == null) {
throw new NullPointerException(message);
}
}
Expand All @@ -117,7 +116,7 @@ void before2(T object, String message) {

@BeforeTemplate
void before3(T object, String message) {
checkArgument(Refaster.anyOf(object != null, null != object), message);
checkArgument(object != null, message);
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class, Optional.class);
}

boolean testIsNull() {
return Objects.isNull("foo");
ImmutableSet<Boolean> testIsNull() {
return ImmutableSet.of(null == "foo", Objects.isNull("bar"));
}

boolean testIsNotNull() {
return Objects.nonNull("foo");
ImmutableSet<Boolean> testIsNotNull() {
return ImmutableSet.of(null != "foo", Objects.nonNull("bar"));
}

ImmutableSet<String> testRequireNonNullElse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class, Optional.class);
}

boolean testIsNull() {
return "foo" == null;
ImmutableSet<Boolean> testIsNull() {
return ImmutableSet.of("foo" == null, "bar" == null);
}

boolean testIsNotNull() {
return "foo" != null;
ImmutableSet<Boolean> testIsNotNull() {
return ImmutableSet.of("foo" != null, "bar" != null);
}

ImmutableSet<String> testRequireNonNullElse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ void testRequireNonNull() {
if ("foo" == null) {
throw new NullPointerException();
}
if (null == "bar") {
throw new NullPointerException();
}
checkNotNull("baz");
checkArgument("qux" != null);
checkArgument(null != "quux");
checkNotNull("bar");
checkArgument("baz" != null);
}

void testRequireNonNullWithMessage() {
if ("foo" == null) {
throw new NullPointerException("The string is null");
}
if (null == "bar") {
throw new NullPointerException("The string is null");
}
checkNotNull("baz", "The string is null");
checkArgument("qux" != null, "The string is null");
checkArgument(null != "quux", "The string is null");
checkNotNull("bar", "The string is null");
checkArgument("baz" != null, "The string is null");
}

void testCheckPositionIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ void testRequireNonNull() {
requireNonNull("foo");
requireNonNull("bar");
requireNonNull("baz");
requireNonNull("qux");
requireNonNull("quux");
}

void testRequireNonNullWithMessage() {
requireNonNull("foo", "The string is null");
requireNonNull("bar", "The string is null");
requireNonNull("baz", "The string is null");
requireNonNull("qux", "The string is null");
requireNonNull("quux", "The string is null");
}

void testCheckPositionIndex() {
Expand Down

0 comments on commit 791ca72

Please sign in to comment.