From c7606e893e73959525484d33cabd3cf2bd1c65ab Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Wed, 12 Jun 2024 10:21:33 +0200 Subject: [PATCH 1/2] Introduce MongoDB Refaster rules --- error-prone-contrib/pom.xml | 2 +- .../refasterrules/MongoDBRules.java | 31 +++++++++++++++++++ .../refasterrules/RefasterRulesTest.java | 1 + .../refasterrules/MongoDBRulesTestInput.java | 13 ++++++++ .../refasterrules/MongoDBRulesTestOutput.java | 13 ++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MongoDBRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestOutput.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 122e0c8a18..76e2a7da9a 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -177,7 +177,7 @@ org.mongodb mongodb-driver-core - test + provided org.openrewrite diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MongoDBRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MongoDBRules.java new file mode 100644 index 0000000000..16f5ee0b90 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MongoDBRules.java @@ -0,0 +1,31 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static com.mongodb.client.model.Filters.eq; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import com.mongodb.client.model.Filters; +import org.bson.conversions.Bson; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to MongoDB client expressions and statements. */ +@OnlineDocumentation +final class MongoDBRules { + private MongoDBRules() {} + + /** Avoid {@link Enum#toString()} invocations when invoking {@link Filters#eq(String, Object)}. */ + static final class Eq { + @BeforeTemplate + Bson before(String field, Enum value) { + return eq(field, value.toString()); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + Bson after(String field, Enum value) { + return eq(field, value); + } + } +} 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 a7c35621a8..f04c294727 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 { MapEntryRules.class, MapRules.class, MockitoRules.class, + MongoDBRules.class, MultimapRules.class, NullRules.class, OptionalRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestInput.java new file mode 100644 index 0000000000..6cc0aa5048 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestInput.java @@ -0,0 +1,13 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.mongodb.client.model.Filters.eq; + +import org.bson.conversions.Bson; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; +import tech.picnic.errorprone.utils.ThirdPartyLibrary; + +final class MongoDBRulesTest implements RefasterRuleCollectionTestCase { + Bson testEq() { + return eq("foo", ThirdPartyLibrary.GUAVA.toString()); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestOutput.java new file mode 100644 index 0000000000..9fae75c176 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MongoDBRulesTestOutput.java @@ -0,0 +1,13 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.mongodb.client.model.Filters.eq; + +import org.bson.conversions.Bson; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; +import tech.picnic.errorprone.utils.ThirdPartyLibrary; + +final class MongoDBRulesTest implements RefasterRuleCollectionTestCase { + Bson testEq() { + return eq("foo", ThirdPartyLibrary.GUAVA); + } +} From c7c1a3d0ed81beb8c76a9969871ced483e7f7983 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Wed, 12 Jun 2024 10:44:54 +0200 Subject: [PATCH 2/2] Add `org.mongodb:bson` dependency --- error-prone-contrib/pom.xml | 5 +++++ pom.xml | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 76e2a7da9a..841344b7cb 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -174,6 +174,11 @@ mockito-core provided + + org.mongodb + bson + provided + org.mongodb mongodb-driver-core diff --git a/pom.xml b/pom.xml index 6595aaeac8..cbfd5c78b2 100644 --- a/pom.xml +++ b/pom.xml @@ -214,6 +214,7 @@ 17 3.9.5 5.12.0 + 5.1.0 1.0.1 0.11.0 1.1.4 @@ -463,10 +464,15 @@ pom import + + org.mongodb + bson + ${version.mongodb} + org.mongodb mongodb-driver-core - 5.1.0 + ${version.mongodb} org.openrewrite