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 c92668319f3..06ed6643d3d 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 @@ -794,6 +794,40 @@ Flux after(Flux flux) { } } + /** Prefer {@link Mono#ofType(Class)} over more contrived alternatives. */ + static final class MonoOfType { + @BeforeTemplate + Mono before(Mono mono) { + return mono.filter( + Refaster.anyOf( + Refaster.clazz()::isInstance, + x -> Refaster.clazz().isAssignableFrom(x.getClass()))) + .cast(Refaster.clazz()); + } + + @AfterTemplate + Mono after(Mono mono) { + return mono.ofType(Refaster.clazz()); + } + } + + /** Prefer {@link Flux#ofType(Class)} over more contrived alternatives. */ + static final class FluxOfType { + @BeforeTemplate + Flux before(Flux flux) { + return flux.filter( + Refaster.anyOf( + Refaster.clazz()::isInstance, + x -> Refaster.clazz().isAssignableFrom(x.getClass()))) + .cast(Refaster.clazz()); + } + + @AfterTemplate + Flux after(Flux flux) { + return flux.ofType(Refaster.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 7f8021f70ca..441e04be6a8 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 @@ -269,6 +269,18 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + ImmutableSet> testMonoOfType() { + return ImmutableSet.of( + Mono.just(1).filter(Number.class::isInstance).cast(Number.class), + Mono.just(2).filter(n -> Number.class.isAssignableFrom(n.getClass())).cast(Number.class)); + } + + ImmutableSet> testFluxOfType() { + return ImmutableSet.of( + Flux.just(1).filter(Number.class::isInstance).cast(Number.class), + Flux.just(2).filter(n -> Number.class.isAssignableFrom(n.getClass())).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 b12af508c48..2a30cdf99f7 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 @@ -266,6 +266,14 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + ImmutableSet> testMonoOfType() { + return ImmutableSet.of(Mono.just(1).ofType(Number.class), Mono.just(2).ofType(Number.class)); + } + + ImmutableSet> testFluxOfType() { + return ImmutableSet.of(Flux.just(1).ofType(Number.class), Flux.just(2).ofType(Number.class)); + } + Mono testMonoFlatMap() { return Mono.just("foo").flatMap(Mono::just); }