diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java index ffaa6bca3f625..35159793b75f7 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcConfig.java @@ -48,7 +48,7 @@ public class ArcConfig { public String removeUnusedBeans; /** - * If set to true {@code @Inject} is automatically added to all non-static fields that are annotated with + * If set to true {@code @Inject} is automatically added to all non-static non-final fields that are annotated with * one of the annotations defined by {@link AutoInjectAnnotationBuildItem}. */ @ConfigItem(defaultValue = "true") 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 861dd0206506f..2da0e5b6d9c28 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 69333fcd13ecb..96b10acffdbbd 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 {