Skip to content

Commit

Permalink
First form of MigrateEmptyLabelMiskCounter passes test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Oct 3, 2023
1 parent 83c4ce6 commit 7807cea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -50,15 +54,18 @@ public List<Recipe> getRecipeList() {
public TreeVisitor<?, ExecutionContext> 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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void defaults(RecipeSpec spec) {
.classpath("misk-metrics", "kotlin-reflect", "kotlin-stdlib"));
}

@Disabled
@Test
void migrateEmptyLabel() {
//language=java
Expand All @@ -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 {
Expand Down

0 comments on commit 7807cea

Please sign in to comment.