From 70407f59a747f842ef255e6201f7dffc1fa86a6c Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 3 Oct 2023 03:13:51 -0700 Subject: [PATCH] Update with latest rewrite-templating --- build.gradle.kts | 16 ++++++----- .../misk/NoExplicitEmptyLabelList.java | 27 +++++-------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9b455f2..1fedb9e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,16 +17,16 @@ dependencies { implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") annotationProcessor("org.openrewrite:rewrite-templating:latest.integration") + implementation("org.openrewrite:rewrite-templating:latest.integration") compileOnly("com.google.errorprone:error_prone_core:2.19.1:with-dependencies") { exclude("com.google.auto.service", "auto-service-annotations") } - // TODO compileOnly? - implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.0") - implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.0") - implementation("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143") - implementation("io.micrometer:micrometer-core:latest.release") - implementation("io.prometheus:simpleclient:latest.release") + compileOnly("org.jetbrains.kotlin:kotlin-stdlib-common:1.9.0") + compileOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.0") + compileOnly("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143") + compileOnly("io.micrometer:micrometer-core:latest.release") + compileOnly("io.prometheus:simpleclient:latest.release") testImplementation("org.openrewrite:rewrite-java-17") testImplementation("org.openrewrite:rewrite-test") @@ -39,4 +39,8 @@ dependencies { testImplementation("io.micrometer:micrometer-registry-prometheus:latest.release") testImplementation("com.google.guava:guava:latest.release") + + testRuntimeOnly("org.jetbrains.kotlin:kotlin-stlib-common:1.9.0") + testRuntimeOnly("org.jetbrains.kotlin:kotlin-reflect:1.9.0") + testRuntimeOnly("com.squareup.misk:misk-metrics:2023.09.27.194750-c3aa143") } diff --git a/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java b/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java index 18922a8..d66ec77 100644 --- a/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java +++ b/src/main/java/org/openrewrite/micrometer/misk/NoExplicitEmptyLabelList.java @@ -1,6 +1,5 @@ package org.openrewrite.micrometer.misk; -import io.prometheus.client.SimpleCollector; import misk.metrics.v2.Metrics; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; @@ -13,13 +12,9 @@ import java.util.Arrays; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import static java.util.stream.Collectors.toList; import static kotlin.collections.CollectionsKt.listOf; -import static org.openrewrite.java.JavaTemplate.compile; +import static org.openrewrite.java.template.Semantics.expression; public class NoExplicitEmptyLabelList extends Recipe { @@ -36,26 +31,18 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return new JavaIsoVisitor<>() { final List hasEmptyLabel = Arrays.asList( - compile(this, "emptyLabelCounter", (JavaTemplate.F3, Metrics, String, String>) - (m, s, s1) -> m.counter(s, s1, listOf())).build(), - compile(this, "emptyLabelGauge", (JavaTemplate.F3, Metrics, String, String>) - (m, s, s1) -> m.gauge(s, s1, listOf())).build(), - compile(this, "emptyLabelPeakGauge", (JavaTemplate.F3, Metrics, String, String>) - (m, s, s1) -> m.peakGauge(s, s1, listOf())).build() + expression(this, "emptyLabelCounter", + (Metrics m, String s, String s1) -> m.counter(s, s1, listOf())).build(), + expression(this, "emptyLabelGauge", + (Metrics m, String s, String s1) -> m.gauge(s, s1, listOf())).build(), + expression(this, "emptyLabelPeakGauge", + (Metrics m, String s, String s1) -> m.peakGauge(s, s1, listOf())).build() ); @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - AtomicInteger i = new AtomicInteger(); - Stream., Metrics, String, String>>of( - (m, s, s1) -> m.counter(s, s1, listOf()), - (m, s, s1) -> m.gauge(s, s1, listOf()), - (m, s, s1) -> m.peakGauge(s, s1, listOf()) - ).map(f -> compile(this, "emptyLabel" + i.incrementAndGet(), f).build()).collect(toList()); - if (method.getSelect() != null && TypeUtils.isOfClassType(method.getSelect().getType(), "misk.metrics.v2.Metrics") && hasEmptyLabel.stream().anyMatch(tmpl -> tmpl.matches(getCursor()))) {