Skip to content

Commit

Permalink
Merge pull request quarkusio#4003 from mkouba/issue-4002-autoinject-f…
Browse files Browse the repository at this point in the history
…ield-bug

Do not auto-@Inject producer fields
  • Loading branch information
geoand authored Sep 13, 2019
2 parents 50a19fa + 6210676 commit 8456e60
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class AutoFieldInjectionTest {
@Test
public void testConfigWasInjected() {
Assertions.assertEquals("ok", bean.foo);
Assertions.assertEquals(1l, bean.bar);
}

@Dependent
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ private List<BeanInfo> findBeans(Collection<DotName> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}

}

0 comments on commit 8456e60

Please sign in to comment.