From 7807cea5e939f7f167a6f02e1c4641b24f4d358c Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 3 Oct 2023 04:07:33 -0700 Subject: [PATCH] First form of MigrateEmptyLabelMiskCounter passes test --- .../misk/MigrateEmptyLabelMiskCounter.java | 20 +++++++++++++------ .../MigrateEmptyLabelMiskCounterTest.java | 3 +-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java b/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java index c216193..dbcbb07 100644 --- a/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java +++ b/src/main/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounter.java @@ -17,16 +17,20 @@ import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Metrics; -import org.openrewrite.*; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.UsesType; -import org.openrewrite.java.template.Semantics; import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.TypeUtils; import java.util.List; -import static kotlin.collections.CollectionsKt.listOf; import static org.openrewrite.java.template.Semantics.expression; public class MigrateEmptyLabelMiskCounter extends Recipe { @@ -50,15 +54,18 @@ public List getRecipeList() { public TreeVisitor getVisitor() { return Preconditions.check(new UsesType<>("misk.metrics.v2.Metrics", true), new JavaIsoVisitor<>() { final MethodMatcher miskCounter = new MethodMatcher("misk.metrics.v2.Metrics counter(..)"); + final MethodMatcher listOf = new MethodMatcher("kotlin.collections.CollectionsKt listOf()", true); @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = super.visitMethodInvocation(method, ctx); if (miskCounter.matches(method)) { boolean emptyLabel = method.getArguments().size() == 2; - if (method.getArguments().size() == 3) { - emptyLabel = expression(this, "listOf", () -> listOf()).build() - .matches(new Cursor(getCursor(), method.getArguments().get(2))); + if (method.getArguments().size() == 3 && method.getArguments().get(2) instanceof J.MethodInvocation) { + JavaType.Method arg2 = ((J.MethodInvocation) method.getArguments().get(2)).getMethodType(); + emptyLabel = arg2 != null && + TypeUtils.isOfClassType(arg2.getDeclaringType(), "kotlin.collections.CollectionsKt") && + arg2.getName().equals("listOf"); } if (!emptyLabel) { return m; @@ -72,6 +79,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu method.getArguments().get(1)); maybeRemoveImport("misk.metrics.v2.Metrics"); + maybeAddImport("io.micrometer.core.instrument.Counter"); } return m; } diff --git a/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java b/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java index 8ad0286..8b8e17f 100644 --- a/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java +++ b/src/test/java/org/openrewrite/micrometer/misk/MigrateEmptyLabelMiskCounterTest.java @@ -32,7 +32,6 @@ public void defaults(RecipeSpec spec) { .classpath("misk-metrics", "kotlin-reflect", "kotlin-stdlib")); } - @Disabled @Test void migrateEmptyLabel() { //language=java @@ -50,7 +49,7 @@ void test(Metrics metrics) { """, """ import io.micrometer.core.instrument.Counter; - + import misk.metrics.v2.Metrics; import static kotlin.collections.CollectionsKt.listOf; class Test {