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

Allow ConfigMappings with default visibility #32692

Merged
merged 1 commit into from
Apr 21, 2023
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
Expand Up @@ -173,8 +173,6 @@ public final class RunTimeConfigurationGenerator {
static final MethodDescriptor CU_ADD_SOURCE_FACTORY_PROVIDER = MethodDescriptor.ofMethod(ConfigUtils.class,
"addSourceFactoryProvider",
void.class, SmallRyeConfigBuilder.class, ConfigSourceFactoryProvider.class);
static final MethodDescriptor CU_WITH_MAPPING = MethodDescriptor.ofMethod(ConfigUtils.class, "addMapping",
void.class, SmallRyeConfigBuilder.class, String.class, String.class);

static final MethodDescriptor RCS_NEW = MethodDescriptor.ofConstructor(RuntimeConfigSource.class, String.class);
static final MethodDescriptor RCSP_NEW = MethodDescriptor.ofConstructor(RuntimeConfigSourceProvider.class, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import io.quarkus.runtime.configuration.ConfigRecorder;
import io.quarkus.runtime.configuration.DefaultsConfigSource;
import io.quarkus.runtime.configuration.DisableableConfigSource;
import io.quarkus.runtime.configuration.MappingsConfigBuilder;
import io.quarkus.runtime.configuration.QuarkusConfigValue;
import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource;
import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
Expand All @@ -91,10 +92,6 @@ public class ConfigGenerationBuildStep {
SmallRyeConfigBuilder.class, "withSources",
SmallRyeConfigBuilder.class, ConfigSource[].class);

private static final MethodDescriptor WITH_MAPPING = MethodDescriptor.ofMethod(
SmallRyeConfigBuilder.class, "withMapping",
SmallRyeConfigBuilder.class, Class.class, String.class);

@BuildStep
void staticInitSources(
BuildProducer<StaticInitConfigSourceProviderBuildItem> staticInitConfigSourceProviderBuildItem,
Expand Down Expand Up @@ -553,23 +550,25 @@ private static void generateMappingsConfigBuilder(
.classOutput(new GeneratedClassGizmoAdaptor(generatedClass, true))
.className(className)
.interfaces(ConfigBuilder.class)
.superClass(MappingsConfigBuilder.class)
.setFinal(true)
.build()) {

MethodCreator method = classCreator.getMethodCreator(CONFIG_BUILDER);
ResultHandle configBuilder = method.getMethodParam(0);

MethodDescriptor addMapping = MethodDescriptor.ofMethod(MappingsConfigBuilder.class, "addMapping", void.class,
SmallRyeConfigBuilder.class, String.class, String.class);

for (ConfigClassWithPrefix mapping : mappings) {
method.invokeVirtualMethod(WITH_MAPPING, configBuilder,
method.loadClass(mapping.getKlass()),
method.invokeStaticMethod(addMapping, configBuilder, method.load(mapping.getKlass().getName()),
method.load(mapping.getPrefix()));
}

method.returnValue(configBuilder);
}

reflectiveClass
.produce(ReflectiveClassBuildItem.builder(className).build());
reflectiveClass.produce(ReflectiveClassBuildItem.builder(className).build());
}

private static Set<String> discoverService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,6 @@ public static void addSourceFactoryProvider(SmallRyeConfigBuilder builder, Confi
builder.withSources(provider.getConfigSourceFactory(Thread.currentThread().getContextClassLoader()));
}

public static void addMapping(SmallRyeConfigBuilder builder, String mappingClass, String prefix) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
builder.withMapping(contextClassLoader.loadClass(mappingClass), prefix);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public static List<String> getProfiles() {
return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getProfiles();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.runtime.configuration;

import io.smallrye.config.SmallRyeConfigBuilder;

/**
* To support mappings that are not public
*/
public abstract class MappingsConfigBuilder implements ConfigBuilder {
protected static void addMapping(SmallRyeConfigBuilder builder, String mappingClass, String prefix) {
// TODO - Ideally should use the classloader passed to Config, but the method is not public. Requires a change in SmallRye Config.
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
builder.withMapping(contextClassLoader.loadClass(mappingClass), prefix);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}