Search in sources :

Example 1 with ConfigItem

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);
    }
}
Also used : ConfigItem(io.quarkus.runtime.annotations.ConfigItem) NoSuchElementException(java.util.NoSuchElementException) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConfigGroup(io.quarkus.runtime.annotations.ConfigGroup) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ConfigGroup (io.quarkus.runtime.annotations.ConfigGroup)1 ConfigItem (io.quarkus.runtime.annotations.ConfigItem)1 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 NoSuchElementException (java.util.NoSuchElementException)1