Skip to content

Commit

Permalink
add LogicalFilterAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jan 16, 2025
1 parent b46e6ff commit aace5da
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
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);
}
};
}
}
1 change: 1 addition & 0 deletions rewrite/src/main/resources/META-INF/rewrite/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ recipeList:
- dev.morphia.rewrite.recipes.MorphiaConfigMigration
- dev.morphia.rewrite.recipes.RegexPatternMerge
- dev.morphia.rewrite.recipes.UpdateExecute
- dev.morphia.rewrite.recipes.LogicalFilterAdd
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.mapping.MapperOptions
newFullyQualifiedTypeName: dev.morphia.config.MorphiaConfig
Expand Down
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)));
}
}
"""));
}
}

0 comments on commit aace5da

Please sign in to comment.