From ffa4c516381e6ee5610b45de88e592c1e5ff95bd Mon Sep 17 00:00:00 2001 From: Chamil Prabodha Date: Mon, 3 Oct 2022 15:52:08 +0200 Subject: [PATCH 1/3] PSM-1627 Create refaster template to rewrite `.onErrorResume(e -> Mono.empty())` to `.onErrorComplete()` Add a new refaster template rule that rewrites `.onErrorResume(e -> Mono.empty())` to `.onErrorComplete()`. This is applied to `.onErrorResume(e -> Flux.empty())` as well. --- .../refastertemplates/ReactorTemplates.java | 26 +++++++++++++++++++ .../ReactorTemplatesTestInput.java | 8 ++++++ .../ReactorTemplatesTestOutput.java | 8 ++++++ 3 files changed, 42 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index 3201a8d4ee..bff2528c1b 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -271,6 +271,32 @@ Flux after(Flux flux) { } } + /** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives */ + static final class FluxOnErrorComplete { + @BeforeTemplate + Flux before(Flux flux) { + return flux.onErrorResume(e -> Flux.empty()); + } + + @AfterTemplate + Flux after(Flux flux) { + return flux.onErrorComplete(); + } + } + + /** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives */ + static final class MonoOnErrorComplete { + @BeforeTemplate + Mono before(Mono mono) { + return mono.onErrorResume(e -> Mono.empty()); + } + + @AfterTemplate + Mono after(Mono mono) { + return mono.onErrorComplete(); + } + } + /** Prefer {@link PublisherProbe#empty()}} over more verbose alternatives. */ static final class PublisherProbeEmpty { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java index bd353ed35a..573b6485d4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java @@ -95,6 +95,14 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + Flux testFluxOnErrorComplete() { + return Flux.error(new IllegalStateException()).onErrorResume(e -> Flux.empty()); + } + + Mono testMonoOnErrorComplete() { + return Mono.error(new IllegalStateException()).onErrorResume(e -> Mono.empty()); + } + ImmutableSet> testPublisherProbeEmpty() { return ImmutableSet.of(PublisherProbe.of(Mono.empty()), PublisherProbe.of(Flux.empty())); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java index 2116246d73..482abe347a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java @@ -94,6 +94,14 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + Flux testFluxOnErrorComplete() { + return Flux.error(new IllegalStateException()).onErrorComplete(); + } + + Mono testMonoOnErrorComplete() { + return Mono.error(new IllegalStateException()).onErrorComplete(); + } + ImmutableSet> testPublisherProbeEmpty() { return ImmutableSet.of(PublisherProbe.empty(), PublisherProbe.empty()); } From c3f6d154631ea0e212d9f8ab782ca2d35baac279 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 4 Oct 2022 10:29:04 +0200 Subject: [PATCH 2/3] Suggestions --- .../refastertemplates/ReactorTemplates.java | 24 +++++++++---------- .../ReactorTemplatesTestInput.java | 8 +++---- .../ReactorTemplatesTestOutput.java | 8 +++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index bff2528c1b..caf804c465 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -271,29 +271,29 @@ Flux after(Flux flux) { } } - /** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives */ - static final class FluxOnErrorComplete { + /** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives. */ + static final class MonoOnErrorComplete { @BeforeTemplate - Flux before(Flux flux) { - return flux.onErrorResume(e -> Flux.empty()); + Mono before(Mono mono) { + return mono.onErrorResume(e -> Mono.empty()); } @AfterTemplate - Flux after(Flux flux) { - return flux.onErrorComplete(); + Mono after(Mono mono) { + return mono.onErrorComplete(); } } - /** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives */ - static final class MonoOnErrorComplete { + /** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives. */ + static final class FluxOnErrorComplete { @BeforeTemplate - Mono before(Mono mono) { - return mono.onErrorResume(e -> Mono.empty()); + Flux before(Flux flux) { + return flux.onErrorResume(e -> Flux.empty()); } @AfterTemplate - Mono after(Mono mono) { - return mono.onErrorComplete(); + Flux after(Flux flux) { + return flux.onErrorComplete(); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java index 573b6485d4..d44ea29e77 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java @@ -95,12 +95,12 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } - Flux testFluxOnErrorComplete() { - return Flux.error(new IllegalStateException()).onErrorResume(e -> Flux.empty()); + Mono testMonoOnErrorComplete() { + return Mono.just(1).onErrorResume(e -> Mono.empty()); } - Mono testMonoOnErrorComplete() { - return Mono.error(new IllegalStateException()).onErrorResume(e -> Mono.empty()); + Flux testFluxOnErrorComplete() { + return Flux.just(1).onErrorResume(e -> Flux.empty()); } ImmutableSet> testPublisherProbeEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java index 482abe347a..72edb92164 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java @@ -94,12 +94,12 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } - Flux testFluxOnErrorComplete() { - return Flux.error(new IllegalStateException()).onErrorComplete(); + Mono testMonoOnErrorComplete() { + return Mono.just(1).onErrorComplete(); } - Mono testMonoOnErrorComplete() { - return Mono.error(new IllegalStateException()).onErrorComplete(); + Flux testFluxOnErrorComplete() { + return Flux.just(1).onErrorComplete(); } ImmutableSet> testPublisherProbeEmpty() { From a8e8c589b523a35eda3a7376764d8a1e189fcfda Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 6 Oct 2022 14:38:40 +0200 Subject: [PATCH 3/3] Suggestions --- .../errorprone/refastertemplates/ReactorTemplates.java | 4 ++-- .../refastertemplates/ReactorTemplatesTestInput.java | 6 ++++-- .../refastertemplates/ReactorTemplatesTestOutput.java | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index caf804c465..ef56d3a32b 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -288,7 +288,7 @@ Mono after(Mono mono) { static final class FluxOnErrorComplete { @BeforeTemplate Flux before(Flux flux) { - return flux.onErrorResume(e -> Flux.empty()); + return flux.onErrorResume(e -> Refaster.anyOf(Mono.empty(), Flux.empty())); } @AfterTemplate @@ -301,7 +301,7 @@ Flux after(Flux flux) { static final class PublisherProbeEmpty { @BeforeTemplate PublisherProbe before() { - return Refaster.anyOf(PublisherProbe.of(Mono.empty()), PublisherProbe.of(Flux.empty())); + return PublisherProbe.of(Refaster.anyOf(Mono.empty(), Flux.empty())); } @AfterTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java index d44ea29e77..4844a8dcab 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java @@ -99,8 +99,10 @@ Mono testMonoOnErrorComplete() { return Mono.just(1).onErrorResume(e -> Mono.empty()); } - Flux testFluxOnErrorComplete() { - return Flux.just(1).onErrorResume(e -> Flux.empty()); + ImmutableSet> testFluxOnErrorComplete() { + return ImmutableSet.of( + Flux.just(1).onErrorResume(e -> Mono.empty()), + Flux.just(2).onErrorResume(e -> Flux.empty())); } ImmutableSet> testPublisherProbeEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java index 72edb92164..34a39c6259 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java @@ -98,8 +98,8 @@ Mono testMonoOnErrorComplete() { return Mono.just(1).onErrorComplete(); } - Flux testFluxOnErrorComplete() { - return Flux.just(1).onErrorComplete(); + ImmutableSet> testFluxOnErrorComplete() { + return ImmutableSet.of(Flux.just(1).onErrorComplete(), Flux.just(2).onErrorComplete()); } ImmutableSet> testPublisherProbeEmpty() {