From d2d9057a8e37e3d975853e4e7ff099ad991e01ee Mon Sep 17 00:00:00 2001 From: Picnic-Bot Date: Mon, 20 Feb 2023 02:00:54 +0000 Subject: [PATCH 1/5] Upgrade io.projectreactor:reactor-bom 2022.0.2 -> 2022.0.3 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c8544c8bec..7d81ab8a32 100644 --- a/pom.xml +++ b/pom.xml @@ -287,7 +287,7 @@ io.projectreactor reactor-bom - 2022.0.2 + 2022.0.3 pom import From 6152afa1c03e1fe555664f7fc9e065c9336e68bc Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 20 Feb 2023 09:09:24 +0100 Subject: [PATCH 2/5] Introduce `MonoSingleOptional` Refaster rule --- .../errorprone/refasterrules/ReactorRules.java | 13 +++++++++++++ 1 file changed, 13 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 c99c77482a..85b0e69ce7 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 @@ -717,6 +717,19 @@ Mono> after(Mono mono) { } } + /** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */ + static final class MonoSingleOptional { + @BeforeTemplate + Mono> before(Mono mono) { + return mono.flux().collect(toOptional()); + } + + @AfterTemplate + Mono> after(Mono mono) { + return mono.singleOptional(); + } + } + /** Prefer {@link Mono#cast(Class)} over {@link Mono#map(Function)} with a cast. */ static final class MonoCast { @BeforeTemplate From e930c8b38e5c04e09112580e04a473d71acc3c88 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 20 Feb 2023 10:12:46 +0100 Subject: [PATCH 3/5] Merge two templates --- .../refasterrules/ReactorRules.java | 22 ++++--------------- .../refasterrules/ReactorRulesTestInput.java | 9 +++++--- .../refasterrules/ReactorRulesTestOutput.java | 6 ++--- 3 files changed, 13 insertions(+), 24 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 85b0e69ce7..f4a64b61ee 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 @@ -8,7 +8,6 @@ import static reactor.function.TupleUtils.function; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.MoreCollectors; import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; @@ -699,32 +698,19 @@ static final class MonoThen { } } - /** - * Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives. - */ + /** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */ // XXX: Consider creating a plugin that flags/discourages `Mono>` method return // types, just as we discourage nullable `Boolean`s and `Optional`s. static final class MonoCollectToOptional { @BeforeTemplate Mono> before(Mono mono) { - return mono.map(Optional::of).defaultIfEmpty(Optional.empty()); + return Refaster.anyOf( + mono.map(Optional::of).defaultIfEmpty(Optional.empty()), + mono.flux().collect(toOptional())); } @AfterTemplate @UseImportPolicy(STATIC_IMPORT_ALWAYS) - Mono> after(Mono mono) { - return mono.flux().collect(toOptional()); - } - } - - /** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */ - static final class MonoSingleOptional { - @BeforeTemplate - Mono> before(Mono mono) { - return mono.flux().collect(toOptional()); - } - - @AfterTemplate Mono> after(Mono mono) { return mono.singleOptional(); } 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 25596c5cc3..719a73e4ff 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 @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refasterrules; +import static com.google.common.collect.MoreCollectors.toOptional; import static java.util.Comparator.reverseOrder; import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; @@ -23,7 +24,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class); + return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class, toOptional()); } ImmutableSet> testMonoFromSupplier() { @@ -229,8 +230,10 @@ Mono testMonoThen() { return Mono.just("foo").flux().then(); } - Mono> testMonoCollectToOptional() { - return Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()); + ImmutableSet>> testMonoCollectToOptional() { + return ImmutableSet.of( + Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()), + Mono.just("bar").flux().collect(toOptional())); } Mono testMonoCast() { 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 7c471e5314..e95f3297a8 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 @@ -26,7 +26,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class); + return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class, toOptional()); } ImmutableSet> testMonoFromSupplier() { @@ -224,8 +224,8 @@ Mono testMonoThen() { return Mono.just("foo").then(); } - Mono> testMonoCollectToOptional() { - return Mono.just("foo").flux().collect(toOptional()); + ImmutableSet>> testMonoCollectToOptional() { + return ImmutableSet.of(Mono.just("foo").singleOptional(), Mono.just("bar").singleOptional()); } Mono testMonoCast() { From d13a5edc89f9d5120f7a11bdac41d9e9c2084b56 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 20 Feb 2023 10:49:57 +0100 Subject: [PATCH 4/5] How could I miss this --- .../tech/picnic/errorprone/refasterrules/ReactorRules.java | 6 +++--- .../errorprone/refasterrules/ReactorRulesTestInput.java | 2 +- .../errorprone/refasterrules/ReactorRulesTestOutput.java | 2 +- 3 files changed, 5 insertions(+), 5 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 f4a64b61ee..b7017340bc 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 @@ -701,12 +701,12 @@ static final class MonoThen { /** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */ // XXX: Consider creating a plugin that flags/discourages `Mono>` method return // types, just as we discourage nullable `Boolean`s and `Optional`s. - static final class MonoCollectToOptional { + static final class MonoSingleOptional { @BeforeTemplate Mono> before(Mono mono) { return Refaster.anyOf( - mono.map(Optional::of).defaultIfEmpty(Optional.empty()), - mono.flux().collect(toOptional())); + mono.flux().collect(toOptional()), + mono.map(Optional::of).defaultIfEmpty(Optional.empty())); } @AfterTemplate 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 719a73e4ff..02a3abad19 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 @@ -230,7 +230,7 @@ Mono testMonoThen() { return Mono.just("foo").flux().then(); } - ImmutableSet>> testMonoCollectToOptional() { + ImmutableSet>> testMonoSingleOptional() { return ImmutableSet.of( Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()), Mono.just("bar").flux().collect(toOptional())); 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 e95f3297a8..fee65bae6b 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 @@ -224,7 +224,7 @@ Mono testMonoThen() { return Mono.just("foo").then(); } - ImmutableSet>> testMonoCollectToOptional() { + ImmutableSet>> testMonoSingleOptional() { return ImmutableSet.of(Mono.just("foo").singleOptional(), Mono.just("bar").singleOptional()); } From b11d288091fae49b5e215331c53ac6c80489e7da Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 20 Feb 2023 16:23:56 +0100 Subject: [PATCH 5/5] Part 2 --- .../errorprone/refasterrules/ReactorRulesTestInput.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 02a3abad19..7c8af048be 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 @@ -232,8 +232,8 @@ Mono testMonoThen() { ImmutableSet>> testMonoSingleOptional() { return ImmutableSet.of( - Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()), - Mono.just("bar").flux().collect(toOptional())); + Mono.just("foo").flux().collect(toOptional()), + Mono.just("bar").map(Optional::of).defaultIfEmpty(Optional.empty())); } Mono testMonoCast() {