From 4ac184a4d2815cfb864cfca760385b283033696f Mon Sep 17 00:00:00 2001 From: Philip Leonard Date: Fri, 26 May 2023 13:05:08 +0200 Subject: [PATCH 1/6] Introduce a BugCheck for the useage of Mongo full text searches via the Java driver --- error-prone-contrib/pom.xml | 5 ++ .../MongoFullTextSearchQueryUsage.java | 49 +++++++++++++++++++ .../bugpatterns/util/ThirdPartyLibrary.java | 11 +++++ .../MongoFullTextSearchQueryUsageTest.java | 28 +++++++++++ .../util/ThirdPartyLibraryTest.java | 18 ++++--- pom.xml | 12 +++++ 6 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java create mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 18dd0e6d78..75a31b47b3 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -176,6 +176,11 @@ mockito-core provided + + org.mongodb + mongodb-driver-core + test + org.reactivestreams reactive-streams diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java new file mode 100644 index 0000000000..55086f74c9 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java @@ -0,0 +1,49 @@ +package tech.picnic.errorprone.bugpatterns; + +import static com.google.errorprone.BugPattern.LinkType.CUSTOM; +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import static com.google.errorprone.BugPattern.StandardTags.PERFORMANCE; +import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; +import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; + +import com.google.auto.service.AutoService; +import com.google.errorprone.BugPattern; +import com.google.errorprone.VisitorState; +import com.google.errorprone.bugpatterns.BugChecker; +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; +import com.google.errorprone.matchers.Description; +import com.google.errorprone.matchers.Matcher; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.MethodInvocationTree; +import tech.picnic.errorprone.bugpatterns.util.ThirdPartyLibrary; + +/** + * A {@link BugChecker} that flags usages of Mongo $text filters used for full text searches. + * + * @see Mongo Text Search + */ +@AutoService(BugChecker.class) +@BugPattern( + summary = "Usage of the Mongo FTS ($text) queries is discouraged", + link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", + linkType = CUSTOM, + severity = WARNING, + tags = PERFORMANCE) +public final class MongoFullTextSearchQueryUsage extends BugChecker + implements MethodInvocationTreeMatcher { + private static final long serialVersionUID = 1L; + private static final Matcher TEXT_FILTER_INVOCATION = + staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); + + /** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */ + public MongoFullTextSearchQueryUsage() {} + + @Override + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { + if (!ThirdPartyLibrary.MONGO.isIntroductionAllowed(state) + || !TEXT_FILTER_INVOCATION.matches(tree, state)) { + return Description.NO_MATCH; + } + return describeMatch(tree); + } +} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java index ad47462880..29384aa644 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java @@ -32,6 +32,17 @@ public enum ThirdPartyLibrary { * @see Guava on GitHub */ GUAVA("com.google.common.collect.ImmutableList"), + + /** + * Mongo's Java driver Filters API. + * + * @see MongoDB + * Filters API + * @see MongoDB Java drvier + */ + MONGO("com.mongodb.client.model.Filters"), + /** * New Relic's Java agent API. * diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java new file mode 100644 index 0000000000..fb5e28c5ef --- /dev/null +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java @@ -0,0 +1,28 @@ +package tech.picnic.errorprone.bugpatterns; + +import com.google.errorprone.CompilationTestHelper; +import org.junit.jupiter.api.Test; + +final class MongoFullTextSearchQueryUsageTest { + @Test + void identification() { + CompilationTestHelper.newInstance(MongoFullTextSearchQueryUsage.class, getClass()) + .addSourceLines( + "A.java", + "import com.mongodb.client.model.Filters;", + "import com.mongodb.client.model.TextSearchOptions;", + "import org.bson.conversions.Bson;", + "", + "class A {", + "", + " void m() {", + " Bson allowed = Filters.eq(\"a\", \"b\");", + " // BUG: Diagnostic contains:", + " Bson textSearch = Filters.text(\"Some text\");", + " // BUG: Diagnostic contains:", + " Bson textSearch2 = Filters.text(\"Some text\", new TextSearchOptions().caseSensitive(true));", + " }", + "}") + .doTest(); + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java index 090ef579b5..417a443b36 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java @@ -23,7 +23,8 @@ void isIntroductionAllowed() { CompilationTestHelper.newInstance(TestChecker.class, getClass()) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, MONGO: true, NEW_RELIC_AGENT_API: true,", + "// REACTOR: true", "class A {}") .doTest(); } @@ -34,15 +35,18 @@ void isIntroductionAllowedWitnessClassesInSymtab() { .addSourceLines( "A.java", "import com.google.common.collect.ImmutableList;", + "import com.mongodb.client.model.Filters;", "import com.newrelic.api.agent.Agent;", "import org.assertj.core.api.Assertions;", "import reactor.core.publisher.Flux;", "", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, MONGO: true, NEW_RELIC_AGENT_API: true,", + "// REACTOR: true", "class A {", " void m(Class clazz) {", " m(Assertions.class);", " m(ImmutableList.class);", + " m(Filters.class);", " m(Agent.class);", " m(Flux.class);", " }", @@ -56,7 +60,8 @@ void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() { .withClasspath(ImmutableList.class, Flux.class) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false, REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, MONGO: false, NEW_RELIC_AGENT_API: false,", + "// REACTOR: true", "class A {}") .doTest(); } @@ -67,7 +72,8 @@ void isIntroductionAllowedWitnessClassesNotOnClassPath() { .withClasspath() .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false, REACTOR:", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, MONGO: false, NEW_RELIC_AGENT_API: false,", + "// REACTOR:", "// false", "class A {}") .doTest(); @@ -82,8 +88,8 @@ void isIntroductionAllowedIgnoreClasspathCompat(boolean ignoreClassPath) { .addSourceLines( "A.java", String.format( - "// BUG: Diagnostic contains: ASSERTJ: %s, GUAVA: true, NEW_RELIC_AGENT_API: %s, REACTOR: true", - ignoreClassPath, ignoreClassPath), + "// BUG: Diagnostic contains: ASSERTJ: %s, GUAVA: true, MONGO: %s, NEW_RELIC_AGENT_API: %s, REACTOR: true", + ignoreClassPath, ignoreClassPath, ignoreClassPath), "class A {}") .doTest(); } diff --git a/pom.xml b/pom.xml index b8855c75ea..77b427c129 100644 --- a/pom.xml +++ b/pom.xml @@ -443,6 +443,11 @@ pom import + + org.mongodb + mongodb-driver-core + 4.9.1 + org.slf4j slf4j-api @@ -1019,6 +1024,13 @@ ${version.jdk} + + + org.bson.codecs.record.* + true From c07e073b45cbd3879b1baa2957efeedeef37e1ff Mon Sep 17 00:00:00 2001 From: Philip Leonard Date: Fri, 26 May 2023 13:27:23 +0200 Subject: [PATCH 2/6] Exclude the org.mongodb:bson-record-codec dependency --- pom.xml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 77b427c129..dc38278d6a 100644 --- a/pom.xml +++ b/pom.xml @@ -447,6 +447,16 @@ org.mongodb mongodb-driver-core 4.9.1 + + + + org.mongodb + bson-record-codec + + org.slf4j @@ -1024,13 +1034,6 @@ ${version.jdk} - - - org.bson.codecs.record.* - true From f77e129b80d5bfa402ebf190b53ba43c753919f8 Mon Sep 17 00:00:00 2001 From: Philip Leonard Date: Fri, 26 May 2023 13:59:35 +0200 Subject: [PATCH 3/6] Better summary --- .../errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java index 55086f74c9..4b7db6bbbb 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java @@ -24,7 +24,8 @@ */ @AutoService(BugChecker.class) @BugPattern( - summary = "Usage of the Mongo FTS ($text) queries is discouraged", + summary = + "Usage of Mongo for full-text search queries via the `$text` operator is discouraged.", link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", linkType = CUSTOM, severity = WARNING, From 25d80f83fe3c0cee474123c64056cfaee9d2ef57 Mon Sep 17 00:00:00 2001 From: Philip Leonard Date: Fri, 26 May 2023 14:47:56 +0200 Subject: [PATCH 4/6] Drop enumeration of Mongo core in `ThirdPartyLibrary` --- .../bugpatterns/MongoFullTextSearchQueryUsage.java | 4 +--- .../bugpatterns/util/ThirdPartyLibrary.java | 10 ---------- .../bugpatterns/util/ThirdPartyLibraryTest.java | 14 ++++++-------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java index 4b7db6bbbb..703ef11c6f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java @@ -15,7 +15,6 @@ import com.google.errorprone.matchers.Matcher; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodInvocationTree; -import tech.picnic.errorprone.bugpatterns.util.ThirdPartyLibrary; /** * A {@link BugChecker} that flags usages of Mongo $text filters used for full text searches. @@ -41,8 +40,7 @@ public MongoFullTextSearchQueryUsage() {} @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - if (!ThirdPartyLibrary.MONGO.isIntroductionAllowed(state) - || !TEXT_FILTER_INVOCATION.matches(tree, state)) { + if (!TEXT_FILTER_INVOCATION.matches(tree, state)) { return Description.NO_MATCH; } return describeMatch(tree); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java index 29384aa644..9138797020 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java @@ -33,16 +33,6 @@ public enum ThirdPartyLibrary { */ GUAVA("com.google.common.collect.ImmutableList"), - /** - * Mongo's Java driver Filters API. - * - * @see MongoDB - * Filters API - * @see MongoDB Java drvier - */ - MONGO("com.mongodb.client.model.Filters"), - /** * New Relic's Java agent API. * diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java index 417a443b36..053dc76f17 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java @@ -23,7 +23,7 @@ void isIntroductionAllowed() { CompilationTestHelper.newInstance(TestChecker.class, getClass()) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, MONGO: true, NEW_RELIC_AGENT_API: true,", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", "// REACTOR: true", "class A {}") .doTest(); @@ -35,18 +35,16 @@ void isIntroductionAllowedWitnessClassesInSymtab() { .addSourceLines( "A.java", "import com.google.common.collect.ImmutableList;", - "import com.mongodb.client.model.Filters;", "import com.newrelic.api.agent.Agent;", "import org.assertj.core.api.Assertions;", "import reactor.core.publisher.Flux;", "", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, MONGO: true, NEW_RELIC_AGENT_API: true,", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", "// REACTOR: true", "class A {", " void m(Class clazz) {", " m(Assertions.class);", " m(ImmutableList.class);", - " m(Filters.class);", " m(Agent.class);", " m(Flux.class);", " }", @@ -60,7 +58,7 @@ void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() { .withClasspath(ImmutableList.class, Flux.class) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, MONGO: false, NEW_RELIC_AGENT_API: false,", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false,", "// REACTOR: true", "class A {}") .doTest(); @@ -72,7 +70,7 @@ void isIntroductionAllowedWitnessClassesNotOnClassPath() { .withClasspath() .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, MONGO: false, NEW_RELIC_AGENT_API: false,", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false,", "// REACTOR:", "// false", "class A {}") @@ -88,8 +86,8 @@ void isIntroductionAllowedIgnoreClasspathCompat(boolean ignoreClassPath) { .addSourceLines( "A.java", String.format( - "// BUG: Diagnostic contains: ASSERTJ: %s, GUAVA: true, MONGO: %s, NEW_RELIC_AGENT_API: %s, REACTOR: true", - ignoreClassPath, ignoreClassPath, ignoreClassPath), + "// BUG: Diagnostic contains: ASSERTJ: %s, GUAVA: true, NEW_RELIC_AGENT_API: %s, REACTOR: true", + ignoreClassPath, ignoreClassPath), "class A {}") .doTest(); } From 1e8a07624fa50585d68b610a18e78090b62cf8ad Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Sat, 10 Jun 2023 19:43:47 +0200 Subject: [PATCH 5/6] Suggestions and partially revert unrelated changes --- .../MongoFullTextSearchQueryUsage.java | 19 +++++++++---------- .../bugpatterns/util/ThirdPartyLibrary.java | 1 - .../MongoFullTextSearchQueryUsageTest.java | 10 +++++----- .../util/ThirdPartyLibraryTest.java | 12 ++++-------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java index 703ef11c6f..60dbdab850 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java @@ -1,7 +1,7 @@ package tech.picnic.errorprone.bugpatterns; import static com.google.errorprone.BugPattern.LinkType.CUSTOM; -import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import static com.google.errorprone.BugPattern.StandardTags.PERFORMANCE; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; @@ -17,22 +17,22 @@ import com.sun.source.tree.MethodInvocationTree; /** - * A {@link BugChecker} that flags usages of Mongo $text filters used for full text searches. + * A {@link BugChecker} that flags usages of Mongo {@code $text} filters used for full text + * searches. * * @see Mongo Text Search */ @AutoService(BugChecker.class) @BugPattern( - summary = - "Usage of Mongo for full-text search queries via the `$text` operator is discouraged.", + summary = "Avoid the `$text` operator in Mongo's full-text search queries", link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", linkType = CUSTOM, - severity = WARNING, + severity = SUGGESTION, tags = PERFORMANCE) public final class MongoFullTextSearchQueryUsage extends BugChecker implements MethodInvocationTreeMatcher { private static final long serialVersionUID = 1L; - private static final Matcher TEXT_FILTER_INVOCATION = + private static final Matcher MONGO_FILTERS_TEXT_METHOD = staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); /** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */ @@ -40,9 +40,8 @@ public MongoFullTextSearchQueryUsage() {} @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - if (!TEXT_FILTER_INVOCATION.matches(tree, state)) { - return Description.NO_MATCH; - } - return describeMatch(tree); + return MONGO_FILTERS_TEXT_METHOD.matches(tree, state) + ? describeMatch(tree) + : Description.NO_MATCH; } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java index 9138797020..ad47462880 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java @@ -32,7 +32,6 @@ public enum ThirdPartyLibrary { * @see Guava on GitHub */ GUAVA("com.google.common.collect.ImmutableList"), - /** * New Relic's Java agent API. * diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java index fb5e28c5ef..2677c0dbb4 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java @@ -11,16 +11,16 @@ void identification() { "A.java", "import com.mongodb.client.model.Filters;", "import com.mongodb.client.model.TextSearchOptions;", - "import org.bson.conversions.Bson;", "", "class A {", - "", " void m() {", - " Bson allowed = Filters.eq(\"a\", \"b\");", + " Filters.eq(\"foo\", \"bar\");", + " // BUG: Diagnostic contains:", + " Filters.text(\"foo\");", " // BUG: Diagnostic contains:", - " Bson textSearch = Filters.text(\"Some text\");", + " Filters.text(\"foo\", new TextSearchOptions());", " // BUG: Diagnostic contains:", - " Bson textSearch2 = Filters.text(\"Some text\", new TextSearchOptions().caseSensitive(true));", + " Filters.text(\"foo\", new TextSearchOptions().caseSensitive(true));", " }", "}") .doTest(); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java index 053dc76f17..090ef579b5 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java @@ -23,8 +23,7 @@ void isIntroductionAllowed() { CompilationTestHelper.newInstance(TestChecker.class, getClass()) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", "class A {}") .doTest(); } @@ -39,8 +38,7 @@ void isIntroductionAllowedWitnessClassesInSymtab() { "import org.assertj.core.api.Assertions;", "import reactor.core.publisher.Flux;", "", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", "class A {", " void m(Class clazz) {", " m(Assertions.class);", @@ -58,8 +56,7 @@ void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() { .withClasspath(ImmutableList.class, Flux.class) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false, REACTOR: true", "class A {}") .doTest(); } @@ -70,8 +67,7 @@ void isIntroductionAllowedWitnessClassesNotOnClassPath() { .withClasspath() .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false,", - "// REACTOR:", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false, REACTOR:", "// false", "class A {}") .doTest(); From 521b1b9ee9970e062fdbd78271afa3e461477c1c Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 23 Jul 2023 13:01:51 +0200 Subject: [PATCH 6/6] Suggestions --- ...eryUsage.java => MongoDBTextFilterUsage.java} | 16 ++++++++-------- ...Test.java => MongoDBTextFilterUsageTest.java} | 6 ++---- pom.xml | 8 +++----- 3 files changed, 13 insertions(+), 17 deletions(-) rename error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/{MongoFullTextSearchQueryUsage.java => MongoDBTextFilterUsage.java} (76%) rename error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/{MongoFullTextSearchQueryUsageTest.java => MongoDBTextFilterUsageTest.java} (72%) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsage.java similarity index 76% rename from error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java rename to error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsage.java index 60dbdab850..77208f673e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsage.java @@ -17,26 +17,26 @@ import com.sun.source.tree.MethodInvocationTree; /** - * A {@link BugChecker} that flags usages of Mongo {@code $text} filters used for full text - * searches. + * A {@link BugChecker} that flags usages of MongoDB {@code $text} filter usages. * - * @see Mongo Text Search + * @see MongoDB Text Search */ @AutoService(BugChecker.class) @BugPattern( - summary = "Avoid the `$text` operator in Mongo's full-text search queries", - link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", + summary = + "Avoid MongoDB's `$text` filter operator, as it can trigger heavy queries and even cause the server to run out of memory", + link = BUG_PATTERNS_BASE_URL + "MongoDBTextFilterUsage", linkType = CUSTOM, severity = SUGGESTION, tags = PERFORMANCE) -public final class MongoFullTextSearchQueryUsage extends BugChecker +public final class MongoDBTextFilterUsage extends BugChecker implements MethodInvocationTreeMatcher { private static final long serialVersionUID = 1L; private static final Matcher MONGO_FILTERS_TEXT_METHOD = staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); - /** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */ - public MongoFullTextSearchQueryUsage() {} + /** Instantiates a new {@link MongoDBTextFilterUsage} instance. */ + public MongoDBTextFilterUsage() {} @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsageTest.java similarity index 72% rename from error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java rename to error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsageTest.java index 2677c0dbb4..ea9ca288a8 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsageTest.java @@ -3,10 +3,10 @@ import com.google.errorprone.CompilationTestHelper; import org.junit.jupiter.api.Test; -final class MongoFullTextSearchQueryUsageTest { +final class MongoDBTextFilterUsageTest { @Test void identification() { - CompilationTestHelper.newInstance(MongoFullTextSearchQueryUsage.class, getClass()) + CompilationTestHelper.newInstance(MongoDBTextFilterUsage.class, getClass()) .addSourceLines( "A.java", "import com.mongodb.client.model.Filters;", @@ -19,8 +19,6 @@ void identification() { " Filters.text(\"foo\");", " // BUG: Diagnostic contains:", " Filters.text(\"foo\", new TextSearchOptions());", - " // BUG: Diagnostic contains:", - " Filters.text(\"foo\", new TextSearchOptions().caseSensitive(true));", " }", "}") .doTest(); diff --git a/pom.xml b/pom.xml index dc38278d6a..265360736b 100644 --- a/pom.xml +++ b/pom.xml @@ -446,11 +446,9 @@ org.mongodb mongodb-driver-core - 4.9.1 - + 4.10.2 + org.mongodb