From 4f5317ee639767cf362d0eeae39d7f760d143d78 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 7 Jul 2021 16:01:42 +0200 Subject: [PATCH] ArC - ignore static and final injection fields --- .../io/quarkus/arc/processor/Injection.java | 8 ++- .../finalfield/FinalFieldInjectionTest.java | 53 +++++++++++++++++++ .../staticfield/StaticFieldInjectionTest.java | 50 +++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/finalfield/FinalFieldInjectionTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/staticfield/StaticFieldInjectionTest.java diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java index a474c0f044dde..66de1b57e1bba 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java @@ -192,7 +192,13 @@ private static List getAllInjectionPoints(BeanDeployment bea for (FieldInfo field : beanClass.fields()) { AnnotationInstance inject = beanDeployment.getAnnotation(field, name); if (inject != null) { - injectAnnotations.add(inject); + if (Modifier.isFinal(field.flags()) || Modifier.isStatic(field.flags())) { + LOGGER.warn("An injection field must be non-static and non-final - ignoring: " + + field.declaringClass().name() + "#" + + field.name()); + } else { + injectAnnotations.add(inject); + } } } for (MethodInfo method : beanClass.methods()) { diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/finalfield/FinalFieldInjectionTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/finalfield/FinalFieldInjectionTest.java new file mode 100644 index 0000000000000..3b5243358eeed --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/finalfield/FinalFieldInjectionTest.java @@ -0,0 +1,53 @@ +package io.quarkus.arc.test.injection.finalfield; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.test.ArcTestContainer; +import java.util.concurrent.atomic.AtomicInteger; +import javax.enterprise.context.Dependent; +import javax.inject.Inject; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class FinalFieldInjectionTest { + + @RegisterExtension + public ArcTestContainer container = new ArcTestContainer(Head.class, CombineHarvester.class); + + @Test + public void testInjection() { + assertNotNull(Arc.container().instance(CombineHarvester.class).get().getHead()); + assertEquals(1, Head.COUNTER.get()); + } + + @Dependent + static class Head { + + static final AtomicInteger COUNTER = new AtomicInteger(0); + + public Head() { + COUNTER.incrementAndGet(); + } + + } + + @Dependent + static class CombineHarvester { + + // This one is ignored + @Inject + final Head head; + + // The parameter is injected + CombineHarvester(Head head) { + this.head = head; + } + + public Head getHead() { + return head; + } + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/staticfield/StaticFieldInjectionTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/staticfield/StaticFieldInjectionTest.java new file mode 100644 index 0000000000000..6f8894cda55e9 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injection/staticfield/StaticFieldInjectionTest.java @@ -0,0 +1,50 @@ +package io.quarkus.arc.test.injection.staticfield; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.test.ArcTestContainer; +import java.util.concurrent.atomic.AtomicInteger; +import javax.enterprise.context.Dependent; +import javax.inject.Inject; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class StaticFieldInjectionTest { + + @RegisterExtension + public ArcTestContainer container = new ArcTestContainer(Head.class, CombineHarvester.class); + + @Test + public void testInjection() { + assertNotNull(Arc.container().instance(CombineHarvester.class).get()); + assertNotNull(CombineHarvester.head); + assertEquals(1, Head.COUNTER.get()); + } + + @Dependent + static class Head { + + static final AtomicInteger COUNTER = new AtomicInteger(0); + + public Head() { + COUNTER.incrementAndGet(); + } + + } + + @Dependent + static class CombineHarvester { + + // This one is ignored + @Inject + static Head head; + + // The parameter is injected + CombineHarvester(Head head) { + CombineHarvester.head = head; + } + + } +}