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 Oct 1, 2021
1 parent 84fd435 commit 9f5080b
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import java.util.Set;

import org.jboss.jandex.DotName;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.annotations.StaticInitSafe;

public final class ConfigClassBuildItem extends MultiBuildItem {
private final Class<?> configClass;
private final Set<String> generatedClasses;
private final String prefix;
private final Type type;

private final DotName name;

public ConfigClassBuildItem(
final Class<?> configClass,
final Set<String> generatedClasses,
Expand All @@ -21,6 +24,7 @@ public ConfigClassBuildItem(
this.generatedClasses = generatedClasses;
this.prefix = prefix;
this.type = type;
this.name = DotName.createSimple(configClass.getName());
}

public Class<?> getConfigClass() {
Expand All @@ -39,6 +43,10 @@ public Type getType() {
return type;
}

public DotName getName() {
return name;
}

public boolean isMapping() {
return Type.MAPPING.equals(type);
}
Expand All @@ -47,10 +55,6 @@ public boolean isProperties() {
return Type.PROPERTIES.equals(type);
}

public boolean isStaticInitSafe() {
return configClass.isAnnotationPresent(StaticInitSafe.class);
}

public enum Type {
MAPPING,
PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.deployment.builditem;

import java.util.Objects;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.annotations.StaticInitSafe;

public final class ConfigMappingBuildItem extends MultiBuildItem {
private final Class<?> configClass;
private final String prefix;

public ConfigMappingBuildItem(final Class<?> configClass, final String prefix) {
this.configClass = configClass;
this.prefix = prefix;
}

public Class<?> getConfigClass() {
return configClass;
}

public String getPrefix() {
return prefix;
}

public boolean isStaticInitSafe() {
return configClass.isAnnotationPresent(StaticInitSafe.class);
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ConfigMappingBuildItem that = (ConfigMappingBuildItem) o;
return configClass.equals(that.configClass) &&
prefix.equals(that.prefix);
}

@Override
public int hashCode() {
return Objects.hash(configClass, prefix);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.deployment.builditem;

import java.util.Objects;

import io.quarkus.builder.item.MultiBuildItem;

public final class ConfigPropertiesBuildItem extends MultiBuildItem {
private final Class<?> configClass;
private final String prefix;

public ConfigPropertiesBuildItem(final Class<?> configClass, final String prefix) {
this.configClass = configClass;
this.prefix = prefix;
}

public Class<?> getConfigClass() {
return configClass;
}

public String getPrefix() {
return prefix;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ConfigPropertiesBuildItem that = (ConfigPropertiesBuildItem) o;
return configClass.equals(that.configClass) &&
prefix.equals(that.prefix);
}

@Override
public int hashCode() {
return Objects.hash(configClass, prefix);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package io.quarkus.deployment.configuration;

import static io.quarkus.deployment.builditem.ConfigClassBuildItem.Type.MAPPING;
import static io.quarkus.deployment.builditem.ConfigClassBuildItem.Type.PROPERTIES;
import static java.util.Collections.emptySet;
import static org.eclipse.microprofile.config.inject.ConfigProperties.UNCONFIGURED_PREFIX;
import static org.jboss.jandex.AnnotationTarget.Kind.CLASS;
import static org.jboss.jandex.AnnotationTarget.Kind.FIELD;
import static org.jboss.jandex.AnnotationTarget.Kind.METHOD_PARAMETER;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -18,12 +12,10 @@
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ConfigClassBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
Expand All @@ -38,7 +30,6 @@ public class ConfigMappingUtils {
private ConfigMappingUtils() {
}

@BuildStep
public static void generateConfigClasses(
CombinedIndexBuildItem combinedIndex,
BuildProducer<GeneratedClassBuildItem> generatedClasses,
Expand All @@ -50,25 +41,6 @@ public static void generateConfigClasses(
AnnotationTarget target = instance.target();
AnnotationValue annotationPrefix = instance.value("prefix");

if (target.kind().equals(FIELD)) {
if (annotationPrefix != null && !annotationPrefix.asString().equals(UNCONFIGURED_PREFIX)) {
configClasses.produce(
toConfigClassBuildItem(instance, toClass(target.asField().type().name()),
annotationPrefix.asString()));
continue;
}
}

if (target.kind().equals(METHOD_PARAMETER)) {
if (annotationPrefix != null && !annotationPrefix.asString().equals(UNCONFIGURED_PREFIX)) {
ClassType classType = target.asMethodParameter().method().parameters()
.get(target.asMethodParameter().position()).asClassType();
configClasses
.produce(toConfigClassBuildItem(instance, toClass(classType.name()), annotationPrefix.asString()));
continue;
}
}

if (!target.kind().equals(CLASS)) {
continue;
}
Expand Down Expand Up @@ -107,7 +79,8 @@ public static void generateConfigClasses(
}
}

configClasses.produce(toConfigClassBuildItem(instance, configClass, generatedClassesNames, prefix));
configClasses.produce(
new ConfigClassBuildItem(configClass, generatedClassesNames, prefix, getConfigClassType(instance)));
}
}

Expand All @@ -120,22 +93,11 @@ private static Class<?> toClass(DotName dotName) {
}
}

private static ConfigClassBuildItem toConfigClassBuildItem(
AnnotationInstance instance,
Class<?> configClass,
String prefix) {
return toConfigClassBuildItem(instance, configClass, emptySet(), prefix);
}

private static ConfigClassBuildItem toConfigClassBuildItem(
AnnotationInstance instance,
Class<?> configClass,
Set<String> generatedClasses,
String prefix) {
private static ConfigClassBuildItem.Type getConfigClassType(AnnotationInstance instance) {
if (instance.name().equals(CONFIG_MAPPING_NAME)) {
return new ConfigClassBuildItem(configClass, generatedClasses, prefix, MAPPING);
return ConfigClassBuildItem.Type.MAPPING;
} else {
return new ConfigClassBuildItem(configClass, generatedClasses, prefix, PROPERTIES);
return ConfigClassBuildItem.Type.PROPERTIES;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.AdditionalBootstrapConfigSourceProviderBuildItem;
import io.quarkus.deployment.builditem.AdditionalStaticInitConfigSourceProviderBuildItem;
import io.quarkus.deployment.builditem.ConfigClassBuildItem;
import io.quarkus.deployment.builditem.ConfigMappingBuildItem;
import io.quarkus.deployment.builditem.ConfigurationBuildItem;
import io.quarkus.deployment.builditem.ConfigurationTypeBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
Expand Down Expand Up @@ -114,7 +114,7 @@ void generateConfigClass(
List<AdditionalBootstrapConfigSourceProviderBuildItem> additionalBootstrapConfigSourceProviders,
List<StaticInitConfigSourceProviderBuildItem> staticInitConfigSourceProviders,
List<StaticInitConfigSourceFactoryBuildItem> staticInitConfigSourceFactories,
List<ConfigClassBuildItem> configClasses)
List<ConfigMappingBuildItem> configMappings)
throws IOException {

if (liveReloadBuildItem.isLiveReload()) {
Expand Down Expand Up @@ -150,8 +150,8 @@ void generateConfigClass(
.setRuntimeConfigSources(discoveredConfigSources)
.setRuntimeConfigSourceProviders(discoveredConfigSourceProviders)
.setRuntimeConfigSourceFactories(discoveredConfigSourceFactories)
.setStaticConfigMappings(staticSafeConfigMappings(configClasses))
.setRuntimeConfigMappings(runtimeConfigMappings(configClasses))
.setStaticConfigMappings(staticSafeConfigMappings(configMappings))
.setRuntimeConfigMappings(runtimeConfigMappings(configMappings))
.build()
.run();
}
Expand Down Expand Up @@ -270,17 +270,15 @@ private static Set<String> staticSafeServices(Set<String> services) {
return staticSafe;
}

private static Set<ConfigClassWithPrefix> staticSafeConfigMappings(List<ConfigClassBuildItem> configClasses) {
return configClasses.stream()
.filter(ConfigClassBuildItem::isMapping)
.filter(ConfigClassBuildItem::isStaticInitSafe)
private static Set<ConfigClassWithPrefix> staticSafeConfigMappings(List<ConfigMappingBuildItem> configMappings) {
return configMappings.stream()
.filter(ConfigMappingBuildItem::isStaticInitSafe)
.map(configMapping -> configClassWithPrefix(configMapping.getConfigClass(), configMapping.getPrefix()))
.collect(toSet());
}

private static Set<ConfigClassWithPrefix> runtimeConfigMappings(List<ConfigClassBuildItem> configClasses) {
return configClasses.stream()
.filter(ConfigClassBuildItem::isMapping)
private static Set<ConfigClassWithPrefix> runtimeConfigMappings(List<ConfigMappingBuildItem> configMappings) {
return configMappings.stream()
.map(configMapping -> configClassWithPrefix(configMapping.getConfigClass(), configMapping.getPrefix()))
.collect(toSet());
}
Expand Down
Loading

0 comments on commit 9f5080b

Please sign in to comment.