Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Mar 26, 2023
1 parent cbb3c05 commit 26c323f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +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 null == object;
}

@BeforeTemplate
boolean before2(@Nullable Object object) {
return Objects.isNull(object);
return Refaster.anyOf(null == object, Objects.isNull(object));
}

@AfterTemplate
Expand All @@ -39,16 +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 null != object;
}

@BeforeTemplate
boolean before2(@Nullable Object object) {
return Objects.nonNull(object);
return Refaster.anyOf(null != object, Objects.nonNull(object));
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,29 @@ void after(int index, int size, String message) {
}
}

/** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */
/** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */
static final class RequireNonNull<T> {
@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<T> {
@BeforeTemplate
void before(T object) {
if (object == null) {
throw new NullPointerException();
}
}

@BeforeTemplate
void before2(T object) {
checkNotNull(object);
}

@BeforeTemplate
void before3(T object) {
checkArgument(object != null);
Expand All @@ -100,20 +109,29 @@ void after(T object) {
}
}

/** Prefer {@link Objects#requireNonNull(Object, String)} over more verbose alternatives. */
/** Prefer {@link Objects#requireNonNull(Object, String)} over non-JDK alternatives. */
static final class RequireNonNullWithMessage<T> {
@BeforeTemplate
T before2(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 Objects#requireNonNull(Object, String)} over more verbose alternatives. */
static final class RequireNonNullWithMessageStatement<T> {
@BeforeTemplate
void before(T object, String message) {
if (object == null) {
throw new NullPointerException(message);
}
}

@BeforeTemplate
void before2(T object, String message) {
checkNotNull(object, message);
}

@BeforeTemplate
void before3(T object, String message) {
checkArgument(object != null, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase {
@Override
@SuppressWarnings("RequireNonNull")
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(checkNotNull(null));
}
Expand All @@ -30,20 +31,26 @@ void testCheckElementIndexWithMessage() {
}
}

void testRequireNonNull() {
String testRequireNonNull() {
return checkNotNull("foo");
}

void testRequireNonNullStatement() {
if ("foo" == null) {
throw new NullPointerException();
}
checkNotNull("bar");
checkArgument("baz" != null);
checkArgument("bar" != null);
}

String testRequireNonNullWithMessage() {
return checkNotNull("foo", "The string is null");
}

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

void testCheckPositionIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase {
@Override
@SuppressWarnings("RequireNonNull")
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(checkNotNull(null));
}
Expand All @@ -28,16 +29,22 @@ void testCheckElementIndexWithMessage() {
checkElementIndex(1, 2, "My index");
}

void testRequireNonNull() {
String testRequireNonNull() {
return requireNonNull("foo");
}

void testRequireNonNullStatement() {
requireNonNull("foo");
requireNonNull("bar");
requireNonNull("baz");
}

void testRequireNonNullWithMessage() {
String testRequireNonNullWithMessage() {
return requireNonNull("foo", "The string is null");
}

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

void testCheckPositionIndex() {
Expand Down

0 comments on commit 26c323f

Please sign in to comment.