-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b46e6ff
commit aace5da
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
rewrite/src/main/java/dev/morphia/rewrite/recipes/LogicalFilterAdd.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,52 @@ | ||
package dev.morphia.rewrite.recipes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.NlsRewrite.Description; | ||
import org.openrewrite.NlsRewrite.DisplayName; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J.MethodInvocation; | ||
|
||
public class LogicalFilterAdd extends Recipe { | ||
private static final MethodMatcher MATCHER = new MethodMatcher("dev.morphia.query.filters.LogicalFilter add(..)"); | ||
|
||
private static final List<String> LOGICAL = List.of("and", "nor", "or"); | ||
|
||
@Override | ||
public @DisplayName String getDisplayName() { | ||
return "Collapses .add() calls to their logical filter"; | ||
} | ||
|
||
@Override | ||
public @Description String getDescription() { | ||
return "Moves the parameters of chained LogicalFilter.add() calls to their logical filter method as varargs."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<>() { | ||
@Override | ||
public MethodInvocation visitMethodInvocation(MethodInvocation method, ExecutionContext executionContext) { | ||
if (MATCHER.matches(method)) { | ||
Expression select = method; | ||
var arguments = new ArrayList<Expression>(); | ||
while (MATCHER.matches(select)) { | ||
var invocation = (MethodInvocation) select; | ||
arguments.add(0, invocation.getArguments().get(0)); | ||
select = invocation.getSelect(); | ||
} | ||
if (LOGICAL.contains(((MethodInvocation) select).getSimpleName())) { | ||
return maybeAutoFormat(method, ((MethodInvocation) select).withArguments(arguments), executionContext); | ||
} | ||
} | ||
return super.visitMethodInvocation(method, executionContext); | ||
} | ||
}; | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
rewrite/src/test/java/dev/morphia/rewrite/recipes/test/LogicalFilterAddTest.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,54 @@ | ||
package dev.morphia.rewrite.recipes.test; | ||
|
||
import dev.morphia.rewrite.recipes.LogicalFilterAdd; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.Recipe; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.openrewrite.java.Assertions.java; | ||
|
||
public class LogicalFilterAddTest extends MorphiaRewriteTest { | ||
@Override | ||
protected @NotNull Recipe getRecipe() { | ||
return new LogicalFilterAdd(); | ||
} | ||
|
||
@Test | ||
public void collapseLogicalFilter() { | ||
rewriteRun(java( | ||
//language:java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.gt; | ||
import static dev.morphia.query.filters.Filters.lt; | ||
import static dev.morphia.query.filters.Filters.or; | ||
public class LogicalFilters { | ||
public void filter(Datastore datastore) { | ||
datastore.find(Object.class) | ||
.filter(or() | ||
.add(lt("budget", 10000)) | ||
.add(gt("budget", 12))); | ||
} | ||
} | ||
""", | ||
//language:java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.gt; | ||
import static dev.morphia.query.filters.Filters.lt; | ||
import static dev.morphia.query.filters.Filters.or; | ||
public class LogicalFilters { | ||
public void filter(Datastore datastore) { | ||
datastore.find(Object.class) | ||
.filter(or(lt("budget", 10000), gt("budget", 12))); | ||
} | ||
} | ||
""")); | ||
} | ||
} |