Skip to content

Commit

Permalink
Allow ConfigMappings with default visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 17, 2023
1 parent b687fbb commit 5ab553a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
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);
}
}
}

0 comments on commit 5ab553a

Please sign in to comment.