diff --git a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewrite.java b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java similarity index 69% rename from rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewrite.java rename to rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java index b4ba7a50a42..36e33c09492 100644 --- a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewrite.java +++ b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java @@ -1,26 +1,19 @@ package dev.morphia.rewrite.recipes; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.StringJoiner; import org.jetbrains.annotations.NotNull; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.JavaParser; -import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.J.MethodInvocation; -public class PipelineRewrite extends Recipe { +public class PipelineRewriteStage1 extends Recipe { static final String AGGREGATION = "dev.morphia.aggregation.Aggregation"; - static final MethodMatcher pipeline = new MethodMatcher(PipelineRewrite.AGGREGATION + " pipeline(..)"); + static final MethodMatcher pipeline = new MethodMatcher(PipelineRewriteStage1.AGGREGATION + " pipeline(..)"); static final List matchers = List.of( new MethodMatcher(AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)"), new MethodMatcher(AGGREGATION + " autoBucket(dev.morphia.aggregation.stages.AutoBucket)"), @@ -74,7 +67,6 @@ public TreeVisitor getVisitor() { @Override public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, @NotNull ExecutionContext context) { return working(methodInvocation, context); - // return notWorking(methodInvocation, context); } public MethodInvocation working(MethodInvocation methodInvocation, @NotNull ExecutionContext context) { @@ -87,40 +79,6 @@ public MethodInvocation working(MethodInvocation methodInvocation, @NotNull Exec } } - public MethodInvocation notWorking(MethodInvocation methodInvocation, @NotNull ExecutionContext context) { - if (matchers.stream().anyMatch(matcher -> matcher.matches(methodInvocation))) { - MethodInvocation mi = methodInvocation; - List arguments = new ArrayList<>(); - Expression expression = mi; - while (expression instanceof MethodInvocation invocation) { - arguments.add(invocation.getArguments().get(0)); - mi = invocation; - expression = mi.getSelect(); - } - Collections.reverse(arguments); - - return applyTemplate(expression, arguments, expression); - } else { - return super.visitMethodInvocation(methodInvocation, context); - - } - } - - private J.MethodInvocation applyTemplate(Expression expression, List arguments, Expression target) { - String code = buildTemplate(expression, arguments); - return JavaTemplate.builder(code) - .contextSensitive() - .javaParser(JavaParser.fromJavaVersion()) - .build() - .apply(getCursor(), target.getCoordinates().replace()); - } - - private String buildTemplate(Expression toReplace, List arguments) { - StringJoiner joiner = new StringJoiner(",\n\t", toReplace + ".pipeline(", ")"); - arguments.forEach(argument -> joiner.add(argument.toString())); - - return joiner.toString(); - } }; } } diff --git a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineVisitor.java b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineVisitor.java index 2c98bd10ca9..e108fbe17ee 100644 --- a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineVisitor.java +++ b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineVisitor.java @@ -10,7 +10,7 @@ class PipelineVisitor extends JavaIsoVisitor { @Override public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, @NotNull ExecutionContext context) { - if (PipelineRewrite.pipeline.matches(methodInvocation)) { + if (PipelineRewriteStage1.pipeline.matches(methodInvocation)) { Expression select = methodInvocation.getSelect(); System.out.println("\n\nselect = " + select); System.out.println("select.getSideEffects() = " + select.getSideEffects()); diff --git a/rewrite/src/main/resources/META-INF/rewrite/morphia3.yml b/rewrite/src/main/resources/META-INF/rewrite/morphia3.yml index f0eb4381f23..32ccc062d3b 100644 --- a/rewrite/src/main/resources/META-INF/rewrite/morphia3.yml +++ b/rewrite/src/main/resources/META-INF/rewrite/morphia3.yml @@ -3,12 +3,7 @@ name: dev.morphia.UpgradeToMorphia30 displayName: Upgrade to Morphia 3.0 description: Adopt new dependencies and breaking changes in moving to Morphia 3. recipeList: - - org.openrewrite.java.ChangeMethodName: - methodPattern: dev.morphia.aggregation.stages.Out to(..) - newMethodName: out - matchOverrides: "true" - ignoreDefinition: "false" - - dev.morphia.rewrite.recipes.PipelineRewrite: + - dev.morphia.rewrite.recipes.PipelineRewriteStage1: matchOverrides: "true" ignoreDefinition: "false" - org.openrewrite.java.ChangeType: diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteTest.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java similarity index 95% rename from rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteTest.java rename to rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java index b7af7cbd90f..50429201249 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteTest.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java @@ -1,6 +1,6 @@ package dev.morphia.rewrite.recipes.test; -import dev.morphia.rewrite.recipes.PipelineRewrite; +import dev.morphia.rewrite.recipes.PipelineRewriteStage1; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; @@ -8,12 +8,12 @@ import static org.openrewrite.java.Assertions.java; -public class PipelineRewriteTest extends MorphiaRewriteTest { +public class PipelineRewriteStage1Test extends MorphiaRewriteTest { @Override @NotNull protected Recipe getRecipe() { - return new PipelineRewrite(); + return new PipelineRewriteStage1(); } @Test diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage2Test.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage2Test.java new file mode 100644 index 00000000000..9554c9dee77 --- /dev/null +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage2Test.java @@ -0,0 +1,77 @@ +package dev.morphia.rewrite.recipes.test; + +import dev.morphia.rewrite.recipes.PipelineRewriteStage1; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; +import org.openrewrite.Recipe; + +import static org.openrewrite.java.Assertions.java; + +public class PipelineRewriteStage2Test extends MorphiaRewriteTest { + + @Override + @NotNull + protected Recipe getRecipe() { + return new PipelineRewriteStage1(); + } + +// @Test + void unwrapStageMethods() { + rewriteRun( + //language=java + java( + """ + import dev.morphia.aggregation.expressions.ComparisonExpressions; + + import static dev.morphia.aggregation.expressions.AccumulatorExpressions.sum; + import static dev.morphia.aggregation.stages.Group.group; + import static dev.morphia.aggregation.stages.Group.id; + import static dev.morphia.aggregation.stages.Projection.project; + import static dev.morphia.aggregation.expressions.Expressions.field; + import static dev.morphia.aggregation.expressions.Expressions.value; + import static dev.morphia.aggregation.stages.Sort.sort; + + import dev.morphia.aggregation.Aggregation; + import org.bson.Document; + + public class UnwrapTest { + public void update(Aggregation aggregation) { + aggregation + .pipeline(group(id("author")).field("count", sum(value(1)))) + .pipeline(sort().ascending("1")) + .pipeline(sort().ascending("2")) + .pipeline(sort().ascending("3")) + .pipeline(sort().ascending("4")) + .execute(Document.class); + } + } + """, + """ + import dev.morphia.aggregation.expressions.ComparisonExpressions; + + import static dev.morphia.aggregation.expressions.AccumulatorExpressions.sum; + import static dev.morphia.aggregation.stages.Group.group; + import static dev.morphia.aggregation.stages.Group.id; + import static dev.morphia.aggregation.stages.Projection.project; + import static dev.morphia.aggregation.expressions.Expressions.field; + import static dev.morphia.aggregation.expressions.Expressions.value; + import static dev.morphia.aggregation.stages.Sort.sort; + + import dev.morphia.aggregation.Aggregation; + import org.bson.Document; + + public class UnwrapTest { + public void update(Aggregation aggregation) { + aggregation + .pipeline( + group(id("author")).field("count", sum(value(1))), + sort().ascending("1"), + sort().ascending("2"), + sort().ascending("3"), + sort().ascending("4")) + .execute(Document.class); + } + } + """)); + } +}