Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not auto-@Inject producer fields #4003

Merged
merged 1 commit into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}

}