From 1afce12b5286103c9c5574c2304144500291b520 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Tue, 6 Dec 2022 08:15:39 +0100 Subject: [PATCH] Introduce `Mono{Empty,Just,JustOrEmpty}` Refaster rules (#385) --- .../refasterrules/ReactorRules.java | 40 +++++++++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 12 ++++++ .../refasterrules/ReactorRulesTestOutput.java | 12 ++++++ 3 files changed, 64 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index d3695bec7f..06b2d97f40 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -26,6 +26,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; +import org.jspecify.nullness.Nullable; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -59,6 +60,45 @@ Mono after(Supplier supplier) { } } + /** Prefer {@link Mono#empty()} over more contrived alternatives. */ + static final class MonoEmpty { + @BeforeTemplate + Mono before() { + return Refaster.anyOf(Mono.justOrEmpty(null), Mono.justOrEmpty(Optional.empty())); + } + + @AfterTemplate + Mono after() { + return Mono.empty(); + } + } + + /** Prefer {@link Mono#just(Object)} over more contrived alternatives. */ + static final class MonoJust { + @BeforeTemplate + Mono before(T value) { + return Mono.justOrEmpty(Optional.of(value)); + } + + @AfterTemplate + Mono after(T value) { + return Mono.just(value); + } + } + + /** Prefer {@link Mono#justOrEmpty(Object)} over more contrived alternatives. */ + static final class MonoJustOrEmpty<@Nullable T> { + @BeforeTemplate + Mono before(T value) { + return Mono.justOrEmpty(Optional.ofNullable(value)); + } + + @AfterTemplate + Mono after(T value) { + return Mono.justOrEmpty(value); + } + } + /** Prefer {@link Mono#justOrEmpty(Optional)} over more verbose alternatives. */ // XXX: If `optional` is a constant and effectively-final expression then the `Mono.defer` can be // dropped. Should look into Refaster support for identifying this. diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index daf40d369f..ab1abfc97c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -34,6 +34,18 @@ ImmutableSet> testMonoFromSupplier() { Mono.fromCallable(this::toString)); } + ImmutableSet> testMonoEmpty() { + return ImmutableSet.of(Mono.justOrEmpty(null), Mono.justOrEmpty(Optional.empty())); + } + + Mono testMonoJust() { + return Mono.justOrEmpty(Optional.of(1)); + } + + Mono testMonoJustOrEmpty() { + return Mono.justOrEmpty(Optional.ofNullable(1)); + } + ImmutableSet> testMonoFromOptional() { return ImmutableSet.of( Mono.fromCallable(() -> Optional.of(1).orElse(null)), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index df6fb70af4..33edca43a6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -37,6 +37,18 @@ ImmutableSet> testMonoFromSupplier() { Mono.fromSupplier(this::toString)); } + ImmutableSet> testMonoEmpty() { + return ImmutableSet.of(Mono.empty(), Mono.empty()); + } + + Mono testMonoJust() { + return Mono.just(1); + } + + Mono testMonoJustOrEmpty() { + return Mono.justOrEmpty(1); + } + ImmutableSet> testMonoFromOptional() { return ImmutableSet.of( Mono.defer(() -> Mono.justOrEmpty(Optional.of(1))),