-
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
33b1ca0
commit 35374b4
Showing
4 changed files
with
229 additions
and
54 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
86 changes: 86 additions & 0 deletions
86
rewrite/src/main/java/dev/morphia/rewrite/recipes/RegexPatternMerge.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,86 @@ | ||
package dev.morphia.rewrite.recipes; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
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 RegexPatternMerge extends Recipe { | ||
private static final MethodMatcher MATCHER = new MethodMatcher("dev.morphia.query.filters.RegexFilter *(..)"); | ||
|
||
@NotNull | ||
@Override | ||
public @DisplayName String getDisplayName() { | ||
return "Merge regex filter expression and pattern calls"; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public @Description String getDescription() { | ||
return "This merges the fluent-style API calls in to a single method call."; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<>() { | ||
@NotNull | ||
@Override | ||
public MethodInvocation visitMethodInvocation(@NotNull MethodInvocation invocation, | ||
@NotNull ExecutionContext executionContext) { | ||
if (MATCHER.matches(invocation)) { | ||
MethodInvocation pattern = findPattern(invocation); | ||
if (pattern != null) { | ||
var arguments = pattern.getArguments(); | ||
Expression select = invocation; | ||
var methods = new ArrayList<MethodInvocation>(); | ||
while (select instanceof MethodInvocation parentInvocation) { | ||
if (parentInvocation.getSimpleName().equals("regex")) { | ||
arguments.addAll(0, parentInvocation.getArguments()); | ||
methods.add(0, parentInvocation); | ||
} else if (!parentInvocation.getSimpleName().equals("pattern")) { | ||
methods.add(0, parentInvocation); | ||
} | ||
select = parentInvocation.getSelect(); | ||
} | ||
if (methods.size() == 1) { | ||
return maybeAutoFormat(invocation, methods.get(0).withArguments(arguments), executionContext); | ||
} | ||
MethodInvocation newMethod = methods.get(0) | ||
.withArguments(arguments); | ||
for (MethodInvocation method : methods.subList(1, methods.size())) { | ||
newMethod = method.withSelect(newMethod); | ||
} | ||
return maybeAutoFormat(invocation, newMethod, executionContext); | ||
} | ||
} | ||
|
||
return super.visitMethodInvocation(invocation, executionContext); | ||
} | ||
}; | ||
} | ||
|
||
@Nullable | ||
private MethodInvocation findPattern(Expression expression) { | ||
if (expression == null) { | ||
return null; | ||
} | ||
if (expression instanceof MethodInvocation invocation) { | ||
if (invocation.getSimpleName().equals("pattern")) { | ||
return invocation; | ||
} else { | ||
return findPattern(invocation.getSelect()); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
rewrite/src/test/java/dev/morphia/rewrite/recipes/test/RegexPatternMergeTest.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,135 @@ | ||
package dev.morphia.rewrite.recipes.test; | ||
|
||
import dev.morphia.rewrite.recipes.RegexPatternMerge; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.Recipe; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class RegexPatternMergeTest extends MorphiaRewriteTest { | ||
|
||
@NotNull | ||
@Override | ||
protected Recipe getRecipe() { | ||
return new RegexPatternMerge(); | ||
} | ||
|
||
@Test | ||
public void testPatternFirstCall() { | ||
rewriteRun(java( | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name") | ||
.pattern(quote("'> FISH BONES") + "$") | ||
.multiline() | ||
.options("options") | ||
); | ||
} | ||
} | ||
""", | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name", quote("'> FISH BONES") + "$") | ||
.multiline() | ||
.options("options") | ||
); | ||
} | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
public void testPatternMerge() { | ||
rewriteRun(java( | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name") | ||
.multiline() | ||
.pattern(quote("'> FISH BONES") + "$") | ||
.options("options") | ||
); | ||
} | ||
} | ||
""", | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name", quote("'> FISH BONES") + "$") | ||
.multiline() | ||
.options("options") | ||
); | ||
} | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
public void testOnlyPatternCalled() { | ||
rewriteRun(java( | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name") | ||
.pattern(quote("'> FISH BONES") + "$") | ||
); | ||
} | ||
} | ||
""", | ||
//language=java | ||
""" | ||
import dev.morphia.Datastore; | ||
import static dev.morphia.query.filters.Filters.regex; | ||
import static java.util.regex.Pattern.quote; | ||
public class Regexes { | ||
public void find(Datastore ds) { | ||
ds.find(Object.class) | ||
.filter(regex("name", quote("'> FISH BONES") + "$") | ||
); | ||
} | ||
} | ||
""")); | ||
} | ||
|
||
} |