From b9e37c6d0864d51ce7b24fcdc368d2dc0faa2ac9 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Mon, 11 Nov 2024 20:55:51 -0500 Subject: [PATCH] stage 2 works. need to fix classpath for stage 1 test. --- .../recipes/PipelineRewriteStage1.java | 2 +- .../recipes/PipelineRewriteStage2.java | 125 +++-------------- .../recipes/test/Morphia2RewriteTest.java | 10 +- .../recipes/test/MorphiaRewriteTest.java | 17 +-- .../test/PipelineRewriteStage1Test.java | 6 - .../test/PipelineRewriteStage2Test.java | 127 +++++++++--------- .../rewrite/recipes/test/UnwrapFieldTest.java | 17 +-- 7 files changed, 100 insertions(+), 204 deletions(-) diff --git a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java index 56402da7548..f57977eae21 100644 --- a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java +++ b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage1.java @@ -69,7 +69,7 @@ public MethodInvocation visitMethodInvocation(MethodInvocation methodInvocation, if (matchers.stream().anyMatch(matcher -> matcher.matches(methodInvocation))) { return super.visitMethodInvocation(methodInvocation .withName(methodInvocation.getName().withSimpleName("pipeline")), - context); + context); } else { return super.visitMethodInvocation(methodInvocation, context); } diff --git a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage2.java b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage2.java index e0511bf098a..b793ba4108b 100644 --- a/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage2.java +++ b/rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewriteStage2.java @@ -1,40 +1,21 @@ package dev.morphia.rewrite.recipes; +import java.util.ArrayList; + import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.Expression; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.J.Block; -import org.openrewrite.java.tree.J.Identifier; import org.openrewrite.java.tree.J.MethodInvocation; -import org.openrewrite.java.tree.JContainer; -import org.openrewrite.java.tree.JRightPadded; -import org.openrewrite.java.tree.Space; -import org.openrewrite.java.tree.Statement; -import org.openrewrite.marker.Markers; - -import java.lang.annotation.ElementType; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; - -import static java.util.List.of; -import static org.openrewrite.java.tree.JRightPadded.build; public class PipelineRewriteStage2 extends Recipe { - static final String AGGREGATION = "dev.morphia.aggregation.Aggregation"; - - static final MethodMatcher PIPELINE = new MethodMatcher(PipelineRewriteStage2.AGGREGATION + " pipeline(..)"); + private static final String AGGREGATION = "dev.morphia.aggregation.Aggregation"; - private final JavaTemplate pipelineTemplate = null; //JavaTemplate.builder("pipeline(..)").contextSensitive().build(); + private static final MethodMatcher PIPELINE = new MethodMatcher(PipelineRewriteStage2.AGGREGATION + " pipeline(..)"); @Override public String getDisplayName() { @@ -51,100 +32,24 @@ public TreeVisitor getVisitor() { return Preconditions.check(new UsesMethod<>(PIPELINE), new JavaIsoVisitor<>() { public MethodInvocation visitMethodInvocation(MethodInvocation method, ExecutionContext p) { - // exit if method doesn't match isEqualTo(..) - if (!PIPELINE.matches(method.getSelect()/*.getMethodType()*/)) { + if (!PIPELINE.matches(method.getSelect())) { return method; } - var arguments = new ArrayList>(); - var select = method.getSelect(); - while (PIPELINE.matches(select)) { -// System.out.println("select = " + select); - J.MethodInvocation invocation = (J.MethodInvocation) select; - arguments.add(build(invocation.getArguments().get(0))); - select = invocation.getSelect(); - } - System.out.println("done: select = " + select); - Collections.reverse(arguments); - Markers markers = new Markers(UUID.randomUUID(), of()); - Identifier identifier = (Identifier) select; - Space prefix = method.getPrefix(); - var newInvocation = new MethodInvocation( - UUID.randomUUID(), prefix, markers, build(method.getSelect()), null, identifier, - JContainer.build(arguments), - method.getMethodType()); - return newInvocation; - } -/* - @Override - public J.Block visitBlock(J.Block block, ExecutionContext ctx) { - J.Block bl = super.visitBlock(block, ctx); - return bl.withStatements(rewritePipelineStatements(bl)); - } -*/ - -/* - private MethodInvocation rewritePipelineStatements(Block bl) { - List statements = new ArrayList<>(); - for (var statement : bl.getStatements()) { - if (statement instanceof J.MethodInvocation && isPipeline(statement)) { - List arguments = new ArrayList<>(); - J.MethodInvocation pipeline = (J.MethodInvocation) statement; - var select = pipeline.getSelect(); - while (PIPELINE.matches(select)) { - System.out.println("select = " + select); - J.MethodInvocation invocation = (J.MethodInvocation) select; - arguments.add(invocation.getArguments().get(0)); - select = invocation.getSelect(); - } - System.out.println("done: select = " + select); - Collections.reverse(arguments); -// Markers markers = new Markers(UUID.randomUUID(), of()); -// Identifier identifier = (Identifier) select; -// Space prefix = statement.getPrefix(); -// var newInvocation = new MethodInvocation( -// UUID.randomUUID(), prefix, markers, null, null, identifier, arguments, -// ((MethodInvocation) statement).getMethodType() ); - MethodInvocation m = pipelineTemplate.apply(getCursor(), statement.getCoordinates().replace()); - return m; - statements.add((newInvocation)); - } else { - statements.add(statement); - } - } - return statements; - } -*/ - - private boolean isPipeline(Statement statement) { - J.MethodInvocation methodInvocation = (J.MethodInvocation) statement; - // Only match method invocations where the select is an assertThat, containing a non-method call argument - if (PIPELINE.matches(methodInvocation.getSelect())) { - J.MethodInvocation invocation = (J.MethodInvocation) methodInvocation.getSelect(); - if (invocation != null && PIPELINE.matches(invocation.getSelect())) { - return true; + var updated = method; + while (PIPELINE.matches(updated.getSelect()) && PIPELINE.matches(((MethodInvocation) updated.getSelect()).getSelect())) { + var select = updated.getSelect(); + MethodInvocation invocation = (MethodInvocation) select; + if (PIPELINE.matches(invocation.getSelect())) { + MethodInvocation parent = (MethodInvocation) invocation.getSelect(); + var args = new ArrayList<>(parent.getArguments()); + args.addAll(invocation.getArguments()); + updated = updated.withSelect(((MethodInvocation) invocation.getSelect()).withArguments(args)); } } - return false; + return updated; } - private J.MethodInvocation getCollapsedAssertThat(List consecutiveAssertThatStatement) { - assert !consecutiveAssertThatStatement.isEmpty(); - Space originalPrefix = consecutiveAssertThatStatement.get(0).getPrefix(); - String continuationIndent = originalPrefix.getIndent().contains("\t") ? "\t\t" : " "; - Space indentedNewline = Space.format(originalPrefix.getLastWhitespace().replaceAll("^\\s+\n", "\n") + - continuationIndent); - J.MethodInvocation collapsed = null; - for (Statement st : consecutiveAssertThatStatement) { - J.MethodInvocation assertion = (J.MethodInvocation) st; - J.MethodInvocation assertThat = (J.MethodInvocation) assertion.getSelect(); - assert assertThat != null; - J.MethodInvocation newSelect = collapsed == null ? assertThat : collapsed; - collapsed = assertion.getPadding().withSelect(build((Expression) newSelect.withPrefix(Space.EMPTY)) - .withAfter(indentedNewline)); - } - return collapsed.withPrefix(originalPrefix); - } }); } } diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/Morphia2RewriteTest.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/Morphia2RewriteTest.java index 29f47b3835f..5935c34f402 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/Morphia2RewriteTest.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/Morphia2RewriteTest.java @@ -6,11 +6,11 @@ public abstract class Morphia2RewriteTest extends MorphiaRewriteTest { @Override protected @NotNull String findMorphiaCore() { var core = runtimeClasspath.stream() - .filter(uri -> { - String string = uri.toString(); - return string.contains("morphia") && string.contains("core"); - }) - .findFirst().orElseThrow().toString(); + .filter(uri -> { + String string = uri.toString(); + return string.contains("morphia") && string.contains("core"); + }) + .findFirst().orElseThrow().toString(); final String artifact = core.contains("morphia-core") ? "morphia-core" : "morphia/core"; return artifact; diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/MorphiaRewriteTest.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/MorphiaRewriteTest.java index 82c8d83fb5f..9038ebe4fec 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/MorphiaRewriteTest.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/MorphiaRewriteTest.java @@ -1,16 +1,17 @@ package dev.morphia.rewrite.recipes.test; -import io.github.classgraph.ClassGraph; +import java.io.File; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + import org.jetbrains.annotations.NotNull; import org.openrewrite.Recipe; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; -import java.io.File; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; +import io.github.classgraph.ClassGraph; public abstract class MorphiaRewriteTest implements RewriteTest { protected List runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs(); @@ -32,9 +33,9 @@ public void defaults(RecipeSpec spec) { @NotNull protected List findMongoArtifacts() { List classpath = runtimeClasspath.stream() - .filter(uri -> uri.toString().contains("mongodb") || uri.toString().contains("bson")) - .map(uri -> new File(uri).getAbsolutePath()/*.getName().replaceAll("-[0-9].*", "")*/) - .collect(ArrayList::new, List::add, List::addAll); + .filter(uri -> uri.toString().contains("mongodb") || uri.toString().contains("bson")) + .map(uri -> new File(uri).getAbsolutePath()/* .getName().replaceAll("-[0-9].*", "") */) + .collect(ArrayList::new, List::add, List::addAll); return classpath; } diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java index 72bffe9fc2c..8dfeb8df303 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage1Test.java @@ -2,16 +2,10 @@ import dev.morphia.rewrite.recipes.PipelineRewriteStage1; -import io.github.classgraph.ClassGraph; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import java.io.File; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; - import static org.openrewrite.java.Assertions.java; public class PipelineRewriteStage1Test extends Morphia2RewriteTest { 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 index 154ac47e45a..b59c3f79697 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage2Test.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/PipelineRewriteStage2Test.java @@ -1,6 +1,10 @@ package dev.morphia.rewrite.recipes.test; +import java.io.File; +import java.nio.file.Path; + import dev.morphia.rewrite.recipes.PipelineRewriteStage2; + import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; @@ -8,10 +12,6 @@ import org.openrewrite.java.JavaParser.Builder; import org.openrewrite.test.RecipeSpec; -import java.io.File; -import java.nio.file.Path; -import java.util.List; - import static org.openrewrite.java.Assertions.java; public class PipelineRewriteStage2Test extends MorphiaRewriteTest { @@ -30,65 +30,61 @@ public class PipelineRewriteStage2Test extends MorphiaRewriteTest { @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); - var dummy = 42; - } - } - """, - """ - 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); - } - } - """)); - } @Override + //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); + } + } + """)); + } + + @Override protected @NotNull String findMorphiaCore() { - return classesFolder; + return classesFolder; } public String[] classpath() { @@ -98,11 +94,11 @@ public String[] classpath() { @Override public void defaults(RecipeSpec spec) { Builder builder = JavaParser.fromJavaVersion() - .addClasspathEntry(Path.of(classesFolder)); + .addClasspathEntry(Path.of(classesFolder)); findMongoArtifacts().stream().map(Path::of) - .forEach(builder::addClasspathEntry); + .forEach(builder::addClasspathEntry); spec.recipe(getRecipe()) - .parser(builder); + .parser(builder); } @Override @@ -111,5 +107,4 @@ protected Recipe getRecipe() { return new PipelineRewriteStage2(); } - } diff --git a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/UnwrapFieldTest.java b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/UnwrapFieldTest.java index f78a3303f0c..06dd1d19a05 100644 --- a/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/UnwrapFieldTest.java +++ b/rewrite/src/test/java/dev/morphia/rewrite/recipes/test/UnwrapFieldTest.java @@ -1,16 +1,17 @@ package dev.morphia.rewrite.recipes.test; +import java.io.File; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + import dev.morphia.rewrite.recipes.UnwrapFieldExpressions; -import io.github.classgraph.ClassGraph; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.openrewrite.Recipe; -import java.io.File; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; +import io.github.classgraph.ClassGraph; import static org.openrewrite.java.Assertions.java; @@ -20,14 +21,14 @@ public class UnwrapFieldTest extends Morphia2RewriteTest { public String[] classpath() { List runtimeClasspath = new ClassGraph().disableNestedJarScanning().getClasspathURIs(); List classpath = runtimeClasspath.stream() - .filter(uri -> { + .filter(uri -> { String string = uri.toString(); return string.contains("mongodb") || string.contains("bson"); }) - .map(uri -> { + .map(uri -> { return new File(uri).getName().replaceAll("-[0-9].*", ""); }) - .collect(ArrayList::new, List::add, List::addAll); + .collect(ArrayList::new, List::add, List::addAll); var core = runtimeClasspath.stream() .filter(uri -> { String string = uri.toString();