Skip to content

Commit

Permalink
Do not validate configs in unused beans
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 8, 2021
1 parent 3c1bb66 commit 75043a5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ void additionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
}

@BuildStep
void analyzeConfigPropertyInjectionPoints(BeanDiscoveryFinishedBuildItem beanDiscovery,
BuildProducer<ConfigPropertyBuildItem> configProperties,
void registerCustomConfigBeanTypes(BeanDiscoveryFinishedBuildItem beanDiscovery,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {

Expand All @@ -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<ConfigPropertyBuildItem> 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");
Expand All @@ -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())
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,13 +28,11 @@ public void testValidationFailed() {
Assertions.fail();
}

@Unremovable
@ApplicationScoped
static class Configured {

@Inject
@ConfigProperty(name = "unconfigured")
String foo;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,9 +27,10 @@ public class NullConverterTest {

@Test
void nullProperty() {

Assertions.fail();
}

@Unremovable
@ApplicationScoped
static class NullConverterBean {
@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 75043a5

Please sign in to comment.