From a5ff1d077a746ea97a380ea74abba85f6873aaa6 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Wed, 19 Oct 2022 10:30:57 +0200 Subject: [PATCH 1/4] Introduce Reactor `Context#empty` Refaster rule --- .../errorprone/refasterrules/ReactorRules.java | 15 +++++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 6 ++++++ .../refasterrules/ReactorRulesTestOutput.java | 6 ++++++ 3 files changed, 27 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 500bd40e34..13468c4f3a 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 @@ -5,6 +5,7 @@ import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; +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; @@ -25,6 +26,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.test.publisher.PublisherProbe; +import reactor.util.context.Context; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; import tech.picnic.errorprone.refaster.matchers.ThrowsCheckedException; @@ -375,6 +377,19 @@ Flux after(Flux flux) { } } + /** Prefer {@link reactor.util.context.Context#empty()}} over more verbose alternatives. */ + static final class ContextEmpty { + @BeforeTemplate + Context before() { + return Context.of(ImmutableMap.of()); + } + + @AfterTemplate + Context after() { + return Context.empty(); + } + } + /** 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/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index cbf5cc888f..f22f0a95a6 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 @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.time.Duration; import java.util.Optional; @@ -12,6 +13,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.test.publisher.PublisherProbe; +import reactor.util.context.Context; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @@ -126,6 +128,10 @@ ImmutableSet> testFluxOnErrorComplete() { Flux.just(2).onErrorResume(e -> Flux.empty())); } + Context testContextEmpty() { + return Context.of(ImmutableMap.of()); + } + 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/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index 94daf79ec9..2bcc9c7d02 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 @@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.time.Duration; import java.util.Optional; @@ -14,6 +15,7 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import reactor.test.publisher.PublisherProbe; +import reactor.util.context.Context; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @@ -124,6 +126,10 @@ ImmutableSet> testFluxOnErrorComplete() { return ImmutableSet.of(Flux.just(1).onErrorComplete(), Flux.just(2).onErrorComplete()); } + Context testContextEmpty() { + return Context.empty(); + } + ImmutableSet> testPublisherProbeEmpty() { return ImmutableSet.of(PublisherProbe.empty(), PublisherProbe.empty()); } From fa1c9660dcb38047a86eaed1f5393b57f3082923 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Thu, 20 Oct 2022 14:45:49 +0200 Subject: [PATCH 2/4] Remove unnecessary class type argument :spaghetti: --- .../java/tech/picnic/errorprone/refasterrules/ReactorRules.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 13468c4f3a..37d4f5c5d2 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 @@ -378,7 +378,7 @@ Flux after(Flux flux) { } /** Prefer {@link reactor.util.context.Context#empty()}} over more verbose alternatives. */ - static final class ContextEmpty { + static final class ContextEmpty { @BeforeTemplate Context before() { return Context.of(ImmutableMap.of()); From bca576996beca1da0a56c5586edfc6cf10749781 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Fri, 21 Oct 2022 09:16:20 +0200 Subject: [PATCH 3/4] Add `ImmutableMap` to `elidedTypesAndStaticImports` --- .../picnic/errorprone/refasterrules/ReactorRulesTestInput.java | 2 +- .../picnic/errorprone/refasterrules/ReactorRulesTestOutput.java | 2 +- 2 files 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 f22f0a95a6..30354c89c1 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 @@ -19,7 +19,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0)); + return ImmutableSet.of(assertThat(0), ImmutableMap.class); } ImmutableSet> testMonoFromSupplier() { 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 2bcc9c7d02..37443fd315 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 @@ -21,7 +21,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0)); + return ImmutableSet.of(assertThat(0), ImmutableMap.class); } ImmutableSet> testMonoFromSupplier() { From 6bd2fd5467e5880a9cfb414741e8b75905d262a9 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Fri, 21 Oct 2022 20:13:34 +0200 Subject: [PATCH 4/4] Suggestions --- .../tech/picnic/errorprone/refasterrules/ReactorRules.java | 5 ++++- .../errorprone/refasterrules/ReactorRulesTestInput.java | 7 ++++--- .../errorprone/refasterrules/ReactorRulesTestOutput.java | 7 ++++--- 3 files changed, 12 insertions(+), 7 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 37d4f5c5d2..1b3bf9c3e9 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 @@ -15,6 +15,7 @@ import com.google.errorprone.refaster.annotation.Placeholder; import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.time.Duration; +import java.util.HashMap; import java.util.Optional; import java.util.concurrent.Callable; import java.util.function.Consumer; @@ -378,10 +379,12 @@ Flux after(Flux flux) { } /** 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. static final class ContextEmpty { @BeforeTemplate Context before() { - return Context.of(ImmutableMap.of()); + return Context.of(Refaster.anyOf(new HashMap<>(), ImmutableMap.of())); } @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 30354c89c1..270dce8306 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 @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.time.Duration; +import java.util.HashMap; import java.util.Optional; import java.util.concurrent.Callable; import java.util.function.Supplier; @@ -19,7 +20,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0), ImmutableMap.class); + return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class); } ImmutableSet> testMonoFromSupplier() { @@ -128,8 +129,8 @@ ImmutableSet> testFluxOnErrorComplete() { Flux.just(2).onErrorResume(e -> Flux.empty())); } - Context testContextEmpty() { - return Context.of(ImmutableMap.of()); + ImmutableSet testContextEmpty() { + return ImmutableSet.of(Context.of(new HashMap<>()), Context.of(ImmutableMap.of())); } ImmutableSet> testPublisherProbeEmpty() { 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 37443fd315..2c790c959b 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 @@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.time.Duration; +import java.util.HashMap; import java.util.Optional; import java.util.concurrent.Callable; import java.util.function.Supplier; @@ -21,7 +22,7 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(assertThat(0), ImmutableMap.class); + return ImmutableSet.of(assertThat(0), HashMap.class, ImmutableMap.class); } ImmutableSet> testMonoFromSupplier() { @@ -126,8 +127,8 @@ ImmutableSet> testFluxOnErrorComplete() { return ImmutableSet.of(Flux.just(1).onErrorComplete(), Flux.just(2).onErrorComplete()); } - Context testContextEmpty() { - return Context.empty(); + ImmutableSet testContextEmpty() { + return ImmutableSet.of(Context.empty(), Context.empty()); } ImmutableSet> testPublisherProbeEmpty() {