99import static io .quarkus .deployment .util .ServiceUtil .classNamesNamedIn ;
1010import static io .smallrye .config .SmallRyeConfig .SMALLRYE_CONFIG_LOCATIONS ;
1111import static java .util .stream .Collectors .toSet ;
12+ import static org .objectweb .asm .Opcodes .ACC_STATIC ;
1213
1314import java .io .Closeable ;
1415import java .io .IOException ;
2425import java .util .LinkedHashSet ;
2526import java .util .List ;
2627import java .util .Map ;
28+ import java .util .Map .Entry ;
2729import java .util .Objects ;
2830import java .util .Optional ;
2931import java .util .Set ;
110112import io .smallrye .config .ConfigSourceInterceptorFactory ;
111113import io .smallrye .config .ConfigValue ;
112114import io .smallrye .config .DefaultValuesConfigSource ;
113- import io .smallrye .config .ProfileConfigSourceInterceptor ;
114115import io .smallrye .config .SecretKeysHandler ;
115116import io .smallrye .config .SecretKeysHandlerFactory ;
116117import io .smallrye .config .SmallRyeConfig ;
@@ -148,10 +149,10 @@ void buildTimeRunTimeConfig(
148149 .build ()) {
149150
150151 FieldDescriptor source = FieldDescriptor .of (classCreator .getClassName (), "source" , ConfigSource .class );
151- classCreator .getFieldCreator (source ).setModifiers (Opcodes . ACC_STATIC | Opcodes .ACC_FINAL );
152+ classCreator .getFieldCreator (source ).setModifiers (ACC_STATIC | Opcodes .ACC_FINAL );
152153
153154 MethodCreator clinit = classCreator .getMethodCreator ("<clinit>" , void .class );
154- clinit .setModifiers (Opcodes . ACC_STATIC );
155+ clinit .setModifiers (ACC_STATIC );
155156
156157 ResultHandle map = clinit .newInstance (MethodDescriptor .ofConstructor (HashMap .class ));
157158 MethodDescriptor put = MethodDescriptor .ofMethod (Map .class , "put" , Object .class , Object .class , Object .class );
@@ -239,30 +240,9 @@ void generateBuilders(
239240 BuildProducer <ReflectiveClassBuildItem > reflectiveClass ) throws Exception {
240241
241242 Map <String , String > defaultValues = new HashMap <>();
242- // Default values from @ConfigRoot
243- for (Map .Entry <String , ConfigValue > entry : configItem .getReadResult ().getRunTimeDefaultValues ().entrySet ()) {
244- defaultValues .put (entry .getKey (), entry .getValue ().getRawValue ());
245- }
246- // Default values from build item RunTimeConfigurationDefaultBuildItem override
247243 for (RunTimeConfigurationDefaultBuildItem e : runTimeDefaults ) {
248244 defaultValues .put (e .getKey (), e .getValue ());
249245 }
250- // Recorded values from build time from any other source (higher ordinal then defaults, so override)
251- SmallRyeConfig config = ConfigProvider .getConfig ().unwrap (SmallRyeConfig .class );
252- List <String > profiles = config .getProfiles ();
253- for (Map .Entry <String , ConfigValue > entry : configItem .getReadResult ().getRunTimeValues ().entrySet ()) {
254- if (DefaultValuesConfigSource .NAME .equals (entry .getValue ().getConfigSourceName ())) {
255- continue ;
256- }
257- // Runtime values may contain active profiled names that override sames names in defaults
258- // We need to keep the original name definition in case a different profile is used to run the app
259- String activeName = ProfileConfigSourceInterceptor .activeName (entry .getKey (), profiles );
260- // But keep the default
261- if (!configItem .getReadResult ().getRunTimeDefaultValues ().containsKey (activeName )) {
262- defaultValues .remove (activeName );
263- }
264- defaultValues .put (entry .getKey (), entry .getValue ().getRawValue ());
265- }
266246
267247 Set <String > converters = discoverService (Converter .class , reflectiveClass );
268248 Set <String > interceptors = discoverService (ConfigSourceInterceptor .class , reflectiveClass );
@@ -303,6 +283,7 @@ void generateBuilders(
303283 combinedIndex ,
304284 sharedFields ,
305285 defaultValues ,
286+ Map .of (),
306287 converters ,
307288 interceptors ,
308289 staticSafeServices (interceptorFactories ),
@@ -319,6 +300,10 @@ void generateBuilders(
319300 reflectiveClass .produce (ReflectiveClassBuildItem .builder (CONFIG_STATIC_NAME ).build ());
320301
321302 // For RunTime Config
303+ Map <String , String > runtimeValues = new HashMap <>();
304+ for (Entry <String , ConfigValue > entry : configItem .getReadResult ().getRunTimeValues ().entrySet ()) {
305+ runtimeValues .put (entry .getKey (), entry .getValue ().getRawValue ());
306+ }
322307 Set <ConfigClass > runTimeMappings = new LinkedHashSet <>();
323308 runTimeMappings .addAll (runtimeConfigMappings (configMappings ));
324309 runTimeMappings .addAll (configItem .getReadResult ().getBuildTimeRunTimeMappings ());
@@ -331,6 +316,7 @@ void generateBuilders(
331316 combinedIndex ,
332317 sharedFields ,
333318 defaultValues ,
319+ runtimeValues ,
334320 converters ,
335321 interceptors ,
336322 interceptorFactories ,
@@ -609,6 +595,9 @@ private static String getPathWithoutExtension(Path path) {
609595 private static final MethodDescriptor WITH_DEFAULTS = MethodDescriptor .ofMethod (AbstractConfigBuilder .class ,
610596 "withDefaultValues" ,
611597 void .class , SmallRyeConfigBuilder .class , Map .class );
598+ private static final MethodDescriptor WITH_RUNTIME_VALUES = MethodDescriptor .ofMethod (AbstractConfigBuilder .class ,
599+ "withRuntimeValues" ,
600+ void .class , SmallRyeConfigBuilder .class , Map .class );
612601 private static final MethodDescriptor WITH_CONVERTER = MethodDescriptor .ofMethod (AbstractConfigBuilder .class ,
613602 "withConverter" ,
614603 void .class , SmallRyeConfigBuilder .class , String .class , int .class , Converter .class );
@@ -654,6 +643,10 @@ private static String getPathWithoutExtension(Path path) {
654643 private static final MethodDescriptor ENSURE_LOADED = MethodDescriptor .ofMethod (AbstractConfigBuilder .class ,
655644 "ensureLoaded" ,
656645 void .class , String .class );
646+ private static final MethodDescriptor MAP_NEW = MethodDescriptor .ofConstructor (HashMap .class , int .class );
647+ private static final MethodDescriptor MAP_PUT = MethodDescriptor .ofMethod (HashMap .class ,
648+ "put" ,
649+ Object .class , Object .class , Object .class );
657650
658651 private static final DotName CONVERTER_NAME = DotName .createSimple (Converter .class .getName ());
659652 private static final DotName PRIORITY_NAME = DotName .createSimple (Priority .class .getName ());
@@ -672,21 +665,21 @@ private static Map<Object, FieldDescriptor> generateSharedConfig(
672665 .build ()) {
673666
674667 MethodCreator clinit = classCreator .getMethodCreator ("<clinit>" , void .class );
675- clinit .setModifiers (Opcodes . ACC_STATIC );
668+ clinit .setModifiers (ACC_STATIC );
676669
677670 int converterIndex = 0 ;
678671 for (String converter : converters ) {
679672 String fieldName = "conv$" + converterIndex ++;
680673 FieldDescriptor converterField = classCreator .getFieldCreator (fieldName , Converter .class )
681- .setModifiers (Opcodes . ACC_STATIC ).getFieldDescriptor ();
674+ .setModifiers (ACC_STATIC ).getFieldDescriptor ();
682675 clinit .writeStaticField (converterField , clinit .newInstance (MethodDescriptor .ofConstructor (converter )));
683676 fields .put (converter , converterField );
684677 }
685678
686679 int mappingIndex = 0 ;
687680 for (ConfigClass mapping : mappings ) {
688681 FieldDescriptor mappingField = classCreator .getFieldCreator ("mapping$" + mappingIndex ++, ConfigClass .class )
689- .setModifiers (Opcodes . ACC_STATIC ).getFieldDescriptor ();
682+ .setModifiers (ACC_STATIC ).getFieldDescriptor ();
690683 clinit .writeStaticField (mappingField , clinit .invokeStaticMethod (CONFIG_CLASS ,
691684 clinit .load (mapping .getType ().getName ()), clinit .load (mapping .getPrefix ())));
692685
@@ -713,6 +706,7 @@ private static void generateConfigBuilder(
713706 CombinedIndexBuildItem combinedIndex ,
714707 Map <Object , FieldDescriptor > sharedFields ,
715708 Map <String , String > defaultValues ,
709+ Map <String , String > runtimeValues ,
716710 Set <String > converters ,
717711 Set <String > interceptors ,
718712 Set <String > interceptorFactories ,
@@ -737,22 +731,31 @@ private static void generateConfigBuilder(
737731 .build ()) {
738732
739733 MethodCreator clinit = classCreator .getMethodCreator ("<clinit>" , void .class );
740- clinit .setModifiers (Opcodes . ACC_STATIC );
734+ clinit .setModifiers (ACC_STATIC );
741735
742736 MethodCreator method = classCreator .getMethodCreator (BUILDER_CUSTOMIZER );
743737 ResultHandle configBuilder = method .getMethodParam (0 );
744738
745- FieldDescriptor defaultsField = classCreator .getFieldCreator ("defaults" , Map .class ).setModifiers (Opcodes . ACC_STATIC )
739+ FieldDescriptor defaultsField = classCreator .getFieldCreator ("defaults" , Map .class ).setModifiers (ACC_STATIC )
746740 .getFieldDescriptor ();
747- clinit .writeStaticField (defaultsField , clinit .newInstance (MethodDescriptor .ofConstructor (HashMap .class , int .class ),
748- clinit .load ((int ) ((float ) defaultValues .size () / 0.75f + 1.0f ))));
749- MethodDescriptor put = MethodDescriptor .ofMethod (HashMap .class , "put" , Object .class , Object .class , Object .class );
741+ clinit .writeStaticField (defaultsField ,
742+ clinit .newInstance (MAP_NEW , clinit .load ((int ) ((float ) defaultValues .size () / 0.75f + 1.0f ))));
750743 for (Map .Entry <String , String > entry : defaultValues .entrySet ()) {
751- clinit .invokeVirtualMethod (put , clinit .readStaticField (defaultsField ), clinit .load (entry .getKey ()),
744+ clinit .invokeVirtualMethod (MAP_PUT , clinit .readStaticField (defaultsField ), clinit .load (entry .getKey ()),
752745 clinit .load (entry .getValue ()));
753746 }
754747 method .invokeStaticMethod (WITH_DEFAULTS , configBuilder , method .readStaticField (defaultsField ));
755748
749+ FieldDescriptor runtimeValuesField = classCreator .getFieldCreator ("runtimeValues" , Map .class )
750+ .setModifiers (ACC_STATIC ).getFieldDescriptor ();
751+ clinit .writeStaticField (runtimeValuesField ,
752+ clinit .newInstance (MAP_NEW , clinit .load ((int ) ((float ) runtimeValues .size () / 0.75f + 1.0f ))));
753+ for (Map .Entry <String , String > entry : runtimeValues .entrySet ()) {
754+ clinit .invokeVirtualMethod (MAP_PUT , clinit .readStaticField (runtimeValuesField ), clinit .load (entry .getKey ()),
755+ clinit .load (entry .getValue ()));
756+ }
757+ method .invokeStaticMethod (WITH_RUNTIME_VALUES , configBuilder , method .readStaticField (runtimeValuesField ));
758+
756759 for (String converter : converters ) {
757760 ClassInfo converterClass = combinedIndex .getComputingIndex ().getClassByName (converter );
758761 Type type = getConverterType (converterClass , combinedIndex );
0 commit comments