use of io.quarkus.runtime.annotations.ConfigItem in project quarkus by quarkusio.
the class ConfigInstantiator method handleObject.
private static void handleObject(String prefix, Object o, SmallRyeConfig config, List<String> quarkusPropertyNames) {
try {
final Class<?> cls = o.getClass();
if (!isClassNameSuffixSupported(o)) {
return;
}
for (Field field : cls.getDeclaredFields()) {
if (field.isSynthetic() || Modifier.isFinal(field.getModifiers())) {
continue;
}
field.setAccessible(true);
ConfigItem configItem = field.getDeclaredAnnotation(ConfigItem.class);
final Class<?> fieldClass = field.getType();
if (configItem == null || fieldClass.isAnnotationPresent(ConfigGroup.class)) {
Constructor<?> constructor = fieldClass.getConstructor();
constructor.setAccessible(true);
Object newInstance = constructor.newInstance();
field.set(o, newInstance);
handleObject(prefix + "." + dashify(field.getName()), newInstance, config, quarkusPropertyNames);
} else {
String name = configItem.name();
if (name.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
name = dashify(field.getName());
} else if (name.equals(ConfigItem.ELEMENT_NAME)) {
name = field.getName();
}
String fullName = prefix + "." + name;
final Type genericType = field.getGenericType();
if (fieldClass == Map.class) {
field.set(o, handleMap(fullName, genericType, config, quarkusPropertyNames));
continue;
}
final Converter<?> conv = getConverterFor(genericType, config);
try {
Optional<?> value = config.getOptionalValue(fullName, conv);
if (value.isPresent()) {
field.set(o, value.get());
} else if (!configItem.defaultValue().equals(ConfigItem.NO_DEFAULT)) {
// the runtime config source handles default automatically
// however this may not have actually been installed depending on where the failure occured
field.set(o, conv.convert(configItem.defaultValue()));
}
} catch (NoSuchElementException ignored) {
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations