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 68650e5b45..b9d79aa450 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 @@ -1053,6 +1053,32 @@ Flux after(Flux flux) { } } + /** Prefer {@link Mono#ofType(Class)} over more contrived alternatives. */ + static final class MonoOfType { + @BeforeTemplate + Mono before(Mono mono, Class clazz) { + return mono.filter(clazz::isInstance).cast(clazz); + } + + @AfterTemplate + Mono after(Mono mono, Class clazz) { + return mono.ofType(clazz); + } + } + + /** Prefer {@link Flux#ofType(Class)} over more contrived alternatives. */ + static final class FluxOfType { + @BeforeTemplate + Flux before(Flux flux, Class clazz) { + return flux.filter(clazz::isInstance).cast(clazz); + } + + @AfterTemplate + Flux after(Flux flux, Class clazz) { + return flux.ofType(clazz); + } + } + /** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ static final class MonoFlatMap { @BeforeTemplate 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 b10b47f57b..cab6be19ef 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 @@ -364,6 +364,14 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + Mono testMonoOfType() { + return Mono.just(1).filter(Number.class::isInstance).cast(Number.class); + } + + Flux testFluxOfType() { + return Flux.just(1).filter(Number.class::isInstance).cast(Number.class); + } + Mono testMonoFlatMap() { return Mono.just("foo").map(Mono::just).flatMap(identity()); } 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 98279c9060..6e8005f65e 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 @@ -357,6 +357,14 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + Mono testMonoOfType() { + return Mono.just(1).ofType(Number.class); + } + + Flux testFluxOfType() { + return Flux.just(1).ofType(Number.class); + } + Mono testMonoFlatMap() { return Mono.just("foo").flatMap(Mono::just); }