From 5ef3b089b13d432210461c1c491799accbf829c7 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Thu, 25 May 2023 11:02:44 +0200 Subject: [PATCH] Fix the Micrometer MP Metrics annotation transformation The annotation transformation replaces an existing annotation with a new one without paying attention to the annotation's target. That's usually OK, because the `TransformationContext` typically contains the correct target, but that isn't true for method parameter annotations. For legacy reasons, the annotation transformation API treats them as annotations on methods, so we need to pay extra attention to preserve the annotation target. This commit does that. --- .../binder/mpmetrics/AnnotationHandler.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/binder/mpmetrics/AnnotationHandler.java b/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/binder/mpmetrics/AnnotationHandler.java index 1645ffbe59ef5..50d29838e394d 100644 --- a/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/binder/mpmetrics/AnnotationHandler.java +++ b/extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/binder/mpmetrics/AnnotationHandler.java @@ -29,12 +29,12 @@ static AnnotationsTransformerBuildItem transformAnnotations(final IndexView inde } static AnnotationsTransformerBuildItem transformAnnotations(final IndexView index, - DotName sourceAnnotation, DotName targetAnnotation) { + DotName sourceAnnotationName, DotName targetAnnotationName) { return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() { @Override public void transform(TransformationContext ctx) { final Collection annotations = ctx.getAnnotations(); - AnnotationInstance annotation = Annotations.find(annotations, sourceAnnotation); + AnnotationInstance annotation = Annotations.find(annotations, sourceAnnotationName); if (annotation == null) { return; } @@ -59,8 +59,8 @@ public void transform(TransformationContext ctx) { // Remove the @Counted annotation when both @Counted & @Timed/SimplyTimed // Ignore @Metric with @Produces - if (removeCountedWhenTimed(sourceAnnotation, target, classInfo, methodInfo) || - removeMetricWhenProduces(sourceAnnotation, target, methodInfo, fieldInfo)) { + if (removeCountedWhenTimed(sourceAnnotationName, target, classInfo, methodInfo) || + removeMetricWhenProduces(sourceAnnotationName, target, methodInfo, fieldInfo)) { ctx.transform() .remove(x -> x == annotation) .done(); @@ -71,10 +71,14 @@ public void transform(TransformationContext ctx) { MetricAnnotationInfo annotationInfo = new MetricAnnotationInfo(annotation, index, classInfo, methodInfo, fieldInfo); + // preserve the original annotation target, `ctx.getTarget()` is different in case of method parameters + AnnotationInstance newAnnotation = AnnotationInstance.create(targetAnnotationName, annotation.target(), + annotationInfo.getAnnotationValues()); + // Remove the existing annotation, and add a new one with all the fields ctx.transform() .remove(x -> x == annotation) - .add(targetAnnotation, annotationInfo.getAnnotationValues()) + .add(newAnnotation) .done(); } });