From 0104d8270306a75d1e5247ebca34115e26babed7 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 9 Aug 2022 16:30:27 +0200 Subject: [PATCH] Qualified injected fields must not be final - just get rid of an annoying warning - resolves #27198 --- .../arc/deployment/AutoInjectFieldProcessor.java | 6 +++++- .../test/autoinject/AutoFieldInjectionTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AutoInjectFieldProcessor.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AutoInjectFieldProcessor.java index 861dd0206506f6..2da0e5b6d9c28a 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AutoInjectFieldProcessor.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AutoInjectFieldProcessor.java @@ -11,6 +11,7 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.DotName; +import org.jboss.jandex.FieldInfo; import org.jboss.logging.Logger; import io.quarkus.arc.processor.AnnotationsTransformer; @@ -69,7 +70,10 @@ public int getPriority() { @Override public void transform(TransformationContext ctx) { Collection fieldAnnotations = ctx.getAnnotations(); - if (Modifier.isStatic(ctx.getTarget().asField().flags()) || contains(fieldAnnotations, DotNames.INJECT) + FieldInfo field = ctx.getTarget().asField(); + if (Modifier.isStatic(field.flags()) + || Modifier.isFinal(field.flags()) + || contains(fieldAnnotations, DotNames.INJECT) || contains(fieldAnnotations, DotNames.PRODUCES)) { return; } diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/autoinject/AutoFieldInjectionTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/autoinject/AutoFieldInjectionTest.java index 69333fcd13ecb8..96b10acffdbbd1 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/autoinject/AutoFieldInjectionTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/autoinject/AutoFieldInjectionTest.java @@ -6,6 +6,7 @@ import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; @@ -35,11 +36,17 @@ public class AutoFieldInjectionTest { public void testInjectionWorks() { assertEquals("ok", bean.foo); assertEquals(1l, bean.bar); + assertNull(Client.staticFoo); + assertNull(bean.baz); } @Dependent static class Client { + // @Inject should not be added here + @MyQualifier + static String staticFoo; + // @Inject is added automatically @MyQualifier String foo; @@ -47,6 +54,14 @@ static class Client { @MyQualifier Long bar; + // @Inject should not be added here + @MyQualifier + final Long baz; + + Client() { + this.baz = null; + } + } static class Producer {