From 4a70fc015fd35270034fe18bc99855d06e1ee1b1 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 10 Oct 2024 17:38:33 +0200 Subject: [PATCH] chore: use enum Signed-off-by: azerr --- .../internal/quarkus/QuarkusConstants.java | 26 ------------ .../core/properties/NamingStrategy.java | 19 +++++++++ .../QuarkusConfigMappingProvider.java | 40 ++++++++++++++----- .../properties/QuarkusConfigRootProvider.java | 16 ++++---- 4 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/NamingStrategy.java diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/QuarkusConstants.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/QuarkusConstants.java index e51bb03cd..dfa3653c8 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/QuarkusConstants.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/QuarkusConstants.java @@ -79,17 +79,6 @@ public class QuarkusConstants { public static final String CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY = "namingStrategy"; - // The method name is used as is to map the configuration property. - public static final String CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM = "VERBATIM"; - - // The method name is derived by replacing case changes with a dash to map the - // configuration property. - public static final String CONFIG_MAPPING_NAMING_STRATEGY_KEBAB_CASE = "KEBAB_CASE"; - - // The method name is derived by replacing case changes with an underscore to - // map the configuration property. - public static final String CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE = "SNAKE_CASE"; - public static final String WITH_NAME_ANNOTATION = "io.smallrye.config.WithName"; public static final String WITH_NAME_ANNOTATION_VALUE = "value"; @@ -107,21 +96,6 @@ public class QuarkusConstants { public static final String CONFIG_PROPERTIES_ANNOTATION_PREFIX = "prefix"; - public static final String CONFIG_PROPERTIES_ANNOTATION_NAMING_STRATEGY = "namingStrategy"; - - public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM = CONFIG_PROPERTIES_ANNOTATION - + ".NamingStrategy"; - - public static final String NAMING_STRATEGY_PREFIX = "NamingStrategy."; - - public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_FROM_CONFIG = NAMING_STRATEGY_PREFIX - + "FROM_CONFIG"; - - public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_VERBATIM = NAMING_STRATEGY_PREFIX + "VERBATIM"; - - public static final String CONFIG_PROPERTIES_NAMING_STRATEGY_ENUM_KEBAB_CASE = NAMING_STRATEGY_PREFIX - + "KEBAB_CASE"; - public static final String QUARKUS_ARC_CONFIG_PROPERTIES_DEFAULT_NAMING_STRATEGY = "quarkus.arc.config-properties-default-naming-strategy"; /** diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/NamingStrategy.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/NamingStrategy.java new file mode 100644 index 000000000..1b2de28e5 --- /dev/null +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/NamingStrategy.java @@ -0,0 +1,19 @@ +package com.redhat.microprofile.psi.internal.quarkus.core.properties; + +public enum NamingStrategy { + + /** + * The method name is used as is to map the configuration property. + */ + VERBATIM, + + /** + * The method name is derived by replacing case changes with a dash to map the configuration property. + */ + KEBAB_CASE, + + /** + * The method name is derived by replacing case changes with an underscore to map the configuration property. + */ + SNAKE_CASE; +} diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigMappingProvider.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigMappingProvider.java index 3bf27b532..acbfba511 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigMappingProvider.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigMappingProvider.java @@ -21,10 +21,12 @@ import com.intellij.psi.PsiModifierListOwner; import com.intellij.psi.PsiType; import com.intellij.psi.util.PsiTreeUtil; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.AbstractAnnotationTypeReferencePropertiesProvider; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.IPropertiesCollector; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.SearchContext; +import com.redhat.devtools.intellij.quarkus.QuarkusConstants; import com.redhat.microprofile.psi.quarkus.PsiQuarkusUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.lsp4mp.commons.metadata.ItemMetadata; @@ -52,8 +54,6 @@ import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION; import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY; import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_ANNOTATION_PREFIX; -import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE; -import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM; import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_DEFAULT_ANNOTATION; import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_DEFAULT_ANNOTATION_VALUE; import static com.redhat.microprofile.psi.internal.quarkus.QuarkusConstants.WITH_NAME_ANNOTATION; @@ -273,18 +273,13 @@ private static String convertName(PsiMember member, PsiAnnotation configMappingA // ConfigMapping.NamingStrategy.VERBATIM) // public interface ServerVerbatimNamingStrategy // --> See https://quarkus.io/guides/config-mappings#namingstrategy - String namingStrategy = getAnnotationMemberValue(configMappingAnnotation, - CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY); + NamingStrategy namingStrategy = getNamingStrategy(configMappingAnnotation); if (namingStrategy != null) { - int index = namingStrategy.lastIndexOf('.'); - if (index != -1) { - namingStrategy = namingStrategy.substring(index + 1); - } switch (namingStrategy) { - case CONFIG_MAPPING_NAMING_STRATEGY_VERBATIM: + case VERBATIM: // The method name is used as is to map the configuration property. return name; - case CONFIG_MAPPING_NAMING_STRATEGY_SNAKE_CASE: + case SNAKE_CASE: // The method name is derived by replacing case changes with an underscore to // map the configuration property. return snake(name); @@ -300,6 +295,31 @@ private static String convertName(PsiMember member, PsiAnnotation configMappingA return hyphenate(name); } + /** + * Returns the Quarkus @ConfigRoot(phase=...) value. + * + * @param configMappingAnnotation + * @return the Quarkus @ConfigRoot(phase=...) value. + */ + private static NamingStrategy getNamingStrategy(PsiAnnotation configMappingAnnotation) { + // 2) Check if ConfigMapping.NamingStrategy is used + // @ConfigMapping(prefix = "server", namingStrategy = + // ConfigMapping.NamingStrategy.VERBATIM) + // public interface ServerVerbatimNamingStrategy + // --> See https://quarkus.io/guides/config-mappings#namingstrategy + String namingStrategy = getAnnotationMemberValue(configMappingAnnotation, + CONFIG_MAPPING_ANNOTATION_NAMING_STRATEGY); + if (namingStrategy != null) { + try { + return NamingStrategy.valueOf(namingStrategy.toUpperCase()); + } + catch(Exception e) { + + } + } + return null; + } + /** * Returns the value of @WithDefault("a value") and null otherwise. * diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigRootProvider.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigRootProvider.java index 645cb1e68..b51bda457 100644 --- a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigRootProvider.java +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/core/properties/QuarkusConfigRootProvider.java @@ -30,8 +30,7 @@ import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; import org.eclipse.lsp4mp.commons.metadata.ItemMetadata; import io.quarkus.runtime.annotations.ConfigItem; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.jetbrains.annotations.NotNull; import java.io.Reader; import java.io.StringReader; @@ -120,14 +119,15 @@ private void processConfigRoot(PsiModifierListOwner psiElement, PsiAnnotation co * @param configRootAnnotation * @return the Quarkus @ConfigRoot(phase=...) value. */ + @NotNull 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; + String phase = AnnotationUtils.getAnnotationMemberValue(configRootAnnotation, QuarkusConstants.CONFIG_ROOT_ANNOTATION_PHASE); + if (phase != null) { + try { + return ConfigPhase.valueOf(phase.toUpperCase()); } - if (value.endsWith(ConfigPhase.BUILD_AND_RUN_TIME_FIXED.name())) { - return ConfigPhase.BUILD_AND_RUN_TIME_FIXED; + catch(Exception e) { + } } return ConfigPhase.BUILD_TIME;