-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
MicrometerRules
Refaster rule collection (#1365)
- Loading branch information
1 parent
be6b17b
commit 56b60f5
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MicrometerRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...rib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ib/src/test/resources/tech/picnic/errorprone/refasterrules/MicrometerRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters