From 0fefb6985e517abb945ed10dfaa3e70c551bbf60 Mon Sep 17 00:00:00 2001 From: Mohamed Sameh <110535847+mohamedsamehsalah@users.noreply.github.com> Date: Wed, 5 Apr 2023 18:12:33 +0200 Subject: [PATCH] Introduce `MonoJustOrEmptyOptional` Refaster rule (#563) While there, rename two other rules. --- .../refasterrules/ReactorRules.java | 20 +++++++++++++++++-- .../refasterrules/ReactorRulesTestInput.java | 8 ++++++-- .../refasterrules/ReactorRulesTestOutput.java | 8 ++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) 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 fed6b70b13..d7f96dc6d0 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 @@ -94,7 +94,7 @@ Mono after(T value) { } /** Prefer {@link Mono#justOrEmpty(Object)} over more contrived alternatives. */ - static final class MonoJustOrEmpty<@Nullable T> { + static final class MonoJustOrEmptyObject<@Nullable T> { @BeforeTemplate Mono before(T value) { return Mono.justOrEmpty(Optional.ofNullable(value)); @@ -107,9 +107,25 @@ Mono after(T value) { } /** Prefer {@link Mono#justOrEmpty(Optional)} over more verbose alternatives. */ + static final class MonoJustOrEmptyOptional { + @BeforeTemplate + Mono before(Optional optional) { + return Mono.just(optional).filter(Optional::isPresent).map(Optional::orElseThrow); + } + + @AfterTemplate + Mono after(Optional optional) { + return Mono.justOrEmpty(optional); + } + } + + /** + * Prefer {@link Mono#defer(Supplier) deferring} {@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. - static final class MonoFromOptional { + static final class MonoDeferMonoJustOrEmpty { @BeforeTemplate @SuppressWarnings( "MonoFromSupplier" /* `optional` may match a checked exception-throwing expression. */) 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 4e346c16b8..c6d11f9276 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 @@ -60,11 +60,15 @@ Mono testMonoJust() { return Mono.justOrEmpty(Optional.of(1)); } - Mono testMonoJustOrEmpty() { + Mono testMonoJustOrEmptyObject() { return Mono.justOrEmpty(Optional.ofNullable(1)); } - ImmutableSet> testMonoFromOptional() { + Mono testMonoJustOrEmptyOptional() { + return Mono.just(Optional.of(1)).filter(Optional::isPresent).map(Optional::orElseThrow); + } + + ImmutableSet> testMonoDeferMonoJustOrEmpty() { return ImmutableSet.of( Mono.fromCallable(() -> Optional.of(1).orElse(null)), Mono.fromSupplier(() -> Optional.of(2).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 6577c5fe24..debaaae622 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 @@ -62,11 +62,15 @@ Mono testMonoJust() { return Mono.just(1); } - Mono testMonoJustOrEmpty() { + Mono testMonoJustOrEmptyObject() { return Mono.justOrEmpty(1); } - ImmutableSet> testMonoFromOptional() { + Mono testMonoJustOrEmptyOptional() { + return Mono.justOrEmpty(Optional.of(1)); + } + + ImmutableSet> testMonoDeferMonoJustOrEmpty() { return ImmutableSet.of( Mono.defer(() -> Mono.justOrEmpty(Optional.of(1))), Mono.defer(() -> Mono.justOrEmpty(Optional.of(2))));