From 710520ebe1c00138d1d84be6eaeef43577503f62 Mon Sep 17 00:00:00 2001 From: Bastien Diederichs Date: Thu, 27 Oct 2022 13:54:48 +0200 Subject: [PATCH] Drop redundant `::isInstance` in `onErrorResume` --- .../errorprone/refasterrules/ReactorRules.java | 13 +++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 5 +++++ .../refasterrules/ReactorRulesTestOutput.java | 4 ++++ 3 files changed, 22 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 5f2f44045e..a98ab8d2e9 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 @@ -676,6 +676,19 @@ Flux after(Flux flux) { } } + /** Drop redundant {@link Class#isInstance} in {@link Mono#onErrorResume(Function)}. */ + static final class MonoOnErrorResume { + @BeforeTemplate + Mono before(Mono mono, Class clazz, Mono other) { + return mono.onErrorResume(clazz::isInstance, e -> other); + } + + @AfterTemplate + Mono after(Mono mono, Class clazz, Mono other) { + return mono.onErrorResume(clazz, e -> other); + } + } + /** Prefer {@link reactor.util.context.Context#empty()}} over more verbose alternatives. */ // XXX: Consider introducing an `IsEmpty` matcher that identifies a wide range of guaranteed-empty // `Collection` and `Map` expressions. 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 8c33d36b80..0e7519921e 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 @@ -218,6 +218,11 @@ ImmutableSet> testFluxOnErrorComplete() { Flux.just(2).onErrorResume(e -> Flux.empty())); } + Mono testMonoOnErrorResume() { + return Mono.just(1) + .onErrorResume(IllegalArgumentException.class::isInstance, e -> Mono.just(2)); + } + ImmutableSet testContextEmpty() { return ImmutableSet.of(Context.of(new HashMap<>()), Context.of(ImmutableMap.of())); } 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 b943f50ea4..795db5fcb4 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 @@ -218,6 +218,10 @@ ImmutableSet> testFluxOnErrorComplete() { return ImmutableSet.of(Flux.just(1).onErrorComplete(), Flux.just(2).onErrorComplete()); } + Mono testMonoOnErrorResume() { + return Mono.just(1).onErrorResume(IllegalArgumentException.class, e -> Mono.just(2)); + } + ImmutableSet testContextEmpty() { return ImmutableSet.of(Context.empty(), Context.empty()); }