From 880cbe5672109e3674c8b187c10856c7528c7772 Mon Sep 17 00:00:00 2001 From: Danylo Naumenko Date: Tue, 22 Oct 2024 13:30:20 +0200 Subject: [PATCH 1/3] Introduce MicrometerRules Refaster rule collection --- error-prone-contrib/pom.xml | 5 ++ .../refasterrules/MicrometerRules.java | 85 +++++++++++++++++++ .../refasterrules/RefasterRulesTest.java | 1 + .../MicrometerRulesTestInput.java | 60 +++++++++++++ .../MicrometerRulesTestOutput.java | 60 +++++++++++++ pom.xml | 5 ++ 6 files changed, 216 insertions(+) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 385f0867ac..f33573f4ca 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -87,6 +87,11 @@ guava provided + + io.micrometer + micrometer-core + provided + io.projectreactor reactor-core diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java new file mode 100644 index 0000000000..e81376a947 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java @@ -0,0 +1,85 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableCollection; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.refaster.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to expressions dealing with Micrometer. */ +@OnlineDocumentation +final class MicrometerRules { + private MicrometerRules() {} + + /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + static final class TagsOf1 { + @BeforeTemplate + ImmutableCollection before(Tag tag) { + return Refaster.anyOf(ImmutableSet.of(tag), ImmutableList.of(tag)); + } + + @AfterTemplate + Iterable after(Tag tag) { + return Tags.of(tag); + } + } + + /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + static final class TagsOf2 { + @BeforeTemplate + ImmutableCollection before(Tag tag1, Tag tag2) { + return Refaster.anyOf(ImmutableSet.of(tag1, tag2), ImmutableList.of(tag1, tag2)); + } + + @AfterTemplate + Iterable after(Tag tag1, Tag tag2) { + return Tags.of(tag1, tag2); + } + } + + /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + static final class TagsOf3 { + @BeforeTemplate + ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3) { + return Refaster.anyOf(ImmutableSet.of(tag1, tag2, tag3), ImmutableList.of(tag1, tag2, tag3)); + } + + @AfterTemplate + Iterable after(Tag tag1, Tag tag2, Tag tag3) { + return Tags.of(tag1, tag2, tag3); + } + } + + /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + static final class TagsOf4 { + @BeforeTemplate + ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3, Tag tag4) { + return Refaster.anyOf( + ImmutableSet.of(tag1, tag2, tag3, tag4), ImmutableList.of(tag1, tag2, tag3, tag4)); + } + + @AfterTemplate + Iterable after(Tag tag1, Tag tag2, Tag tag3, Tag tag4) { + return Tags.of(tag1, tag2, tag3, tag4); + } + } + + /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + static final class TagsOf5 { + @BeforeTemplate + ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3, Tag tag4, Tag tag5) { + return Refaster.anyOf( + ImmutableSet.of(tag1, tag2, tag3, tag4, tag5), + ImmutableList.of(tag1, tag2, tag3, tag4, tag5)); + } + + @AfterTemplate + Iterable after(Tag tag1, Tag tag2, Tag tag3, Tag tag4, Tag tag5) { + return Tags.of(tag1, tag2, tag3, tag4, tag5); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index 30839af965..fd5cb29672 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -59,6 +59,7 @@ final class RefasterRulesTest { LongStreamRules.class, MapEntryRules.class, MapRules.class, + MicrometerRules.class, MockitoRules.class, MultimapRules.class, NullRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java new file mode 100644 index 0000000000..fff374eb96 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java @@ -0,0 +1,60 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import io.micrometer.core.instrument.Tag; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class MicrometerRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(ImmutableList.class); + } + + ImmutableSet> testTagsOf1() { + return ImmutableSet.of( + ImmutableSet.of(Tag.of("foo", "bar")), ImmutableList.of(Tag.of("foo", "bar"))); + } + + ImmutableSet> testTagsOf2() { + return ImmutableSet.of( + ImmutableSet.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1")), + ImmutableList.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"))); + } + + ImmutableSet> testTagsOf3() { + return ImmutableSet.of( + ImmutableSet.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2")), + ImmutableList.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2"))); + } + + ImmutableSet> testTagsOf4() { + return ImmutableSet.of( + ImmutableSet.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3")), + ImmutableList.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"))); + } + + ImmutableSet> testTagsOf5() { + return ImmutableSet.of( + ImmutableSet.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"), + Tag.of("foo4", "bar4")), + ImmutableList.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"), + Tag.of("foo4", "bar4"))); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java new file mode 100644 index 0000000000..839cf781d8 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java @@ -0,0 +1,60 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class MicrometerRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(ImmutableList.class); + } + + ImmutableSet> testTagsOf1() { + return ImmutableSet.of(Tags.of(Tag.of("foo", "bar")), Tags.of(Tag.of("foo", "bar"))); + } + + ImmutableSet> testTagsOf2() { + return ImmutableSet.of( + Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1")), + Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"))); + } + + ImmutableSet> testTagsOf3() { + return ImmutableSet.of( + Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2")), + Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2"))); + } + + ImmutableSet> testTagsOf4() { + return ImmutableSet.of( + Tags.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3")), + Tags.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"))); + } + + ImmutableSet> testTagsOf5() { + return ImmutableSet.of( + Tags.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"), + Tag.of("foo4", "bar4")), + Tags.of( + Tag.of("foo", "bar"), + Tag.of("foo1", "bar1"), + Tag.of("foo2", "bar2"), + Tag.of("foo3", "bar3"), + Tag.of("foo4", "bar4"))); + } +} diff --git a/pom.xml b/pom.xml index c0764d508d..07f4d433d7 100644 --- a/pom.xml +++ b/pom.xml @@ -365,6 +365,11 @@ nullaway ${version.nullaway} + + io.micrometer + micrometer-core + 1.13.6 + io.projectreactor reactor-bom From 6c562669caa72c2a6079cd9ae56e9b0314ca2694 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 23 Oct 2024 09:57:08 +0200 Subject: [PATCH 2/3] Suggestions --- .../refasterrules/MicrometerRules.java | 10 ++--- .../MicrometerRulesTestInput.java | 43 +++++++++--------- .../MicrometerRulesTestOutput.java | 44 +++++++++---------- 3 files changed, 45 insertions(+), 52 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java index e81376a947..153527adcb 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java @@ -15,7 +15,7 @@ final class MicrometerRules { private MicrometerRules() {} - /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + /** Prefer using {@link Tags} over other immutable collections. */ static final class TagsOf1 { @BeforeTemplate ImmutableCollection before(Tag tag) { @@ -28,7 +28,7 @@ Iterable after(Tag tag) { } } - /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + /** Prefer using {@link Tags} over other immutable collections. */ static final class TagsOf2 { @BeforeTemplate ImmutableCollection before(Tag tag1, Tag tag2) { @@ -41,7 +41,7 @@ Iterable after(Tag tag1, Tag tag2) { } } - /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + /** Prefer using {@link Tags} over other immutable collections. */ static final class TagsOf3 { @BeforeTemplate ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3) { @@ -54,7 +54,7 @@ Iterable after(Tag tag1, Tag tag2, Tag tag3) { } } - /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + /** Prefer using {@link Tags} over other immutable collections. */ static final class TagsOf4 { @BeforeTemplate ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3, Tag tag4) { @@ -68,7 +68,7 @@ Iterable after(Tag tag1, Tag tag2, Tag tag3, Tag tag4) { } } - /** Prefer {@link Tags} over other immutable {@link Iterable}'s. */ + /** Prefer using {@link Tags} over other immutable collections. */ static final class TagsOf5 { @BeforeTemplate ImmutableCollection before(Tag tag1, Tag tag2, Tag tag3, Tag tag4, Tag tag5) { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java index fff374eb96..256c9494bb 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java @@ -13,48 +13,45 @@ public ImmutableSet elidedTypesAndStaticImports() { ImmutableSet> testTagsOf1() { return ImmutableSet.of( - ImmutableSet.of(Tag.of("foo", "bar")), ImmutableList.of(Tag.of("foo", "bar"))); + ImmutableSet.of(Tag.of("foo", "v1")), ImmutableList.of(Tag.of("bar", "v2"))); } ImmutableSet> testTagsOf2() { return ImmutableSet.of( - ImmutableSet.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1")), - ImmutableList.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"))); + ImmutableSet.of(Tag.of("foo", "v1"), Tag.of("bar", "v2")), + ImmutableList.of(Tag.of("baz", "v3"), Tag.of("qux", "v4"))); } ImmutableSet> testTagsOf3() { return ImmutableSet.of( - ImmutableSet.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2")), - ImmutableList.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2"))); + ImmutableSet.of(Tag.of("foo", "v1"), Tag.of("bar", "v2"), Tag.of("baz", "v3")), + ImmutableList.of(Tag.of("qux", "v4"), Tag.of("quux", "v5"), Tag.of("corge", "v6"))); } ImmutableSet> testTagsOf4() { return ImmutableSet.of( ImmutableSet.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3")), + Tag.of("foo", "v1"), Tag.of("bar", "v2"), Tag.of("baz", "v3"), Tag.of("qux", "v4")), ImmutableList.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"))); + Tag.of("quux", "v5"), + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"))); } ImmutableSet> testTagsOf5() { return ImmutableSet.of( ImmutableSet.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"), - Tag.of("foo4", "bar4")), + Tag.of("foo", "v1"), + Tag.of("bar", "v2"), + Tag.of("baz", "v3"), + Tag.of("qux", "v4"), + Tag.of("quux", "v5")), ImmutableList.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"), - Tag.of("foo4", "bar4"))); + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"), + Tag.of("waldo", "v9"), + Tag.of("fred", "v10"))); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java index 839cf781d8..74c4dd50c8 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java @@ -13,48 +13,44 @@ public ImmutableSet elidedTypesAndStaticImports() { } ImmutableSet> testTagsOf1() { - return ImmutableSet.of(Tags.of(Tag.of("foo", "bar")), Tags.of(Tag.of("foo", "bar"))); + return ImmutableSet.of(Tags.of(Tag.of("foo", "v1")), Tags.of(Tag.of("bar", "v2"))); } ImmutableSet> testTagsOf2() { return ImmutableSet.of( - Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1")), - Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"))); + Tags.of(Tag.of("foo", "v1"), Tag.of("bar", "v2")), + Tags.of(Tag.of("baz", "v3"), Tag.of("qux", "v4"))); } ImmutableSet> testTagsOf3() { return ImmutableSet.of( - Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2")), - Tags.of(Tag.of("foo", "bar"), Tag.of("foo1", "bar1"), Tag.of("foo2", "bar2"))); + Tags.of(Tag.of("foo", "v1"), Tag.of("bar", "v2"), Tag.of("baz", "v3")), + Tags.of(Tag.of("qux", "v4"), Tag.of("quux", "v5"), Tag.of("corge", "v6"))); } ImmutableSet> testTagsOf4() { return ImmutableSet.of( + Tags.of(Tag.of("foo", "v1"), Tag.of("bar", "v2"), Tag.of("baz", "v3"), Tag.of("qux", "v4")), Tags.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3")), - Tags.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"))); + Tag.of("quux", "v5"), + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"))); } ImmutableSet> testTagsOf5() { return ImmutableSet.of( Tags.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"), - Tag.of("foo4", "bar4")), + Tag.of("foo", "v1"), + Tag.of("bar", "v2"), + Tag.of("baz", "v3"), + Tag.of("qux", "v4"), + Tag.of("quux", "v5")), Tags.of( - Tag.of("foo", "bar"), - Tag.of("foo1", "bar1"), - Tag.of("foo2", "bar2"), - Tag.of("foo3", "bar3"), - Tag.of("foo4", "bar4"))); + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"), + Tag.of("waldo", "v9"), + Tag.of("fred", "v10"))); } } From f6caa703498e7f766fa463d289ae731ee84b5282 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 26 Oct 2024 14:36:53 +0200 Subject: [PATCH 3/3] Suggestion --- .../tech/picnic/errorprone/refasterrules/MicrometerRules.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java index 153527adcb..40800c6d09 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java @@ -11,6 +11,9 @@ import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** Refaster rules related to expressions dealing with Micrometer. */ +// XXX: Consider replacing the `TagsOf[N]` rules with a bug checker, so that various other +// expressions (e.g. those creating other collection types, those passing in tags some other way, or +// those passing in more tags) can be replaced as wel. @OnlineDocumentation final class MicrometerRules { private MicrometerRules() {}