From 75043a5e4f59c3dd49c819b20f934217a2b0ee90 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Tue, 7 Sep 2021 13:38:54 +0100 Subject: [PATCH] Do not validate configs in unused beans --- .../arc/deployment/ConfigBuildStep.java | 59 ++++++++++++------- ...ConfigPropertyInjectionValidationTest.java | 5 +- .../arc/test/config/NullConverterTest.java | 5 +- .../RemovedBeanSkipConfigValidationTest.java | 31 ++++++++++ 4 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/RemovedBeanSkipConfigValidationTest.java diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigBuildStep.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigBuildStep.java index a0d855f1ce38f..88ce41392f65d 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigBuildStep.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigBuildStep.java @@ -90,8 +90,7 @@ void additionalBeans(BuildProducer additionalBeans) { } @BuildStep - void analyzeConfigPropertyInjectionPoints(BeanDiscoveryFinishedBuildItem beanDiscovery, - BuildProducer configProperties, + void registerCustomConfigBeanTypes(BeanDiscoveryFinishedBuildItem beanDiscovery, BuildProducer reflectiveClass, BuildProducer syntheticBeans) { @@ -103,6 +102,42 @@ void analyzeConfigPropertyInjectionPoints(BeanDiscoveryFinishedBuildItem beanDis continue; } + AnnotationInstance configProperty = injectionPoint.getRequiredQualifier(MP_CONFIG_PROPERTY_NAME); + if (configProperty != null) { + // Register a custom bean for injection points that are not handled by ConfigProducer + Type injectedType = injectionPoint.getType(); + if (!isHandledByProducers(injectedType)) { + customBeanTypes.add(injectedType); + } + } + } + + for (Type type : customBeanTypes) { + if (type.kind() != Kind.ARRAY) { + // Implicit converters are most likely used + reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, type.name().toString())); + } + DotName implClazz = type.kind() == Kind.ARRAY ? DotName.createSimple(ConfigBeanCreator.class.getName()) + : type.name(); + syntheticBeans.produce(SyntheticBeanBuildItem.configure(implClazz) + .creator(ConfigBeanCreator.class) + .providerType(type) + .types(type) + .addQualifier(MP_CONFIG_PROPERTY_NAME) + .param("requiredType", type.name().toString()).done()); + } + } + + @BuildStep + void processConfigPropertiesmvn(ValidationPhaseBuildItem validationPhase, + BuildProducer configProperties) { + + for (InjectionPointInfo injectionPoint : validationPhase.getContext().getInjectionPoints()) { + if (injectionPoint.hasDefaultedQualifier()) { + // Defaulted qualifier means no @ConfigProperty + continue; + } + AnnotationInstance configProperty = injectionPoint.getRequiredQualifier(MP_CONFIG_PROPERTY_NAME); if (configProperty != null) { AnnotationValue nameValue = configProperty.value("name"); @@ -124,12 +159,7 @@ void analyzeConfigPropertyInjectionPoints(BeanDiscoveryFinishedBuildItem beanDis } } - // Register a custom bean for injection points that are not handled by ConfigProducer Type injectedType = injectionPoint.getType(); - if (!isHandledByProducers(injectedType)) { - customBeanTypes.add(injectedType); - } - if (DotNames.OPTIONAL.equals(injectedType.name()) || DotNames.OPTIONAL_INT.equals(injectedType.name()) || DotNames.OPTIONAL_LONG.equals(injectedType.name()) @@ -151,21 +181,6 @@ void analyzeConfigPropertyInjectionPoints(BeanDiscoveryFinishedBuildItem beanDis configProperties.produce(new ConfigPropertyBuildItem(propertyName, injectedType, propertyDefaultValue)); } } - - for (Type type : customBeanTypes) { - if (type.kind() != Kind.ARRAY) { - // Implicit converters are most likely used - reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, type.name().toString())); - } - DotName implClazz = type.kind() == Kind.ARRAY ? DotName.createSimple(ConfigBeanCreator.class.getName()) - : type.name(); - syntheticBeans.produce(SyntheticBeanBuildItem.configure(implClazz) - .creator(ConfigBeanCreator.class) - .providerType(type) - .types(type) - .addQualifier(MP_CONFIG_PROPERTY_NAME) - .param("requiredType", type.name().toString()).done()); - } } @BuildStep diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigPropertyInjectionValidationTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigPropertyInjectionValidationTest.java index 86582d4112cf1..fad6e0815387f 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigPropertyInjectionValidationTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigPropertyInjectionValidationTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.arc.Unremovable; import io.quarkus.runtime.configuration.ConfigurationException; import io.quarkus.test.QuarkusUnitTest; @@ -27,13 +28,11 @@ public void testValidationFailed() { Assertions.fail(); } + @Unremovable @ApplicationScoped static class Configured { - @Inject @ConfigProperty(name = "unconfigured") String foo; - } - } diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/NullConverterTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/NullConverterTest.java index afd8ecf2f1128..bc04f81556051 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/NullConverterTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/NullConverterTest.java @@ -9,9 +9,11 @@ import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.arc.Unremovable; import io.quarkus.test.QuarkusUnitTest; public class NullConverterTest { @@ -25,9 +27,10 @@ public class NullConverterTest { @Test void nullProperty() { - + Assertions.fail(); } + @Unremovable @ApplicationScoped static class NullConverterBean { @Inject diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/RemovedBeanSkipConfigValidationTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/RemovedBeanSkipConfigValidationTest.java new file mode 100644 index 0000000000000..19bc20a0cffc8 --- /dev/null +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/RemovedBeanSkipConfigValidationTest.java @@ -0,0 +1,31 @@ +package io.quarkus.arc.test.config; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.enterprise.context.ApplicationScoped; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.test.QuarkusUnitTest; + +public class RemovedBeanSkipConfigValidationTest { + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(RemovedConfigBean.class)); + + @Test + void skipConfigValidation() { + assertEquals(0, Arc.container().beanManager().getBeans(RemovedConfigBean.class).size()); + } + + @ApplicationScoped + public static class RemovedConfigBean { + @ConfigProperty(name = "my.prop") + String prop; + } +}