Skip to content

Commit

Permalink
Merge pull request #27207 from mkouba/issue-27198
Browse files Browse the repository at this point in the history
Qualified injected fields must not be final
  • Loading branch information
gsmet committed Aug 9, 2022
2 parents 2c281f1 + 6ee79e9 commit 329f197
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,7 +70,10 @@ public int getPriority() {
@Override
public void transform(TransformationContext ctx) {
Collection<AnnotationInstance> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,18 +36,32 @@ 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;

@MyQualifier
Long bar;

// @Inject should not be added here
@MyQualifier
final Long baz;

Client() {
this.baz = null;
}

}

static class Producer {
Expand Down

0 comments on commit 329f197

Please sign in to comment.