io.quarkus.runtime.annotations.ConfigItem Java Examples

The following examples show how to use io.quarkus.runtime.annotations.ConfigItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the Quarkus extension name according the
 * <code>configRootClassSimpleName</code>, <code>configRootAnnotationName</code>
 * and <code>configPhase</code>.
 * 
 * @param configRootClassSimpleName the simple class name where ConfigRoot is
 *                                  declared.
 * @param configRootAnnotationName  the name declared in the ConfigRoot
 *                                  annotation.
 * @param configPhase               the config phase.
 * @see <a href="https://github.com/quarkusio/quarkus/blob/master/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigDefinition.java#L173">
 *      (registerConfigRoot)</a>
 * @return the Quarkus extension name according the
 *         <code>configRootClassSimpleName</code>,
 *         <code>configRootAnnotationName</code> and <code>configPhase</code>.
 */
private static String getExtensionName(String configRootClassSimpleName, String configRootAnnotationName,
		ConfigPhase configPhase) {
	// See
	// https://github.com/quarkusio/quarkus/blob/master/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigDefinition.java#L173
	// registerConfigRoot
	String rootName = configRootAnnotationName;
	final List<String> segments = toList(camelHumpsIterator(configRootClassSimpleName));
	final List<String> trimmedSegments;
	if (configPhase == ConfigPhase.RUN_TIME) {
		trimmedSegments = withoutSuffix(
				withoutSuffix(
						withoutSuffix(
								withoutSuffix(withoutSuffix(withoutSuffix(segments, "Runtime", "Configuration"),
										"Runtime", "Config"), "Run", "Time", "Configuration"),
								"Run", "Time", "Config"),
						"Configuration"),
				"Config");
	} else if (configPhase == ConfigPhase.BOOTSTRAP) {
		trimmedSegments = withoutSuffix(withoutSuffix(
				withoutSuffix(withoutSuffix(segments, "Bootstrap", "Configuration"), "Bootstrap", "Config"),
				"Configuration"), "Config");
	} else {
		trimmedSegments = withoutSuffix(withoutSuffix(
				withoutSuffix(withoutSuffix(segments, "Build", "Time", "Configuration"), "Build", "Time", "Config"),
				"Configuration"), "Config");
	}
	if (rootName.equals(ConfigItem.PARENT)) {
		rootName = "";
	} else if (rootName.equals(ConfigItem.ELEMENT_NAME)) {
		rootName = String.join("", (Iterable<String>) () -> lowerCaseFirst(trimmedSegments.iterator()));
	} else if (rootName.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
		rootName = String.join("-", (Iterable<String>) () -> lowerCase(trimmedSegments.iterator()));
	}
	return rootName;
}
 
Example #2
Source File: ConfigDescriptionBuildStep.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void processConfig(ConfigPatternMap<Container> patterns, List<ConfigDescriptionBuildItem> ret,
        Properties javadoc) {

    patterns.forEach(new Consumer<Container>() {
        @Override
        public void accept(Container node) {
            Field field = node.findField();
            ConfigItem configItem = field.getAnnotation(ConfigItem.class);
            final ConfigProperty configProperty = field.getAnnotation(ConfigProperty.class);
            String defaultDefault;
            final Class<?> valueClass = field.getType();
            if (valueClass == boolean.class) {
                defaultDefault = "false";
            } else if (valueClass.isPrimitive() && valueClass != char.class) {
                defaultDefault = "0";
            } else {
                defaultDefault = null;
            }
            String defVal = defaultDefault;
            if (configItem != null) {
                final String itemDefVal = configItem.defaultValue();
                if (!itemDefVal.equals(ConfigItem.NO_DEFAULT)) {
                    defVal = itemDefVal;
                }
            } else if (configProperty != null) {
                final String propDefVal = configProperty.defaultValue();
                if (!propDefVal.equals(ConfigProperty.UNCONFIGURED_VALUE)) {
                    defVal = propDefVal;
                }
            }
            String javadocKey = field.getDeclaringClass().getName().replace("$", ".") + "." + field.getName();
            ret.add(new ConfigDescriptionBuildItem("quarkus." + node.getPropertyName(),
                    node.findEnclosingClass().getConfigurationClass(),
                    defVal, javadoc.getProperty(javadocKey)));
        }
    });
}
 
Example #3
Source File: ClassDefinition.java    From quarkus with Apache License 2.0 5 votes vote down vote up
LeafMember(final ClassDefinition classDefinition, final Field field) {
    this.classDefinition = Assert.checkNotNullParam("classDefinition", classDefinition);
    this.field = Assert.checkNotNullParam("field", field);
    final Class<?> declaringClass = field.getDeclaringClass();
    final Class<?> configurationClass = classDefinition.configurationClass;
    if (declaringClass != configurationClass) {
        throw new IllegalArgumentException(
                "Member declaring " + declaringClass + " does not match configuration " + configurationClass);
    }
    descriptor = FieldDescriptor.of(field);
    final ConfigItem configItem = field.getAnnotation(ConfigItem.class);
    String propertyName = ConfigItem.HYPHENATED_ELEMENT_NAME;
    if (configItem != null) {
        propertyName = configItem.name();
        if (propertyName.isEmpty()) {
            throw reportError(field, "Invalid empty property name");
        }
    }
    if (propertyName.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
        this.propertyName = StringUtil.hyphenate(field.getName());
    } else if (propertyName.equals(ConfigItem.ELEMENT_NAME)) {
        this.propertyName = field.getName();
    } else if (propertyName.equals(ConfigItem.PARENT)) {
        this.propertyName = "";
    } else {
        this.propertyName = propertyName;
    }
}
 
Example #4
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Process Quarkus ConfigGroup annotation from the given
 * <code>psiElement</code>.
 * 
 * @param extensionName the Quarkus extension name
 * 
 * @param psiElement   the class, field element which have a Quarkus
 *                      ConfigGroup annotations.
 * @param baseKey       the base key
 * @param configPhase   the phase
 * @param javadocCache  the Javadoc cache
 * @param collector     the properties to fill.
 */
private void processConfigGroup(String extensionName, PsiModifierListOwner psiElement, String baseKey,
		ConfigPhase configPhase, Map<VirtualFile, Properties> javadocCache, IPropertiesCollector collector) {
	if (psiElement instanceof PsiClass) {
		PsiElement[] elements = psiElement.getChildren();
		for (PsiElement child : elements) {
			if (child instanceof PsiField) {
				PsiField field = (PsiField) child;
				if (!canProcess(field)) {
					continue;
				}
				final PsiAnnotation configItemAnnotation = AnnotationUtils.getAnnotation(field,
						QuarkusConstants.CONFIG_ITEM_ANNOTATION);
				String name = configItemAnnotation == null ? hyphenate(field.getName())
						: AnnotationUtils.getAnnotationMemberValue(configItemAnnotation, QuarkusConstants.CONFIG_ANNOTATION_NAME);
				if (name == null) {
					name = ConfigItem.HYPHENATED_ELEMENT_NAME;
				}
				String subKey;
				if (name.equals(ConfigItem.PARENT)) {
					subKey = baseKey;
				} else if (name.equals(ConfigItem.ELEMENT_NAME)) {
					subKey = baseKey + "." + field.getName();
				} else if (name.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
					subKey = baseKey + "." + hyphenate(field.getName());
				} else {
					subKey = baseKey + "." + name;
				}
				final String defaultValue = configItemAnnotation == null ? ConfigItem.NO_DEFAULT
						: AnnotationUtils.getAnnotationMemberValue(configItemAnnotation,
								QuarkusConstants.CONFIG_ITEM_ANNOTATION_DEFAULT_VALUE);

				String fieldTypeName = PsiTypeUtils.getResolvedTypeName(field);
				PsiClass fieldClass = PsiTypeUtils.findType(field.getManager(), fieldTypeName);
				final PsiAnnotation configGroupAnnotation = AnnotationUtils.getAnnotation(fieldClass,
						QuarkusConstants.CONFIG_GROUP_ANNOTATION);
				if (configGroupAnnotation != null) {
					processConfigGroup(extensionName, fieldClass, subKey, configPhase, javadocCache, collector);
				} else {
					addItemMetadata(extensionName, field, fieldTypeName, fieldClass, subKey, defaultValue,
							javadocCache, configPhase, collector);
				}
			}
		}
	}
}
 
Example #5
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
private void addItemMetadata(String extensionName, PsiField field, String fieldTypeName, PsiClass fieldClass,
		String name, String defaultValue, Map<VirtualFile, Properties> javadocCache,
		ConfigPhase configPhase, IPropertiesCollector collector) {

	// Class type
	String type = PsiTypeUtils.getPropertyType(fieldClass, fieldTypeName);

	// Javadoc
	String description = getJavadoc(field, javadocCache);

	// field and class source
	String sourceType = PsiTypeUtils.getSourceType(field);
	String sourceField = PsiTypeUtils.getSourceField(field);

	// Enumerations
	super.updateHint(collector, fieldClass);

	ItemMetadata item = null;
	// Default value for primitive type
	if (PsiTypeUtils.isPrimitiveBoolean(fieldTypeName)) {
		item = super.addItemMetadata(collector, name, type, description, sourceType, sourceField, null,
				defaultValue == null || ConfigItem.NO_DEFAULT.equals(defaultValue) ? "false" : defaultValue,
				extensionName, PsiTypeUtils.isBinary(field));
	} else if (PsiTypeUtils.isNumber(fieldTypeName)) {
		item = super.addItemMetadata(collector, name, type, description, sourceType, sourceField, null,
				defaultValue == null || ConfigItem.NO_DEFAULT.equals(defaultValue) ? "0" : defaultValue,
				extensionName, PsiTypeUtils.isBinary(field));
	} else if (PsiTypeUtils.isMap(fieldTypeName)) {
		// FIXME: find better mean to check field is a Map
		// this code works only if user uses Map as declaration and not if they declare
		// HashMap for instance
		String[] rawTypeParameters = getRawTypeParameters(fieldTypeName);
		if ((rawTypeParameters[0].trim().equals("java.lang.String"))) {
			// The key Map must be a String
			processMap(field, name, rawTypeParameters[1], description, extensionName, sourceType, configPhase,
					javadocCache, collector);
		}
	} else if (PsiTypeUtils.isList(fieldTypeName)) {
		item = super.addItemMetadata(collector, name, type, description, sourceType, sourceField, null,
				defaultValue, extensionName, PsiTypeUtils.isBinary(field));
	} else if (PsiTypeUtils.isOptional(fieldTypeName)) {
		item = super.addItemMetadata(collector, name, type, description, sourceType, sourceField, null,
				defaultValue, extensionName, PsiTypeUtils.isBinary(field));
		item.setRequired(false);
	} else {
		if (ConfigItem.NO_DEFAULT.equals(defaultValue)) {
			defaultValue = null;
		}
		item = super.addItemMetadata(collector, name, type, description, sourceType, sourceField, null,
				defaultValue, extensionName, PsiTypeUtils.isBinary(field));
	}
	if (item != null) {
		item.setPhase(getPhase(configPhase));
	}
	PsiQuarkusUtils.updateConverterKinds(item, field, fieldClass);
}
 
Example #6
Source File: RootDefinition.java    From quarkus with Apache License 2.0 4 votes vote down vote up
RootDefinition(final Builder builder) {
    super(builder);
    this.configPhase = builder.configPhase;
    String rootName = builder.rootName;
    final Class<?> configClass = getConfigurationClass();
    final List<String> segments = toList(camelHumpsIterator(configClass.getSimpleName()));
    final List<String> trimmedSegments;
    if (configPhase == ConfigPhase.RUN_TIME) {
        trimmedSegments = withoutSuffix(
                withoutSuffix(
                        withoutSuffix(
                                withoutSuffix(
                                        withoutSuffix(
                                                withoutSuffix(
                                                        segments,
                                                        "Runtime", "Configuration"),
                                                "Runtime", "Config"),
                                        "Run", "Time", "Configuration"),
                                "Run", "Time", "Config"),
                        "Configuration"),
                "Config");
    } else if (configPhase == ConfigPhase.BOOTSTRAP) {
        trimmedSegments = withoutSuffix(
                withoutSuffix(
                        withoutSuffix(
                                withoutSuffix(
                                        segments,
                                        "Bootstrap", "Configuration"),
                                "Bootstrap", "Config"),
                        "Configuration"),
                "Config");
    } else {
        trimmedSegments = withoutSuffix(
                withoutSuffix(
                        withoutSuffix(
                                withoutSuffix(
                                        segments,
                                        "Build", "Time", "Configuration"),
                                "Build", "Time", "Config"),
                        "Configuration"),
                "Config");
    }
    if (rootName.equals(ConfigItem.PARENT)) {
        rootName = "";
    } else if (rootName.equals(ConfigItem.ELEMENT_NAME)) {
        rootName = String.join("", (Iterable<String>) () -> lowerCaseFirst(trimmedSegments.iterator()));
    } else if (rootName.equals(ConfigItem.HYPHENATED_ELEMENT_NAME)) {
        rootName = String.join("-", (Iterable<String>) () -> lowerCase(trimmedSegments.iterator()));
    }
    this.rootName = rootName;
    this.descriptor = FieldDescriptor.of(CONFIG_CLASS_NAME, String.join("", segments), Object.class);
}
 
Example #7
Source File: ConfigInstantiator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static void handleObject(String prefix, Object o, SmallRyeConfig config) {

        try {
            final Class cls = o.getClass();
            if (!isClassNameSuffixSupported(o)) {
                return;
            }
            for (Field field : cls.getDeclaredFields()) {
                if (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);
                } else if (fieldClass == Map.class) { //TODO: FIXME, this cannot handle Map yet
                    field.set(o, new HashMap<>());
                } 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();
                    final Converter<?> conv = getConverterFor(genericType);
                    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);
        }
    }
 
Example #8
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns the Quarkus @ConfigRoot(name=...) value and
 * {@link ConfigItem#HYPHENATED_ELEMENT_NAME} otherwise.
 * 
 * @param configRootAnnotation @ConfigRoot annotation
 * @return the Quarkus @ConfigRoot(name=...) value and
 *         {@link ConfigItem#HYPHENATED_ELEMENT_NAME} otherwise.
 */
private static String getConfigRootName(PsiAnnotation configRootAnnotation) {
	String value = AnnotationUtils.getAnnotationMemberValue(configRootAnnotation, QuarkusConstants.CONFIG_ANNOTATION_NAME);
	if (value != null) {
		return value;
	}
	return ConfigItem.HYPHENATED_ELEMENT_NAME;
}