From 56b60f5cf63854e8c1a8c269ca8b10014ca79486 Mon Sep 17 00:00:00 2001 From: Danylo Naumenko Date: Sat, 26 Oct 2024 14:56:11 +0200 Subject: [PATCH] Introduce `MicrometerRules` Refaster rule collection (#1365) --- error-prone-contrib/pom.xml | 5 ++ .../refasterrules/MicrometerRules.java | 88 +++++++++++++++++++ .../refasterrules/RefasterRulesTest.java | 1 + .../MicrometerRulesTestInput.java | 57 ++++++++++++ .../MicrometerRulesTestOutput.java | 56 ++++++++++++ pom.xml | 5 ++ 6 files changed, 212 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..40800c6d09 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java @@ -0,0 +1,88 @@ +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. */ +// 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() {} + + /** Prefer using {@link Tags} over other immutable collections. */ + 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 using {@link Tags} over other immutable collections. */ + 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 using {@link Tags} over other immutable collections. */ + 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 using {@link Tags} over other immutable collections. */ + 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 using {@link Tags} over other immutable collections. */ + 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..256c9494bb --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java @@ -0,0 +1,57 @@ +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", "v1")), ImmutableList.of(Tag.of("bar", "v2"))); + } + + ImmutableSet> testTagsOf2() { + return ImmutableSet.of( + 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", "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", "v1"), Tag.of("bar", "v2"), Tag.of("baz", "v3"), Tag.of("qux", "v4")), + ImmutableList.of( + 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", "v1"), + Tag.of("bar", "v2"), + Tag.of("baz", "v3"), + Tag.of("qux", "v4"), + Tag.of("quux", "v5")), + ImmutableList.of( + 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 new file mode 100644 index 0000000000..74c4dd50c8 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java @@ -0,0 +1,56 @@ +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", "v1")), Tags.of(Tag.of("bar", "v2"))); + } + + ImmutableSet> testTagsOf2() { + return ImmutableSet.of( + 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", "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("quux", "v5"), + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"))); + } + + ImmutableSet> testTagsOf5() { + return ImmutableSet.of( + Tags.of( + Tag.of("foo", "v1"), + Tag.of("bar", "v2"), + Tag.of("baz", "v3"), + Tag.of("qux", "v4"), + Tag.of("quux", "v5")), + Tags.of( + Tag.of("corge", "v6"), + Tag.of("grault", "v7"), + Tag.of("garply", "v8"), + Tag.of("waldo", "v9"), + Tag.of("fred", "v10"))); + } +} 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