io.quarkus.runtime.annotations.ConfigPhase Java Examples

The following examples show how to use io.quarkus.runtime.annotations.ConfigPhase. 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 6 votes vote down vote up
/**
 * Process Quarkus ConfigRoot annotation from the given
 * <code>psiElement</code>.
 * 
 * @param psiElement          the class, field element which have a Quarkus
 *                             ConfigRoot annotations
 * @param configRootAnnotation the Quarkus ConfigRoot annotation
 * @param javadocCache         the documentation cache
 * @param collector            the properties to fill
 */
private void processConfigRoot(PsiModifierListOwner psiElement, PsiAnnotation configRootAnnotation,
		Map<VirtualFile, Properties> javadocCache, IPropertiesCollector collector) {
	ConfigPhase configPhase = getConfigPhase(configRootAnnotation);
	String configRootAnnotationName = getConfigRootName(configRootAnnotation);
	String extension = getExtensionName(getSimpleName(psiElement), configRootAnnotationName, configPhase);
	if (extension == null) {
		return;
	}
	// Location (JAR, src)
	VirtualFile packageRoot = PsiTypeUtils.getRootDirectory(PsiTreeUtil.getParentOfType(psiElement, PsiFile.class));
	String location = PsiTypeUtils.getLocation(psiElement.getProject(), packageRoot);
	// Quarkus Extension name
	String extensionName = PsiQuarkusUtils.getExtensionName(location);

	String baseKey = extension.isEmpty() ? QuarkusConstants.QUARKUS_PREFIX
			: QuarkusConstants.QUARKUS_PREFIX + '.' + extension;
	processConfigGroup(extensionName, psiElement, baseKey, configPhase, javadocCache, collector);
}
 
Example #2
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 6 votes vote down vote up
private void processMap(PsiField field, String baseKey, String mapValueClass, String docs, String extensionName,
		String source, ConfigPhase configPhase, Map<VirtualFile, Properties> javadocCache,
		IPropertiesCollector collector) {
	final String subKey = baseKey + ".{*}";
	if ("java.util.Map".equals(mapValueClass)) {
		// ignore, Map must be parameterized
	} else if (PsiTypeUtils.isMap(mapValueClass)) {
		String[] rawTypeParameters = getRawTypeParameters(mapValueClass);
		processMap(field, subKey, rawTypeParameters[1], docs, extensionName, source, configPhase, javadocCache,
				collector);
	} else if (PsiTypeUtils.isOptional(mapValueClass)) {
		// Optionals are not allowed as a map value type
	} else {
		PsiClass type = PsiTypeUtils.findType(field.getManager(), mapValueClass);
		if (type == null || PsiTypeUtils.isPrimitiveType(mapValueClass)) {
			// This case comes from when mapValueClass is:
			// - Simple type, like java.lang.String
			// - Type which cannot be found (bad classpath?)
			addItemMetadata(extensionName, field, mapValueClass, null, subKey, null, javadocCache, configPhase,
					collector);
		} else {
			processConfigGroup(extensionName, type, subKey, configPhase, javadocCache, collector);
		}
	}
}
 
Example #3
Source File: ConfigBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
BeanRegistrarBuildItem registerConfigRootsAsBeans(ConfigurationBuildItem configItem) {
    return new BeanRegistrarBuildItem(new BeanRegistrar() {
        @Override
        public void register(RegistrationContext context) {
            for (RootDefinition rootDefinition : configItem.getReadResult().getAllRoots()) {
                if (rootDefinition.getConfigPhase() == ConfigPhase.BUILD_AND_RUN_TIME_FIXED
                        || rootDefinition.getConfigPhase() == ConfigPhase.RUN_TIME) {
                    Class<?> configRootClass = rootDefinition.getConfigurationClass();
                    context.configure(configRootClass).types(configRootClass)
                            .scope(Dependent.class).creator(mc -> {
                                // e.g. return Config.ApplicationConfig
                                ResultHandle configRoot = mc.readStaticField(rootDefinition.getDescriptor());
                                // BUILD_AND_RUN_TIME_FIXED roots are always set before the container is started (in the static initializer of the generated Config class)
                                // However, RUN_TIME roots may be not be set when the bean instance is created 
                                mc.ifNull(configRoot).trueBranch().throwException(CreationException.class,
                                        String.format("Config root [%s] with config phase [%s] not initialized yet.",
                                                configRootClass.getName(), rootDefinition.getConfigPhase().name()));
                                mc.returnValue(configRoot);
                            }).done();
                }
            }
        }
    });
}
 
Example #4
Source File: ConfigGenerationBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Warns if build time config properties have been changed at runtime.
 */
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void checkForBuildTimeConfigChange(
        ConfigChangeRecorder recorder, ConfigurationBuildItem configItem, LoggingSetupBuildItem loggingSetupBuildItem) {
    BuildTimeConfigurationReader.ReadResult readResult = configItem.getReadResult();
    Config config = ConfigProvider.getConfig();

    Map<String, String> values = new HashMap<>();
    for (RootDefinition root : readResult.getAllRoots()) {
        if (root.getConfigPhase() == ConfigPhase.BUILD_AND_RUN_TIME_FIXED ||
                root.getConfigPhase() == ConfigPhase.BUILD_TIME) {

            Iterable<ClassDefinition.ClassMember> members = root.getMembers();
            handleMembers(config, values, members, "quarkus." + root.getRootName() + ".");
        }
    }
    values.remove("quarkus.profile");
    recorder.handleConfigChange(values);
}
 
Example #5
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the Quarkus @ConfigRoot(phase=...) value.
 * 
 * @param configRootAnnotation
 * @return the Quarkus @ConfigRoot(phase=...) value.
 */
private static ConfigPhase getConfigPhase(PsiAnnotation configRootAnnotation) {
	String value = AnnotationUtils.getAnnotationMemberValue(configRootAnnotation, QuarkusConstants.CONFIG_ROOT_ANNOTATION_PHASE);
	if (value != null) {
		if (value.endsWith(ConfigPhase.RUN_TIME.name())) {
			return ConfigPhase.RUN_TIME;
		}
		if (value.endsWith(ConfigPhase.BUILD_AND_RUN_TIME_FIXED.name())) {
			return ConfigPhase.BUILD_AND_RUN_TIME_FIXED;
		}
	}
	return ConfigPhase.BUILD_TIME;
}
 
Example #6
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 #7
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private static int getPhase(ConfigPhase configPhase) {
	switch (configPhase) {
	case BUILD_TIME:
		return ItemMetadata.CONFIG_PHASE_BUILD_TIME;
	case BUILD_AND_RUN_TIME_FIXED:
		return ItemMetadata.CONFIG_PHASE_BUILD_AND_RUN_TIME_FIXED;
	case RUN_TIME:
		return ItemMetadata.CONFIG_PHASE_RUN_TIME;
	default:
		return ItemMetadata.CONFIG_PHASE_BUILD_TIME;
	}
}
 
Example #8
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 #9
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 #10
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 #11
Source File: RootDefinition.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ConfigPhase getConfigPhase() {
    return configPhase;
}
 
Example #12
Source File: RootDefinition.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ConfigPhase getConfigPhase() {
    return configPhase;
}
 
Example #13
Source File: RootDefinition.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public Builder setConfigPhase(final ConfigPhase configPhase) {
    Assert.checkNotNullParam("configPhase", configPhase);
    this.configPhase = configPhase;
    return this;
}