-
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
MongoDBTextFilterUsage
check (#649)
- Loading branch information
1 parent
b40c5d6
commit e03d0de
Showing
4 changed files
with
91 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
47 changes: 47 additions & 0 deletions
47
...rone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsage.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,47 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
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; | ||
|
||
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; | ||
|
||
/** | ||
* A {@link BugChecker} that flags usages of MongoDB {@code $text} filter usages. | ||
* | ||
* @see <a href="https://www.mongodb.com/docs/manual/text-search/">MongoDB Text Search</a> | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
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 MongoDBTextFilterUsage extends BugChecker | ||
implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> MONGO_FILTERS_TEXT_METHOD = | ||
staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); | ||
|
||
/** Instantiates a new {@link MongoDBTextFilterUsage} instance. */ | ||
public MongoDBTextFilterUsage() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
return MONGO_FILTERS_TEXT_METHOD.matches(tree, state) | ||
? describeMatch(tree) | ||
: Description.NO_MATCH; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsageTest.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,26 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class MongoDBTextFilterUsageTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(MongoDBTextFilterUsage.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import com.mongodb.client.model.Filters;", | ||
"import com.mongodb.client.model.TextSearchOptions;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Filters.eq(\"foo\", \"bar\");", | ||
" // BUG: Diagnostic contains:", | ||
" Filters.text(\"foo\");", | ||
" // BUG: Diagnostic contains:", | ||
" Filters.text(\"foo\", new TextSearchOptions());", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
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