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

Do not validate configs in unused beans #19965

Merged
merged 1 commit into from
Oct 7, 2021
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
@@ -1,16 +1,20 @@
package io.quarkus.deployment.builditem;

import java.util.Objects;
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 +25,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 +44,10 @@ public Type getType() {
return type;
}

public DotName getName() {
return name;
}

public boolean isMapping() {
return Type.MAPPING.equals(type);
}
Expand All @@ -47,12 +56,27 @@ public boolean isProperties() {
return Type.PROPERTIES.equals(type);
}

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

public enum Type {
MAPPING,
PROPERTIES
}

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

@Override
public int hashCode() {
return Objects.hash(configClass, prefix, type);
}
}
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,7 +12,6 @@
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;

Expand Down Expand Up @@ -48,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 @@ -105,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 @@ -118,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