Skip to content

Commit

Permalink
Introduce MicrometerRules Refaster rule collection (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
danylo-naumenko authored Oct 26, 2024
1 parent be6b17b commit 56b60f5
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 0 deletions.
5 changes: 5 additions & 0 deletions error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
<artifactId>guava</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Tag> before(Tag tag) {
return Refaster.anyOf(ImmutableSet.of(tag), ImmutableList.of(tag));
}

@AfterTemplate
Iterable<Tag> after(Tag tag) {
return Tags.of(tag);
}
}

/** Prefer using {@link Tags} over other immutable collections. */
static final class TagsOf2 {
@BeforeTemplate
ImmutableCollection<Tag> before(Tag tag1, Tag tag2) {
return Refaster.anyOf(ImmutableSet.of(tag1, tag2), ImmutableList.of(tag1, tag2));
}

@AfterTemplate
Iterable<Tag> after(Tag tag1, Tag tag2) {
return Tags.of(tag1, tag2);
}
}

/** Prefer using {@link Tags} over other immutable collections. */
static final class TagsOf3 {
@BeforeTemplate
ImmutableCollection<Tag> before(Tag tag1, Tag tag2, Tag tag3) {
return Refaster.anyOf(ImmutableSet.of(tag1, tag2, tag3), ImmutableList.of(tag1, tag2, tag3));
}

@AfterTemplate
Iterable<Tag> 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<Tag> 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<Tag> 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<Tag> 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<Tag> after(Tag tag1, Tag tag2, Tag tag3, Tag tag4, Tag tag5) {
return Tags.of(tag1, tag2, tag3, tag4, tag5);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class RefasterRulesTest {
LongStreamRules.class,
MapEntryRules.class,
MapRules.class,
MicrometerRules.class,
MockitoRules.class,
MultimapRules.class,
NullRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(ImmutableList.class);
}

ImmutableSet<Iterable<Tag>> testTagsOf1() {
return ImmutableSet.of(
ImmutableSet.of(Tag.of("foo", "v1")), ImmutableList.of(Tag.of("bar", "v2")));
}

ImmutableSet<Iterable<Tag>> 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<Iterable<Tag>> 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<Iterable<Tag>> 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<Iterable<Tag>> 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")));
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(ImmutableList.class);
}

ImmutableSet<Iterable<Tag>> testTagsOf1() {
return ImmutableSet.of(Tags.of(Tag.of("foo", "v1")), Tags.of(Tag.of("bar", "v2")));
}

ImmutableSet<Iterable<Tag>> 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<Iterable<Tag>> 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<Iterable<Tag>> 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<Iterable<Tag>> 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")));
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@
<artifactId>nullaway</artifactId>
<version>${version.nullaway}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.13.6</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-bom</artifactId>
Expand Down

0 comments on commit 56b60f5

Please sign in to comment.