Repository: quarkusio/quarkus
Issue: NPE When Constructor Injecting a Microprofile Metric

### Describe the bug

Quarkus throws an NPE when a bean constructor injects a microprofile metric e.g.

```java
@ApplicationScoped
public class FailsToInjectMetric {

  private final Timer timer;

  @Inject
  public FailsToInjectMetric(
      @Metric(name = "my-timer", unit = MetricUnits.MILLISECONDS) final Timer timer) {
    this.timer = timer;
  }
```

```
java.lang.NullPointerException: Cannot invoke "org.eclipse.microprofile.metrics.annotation.Metric.name()" because "annotation" is null

	at io.quarkus.micrometer.runtime.binder.mpmetrics.MetricRegistryAdapter.injectedTimer(MetricRegistryAdapter.java:365)
	at io.quarkus.micrometer.runtime.binder.mpmetrics.InjectedMetricProducer.getTimer(InjectedMetricProducer.java:72)
	at io.quarkus.micrometer.runtime.binder.mpmetrics.InjectedMetricProducer_ProducerMethod_getTimer_40638bc6dcfae855a99f42cfc454029922b9370a_Bean.doCreate(Unknown Source)
	at io.quarkus.micrometer.runtime.binder.mpmetrics.InjectedMetricProducer_ProducerMethod_getTimer_40638bc6dcfae855a99f42cfc454029922b9370a_Bean.create(Unknown Source)
	at io.quarkus.micrometer.runtime.binder.mpmetrics.InjectedMetricProducer_ProducerMethod_getTimer_40638bc6dcfae855a99f42cfc454029922b9370a_Bean.get(Unknown Source)
	at io.quarkus.micrometer.runtime.binder.mpmetrics.InjectedMetricProducer_ProducerMethod_getTimer_40638bc6dcfae855a99f42cfc454029922b9370a_Bean.get(Unknown Source)
	at io.quarkus.arc.impl.CurrentInjectionPointProvider.get(CurrentInjectionPointProvider.java:48)
	at com.acme.FailsToInjectMetric_Bean.doCreate(Unknown Source)
```

Changing the bean to use package-private field injection instead of constructor injection works around this issue.

### Expected behavior

Quarkus constructor injection works with microprofile metrics.

### Actual behavior

Quarkus throws NPE.

### How to Reproduce?

Run the tests in the attached Maven project.

[constructor-inject-microprofile-metric-issue.zip](https://github.com/quarkusio/quarkus/files/11557001/constructor-inject-microprofile-metric-issue.zip)


### Output of `uname -a` or `ver`

Darwin pixee-mbp-gilday.localdomain 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64

### Output of `java -version`

openjdk version "17.0.7" 2023-04-18 OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7) OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode)

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

3.0.3.Final

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39) Maven home: /Users/jgilday/.m2/wrapper/dists/apache-maven-3.8.8-bin/67c30f74/apache-maven-3.8.8 Java version: 17.0.7, vendor: Eclipse Adoptium, runtime: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "13.3.1", arch: "aarch64", family: "mac"

### Additional information

_No response_

Changed files:
extensions/micrometer/deployment/src/main/java/io/quarkus/micrometer/deployment/binder/mpmetrics/AnnotationHandler.java

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 1645ffbe59e..50d29838e39 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<AnnotationInstance> 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();
             }
         });