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

Qualified injected fields must not be final #27207

Merged
merged 1 commit into from
Aug 9, 2022
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 @@ -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