Skip to content

Commit

Permalink
Fix the Micrometer MP Metrics annotation transformation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ladicek committed May 25, 2023
1 parent b0708bc commit 3ebd80a
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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(targetAnnotation, 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();
}
});
Expand Down

0 comments on commit 3ebd80a

Please sign in to comment.