From 62106760318d7ef617dd5d625d39b208fa2b3cca Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Fri, 13 Sep 2019 10:16:40 +0200 Subject: [PATCH] Do not auto-@Inject producer fields - resolves #4002 - also if a producer field is annotated with @Inject, the container detects the problem --- .../deployment/AutoInjectFieldProcessor.java | 3 +- .../autoinject/AutoFieldInjectionTest.java | 9 +++++ .../quarkus/arc/processor/BeanDeployment.java | 3 ++ .../illegal/ProducerFieldWithInjectTest.java | 36 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/producer/illegal/ProducerFieldWithInjectTest.java 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 4c9c68c27083b..2de1dfe1925c4 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 @@ -58,7 +58,8 @@ public boolean appliesTo(AnnotationTarget.Kind kind) { @Override public void transform(TransformationContext transformationContext) { FieldInfo field = transformationContext.getTarget().asField(); - if (Modifier.isStatic(field.flags()) || field.hasAnnotation(DotNames.INJECT)) { + if (Modifier.isStatic(field.flags()) || field.hasAnnotation(DotNames.INJECT) + || field.hasAnnotation(DotNames.PRODUCES)) { return; } for (DotName annotation : annotations) { 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 879297f74aa3c..caaa359c98de4 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 @@ -36,6 +36,7 @@ public class AutoFieldInjectionTest { @Test public void testConfigWasInjected() { Assertions.assertEquals("ok", bean.foo); + Assertions.assertEquals(1l, bean.bar); } @Dependent @@ -45,10 +46,18 @@ static class Client { @MyQualifier String foo; + @MyQualifier + Long bar; + } static class Producer { + // @Inject should not be added here + @MyQualifier + @Produces + Long producedLong = 1l; + @MyQualifier @Produces String produceString() { diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java index 8d8e54de726ea..0bec4732f0171 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java @@ -662,6 +662,9 @@ private List findBeans(Collection beanDefiningAnnotations, Li } for (FieldInfo field : beanClass.fields()) { if (annotationStore.hasAnnotation(field, DotNames.PRODUCES)) { + if (annotationStore.hasAnnotation(field, DotNames.INJECT)) { + throw new DefinitionException("Injected field cannot be annotated with @Produces: " + field); + } // Producer fields are not inherited producerFields.add(field); if (!hasBeanDefiningAnnotation) { diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/producer/illegal/ProducerFieldWithInjectTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/producer/illegal/ProducerFieldWithInjectTest.java new file mode 100644 index 0000000000000..99545aa5b444a --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/producer/illegal/ProducerFieldWithInjectTest.java @@ -0,0 +1,36 @@ +package io.quarkus.arc.test.producer.illegal; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import io.quarkus.arc.test.ArcTestContainer; +import java.time.temporal.Temporal; +import javax.enterprise.context.Dependent; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.DefinitionException; +import javax.inject.Inject; +import org.junit.Rule; +import org.junit.Test; + +public class ProducerFieldWithInjectTest { + + @Rule + public ArcTestContainer container = ArcTestContainer.builder().beanClasses(IllegalProducer.class).shouldFail().build(); + + @Test + public void testFailure() { + Throwable error = container.getFailure(); + assertNotNull(error); + assertTrue(error instanceof DefinitionException); + } + + @Dependent + static class IllegalProducer { + + @Inject + @Produces + Temporal temporal; + + } + +}