Skip to content

Commit

Permalink
Implement new EnumResolver.constructUsingEnumNamingStrategy() via `…
Browse files Browse the repository at this point in the history
…AnnotatedClass` instead of `Class<?>` (#4032)
  • Loading branch information
JooHyukKim authored Jul 13, 2023
1 parent b14168d commit c22be66
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ public JsonDeserializer<?> createEnumDeserializer(DeserializationContext ctxt,
if (deser == null) {
deser = new EnumDeserializer(constructEnumResolver(enumClass, config, beanDesc),
config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS),
constructEnumNamingStrategyResolver(config, enumClass, beanDesc.getClassInfo()),
constructEnumNamingStrategyResolver(config, beanDesc.getClassInfo()),
// since 2.16
EnumResolver.constructUsingToString(config, beanDesc.getClassInfo())
);
Expand Down Expand Up @@ -1923,7 +1923,7 @@ private KeyDeserializer _createEnumKeyDeserializer(DeserializationContext ctxt,
}
}
EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc);
EnumResolver byEnumNamingResolver = constructEnumNamingStrategyResolver(config, enumClass, beanDesc.getClassInfo());
EnumResolver byEnumNamingResolver = constructEnumNamingStrategyResolver(config, beanDesc.getClassInfo());
EnumResolver byToStringResolver = EnumResolver.constructUsingToString(config, beanDesc.getClassInfo());

// May have @JsonCreator for static factory method
Expand Down Expand Up @@ -2439,7 +2439,10 @@ protected EnumResolver constructEnumResolver(Class<?> enumClass,
* with {@link EnumNamingStrategy} applied for the target class.
*
* @since 2.15
* @deprecated Since 2.16.
* Use {@link #constructEnumNamingStrategyResolver(DeserializationConfig, AnnotatedClass)} instead.
*/
@Deprecated
protected EnumResolver constructEnumNamingStrategyResolver(DeserializationConfig config, Class<?> enumClass,
AnnotatedClass annotatedClass) {
Object namingDef = config.getAnnotationIntrospector().findEnumNamingStrategy(config, annotatedClass);
Expand All @@ -2449,6 +2452,22 @@ protected EnumResolver constructEnumNamingStrategyResolver(DeserializationConfig
: EnumResolver.constructUsingEnumNamingStrategy(config, enumClass, enumNamingStrategy);
}

/**
* Factory method used to resolve an instance of {@link CompactStringObjectMap}
* with {@link EnumNamingStrategy} applied for the target class.
*
* @since 2.16
*/
protected EnumResolver constructEnumNamingStrategyResolver(DeserializationConfig config,
AnnotatedClass annotatedClass)
{
Object namingDef = config.getAnnotationIntrospector().findEnumNamingStrategy(config, annotatedClass);
EnumNamingStrategy enumNamingStrategy = EnumNamingStrategyFactory.createEnumNamingStrategyInstance(
namingDef, config.canOverrideAccessModifiers());
return enumNamingStrategy == null ? null
: EnumResolver.constructUsingEnumNamingStrategy(config, annotatedClass, enumNamingStrategy);
}

/**
* @since 2.9
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ public static EnumResolver constructUsingIndex(DeserializationConfig config,
* The output {@link EnumResolver} should contain values that are symmetric to
* {@link EnumValues#constructUsingEnumNamingStrategy(MapperConfig, Class, EnumNamingStrategy)}.
* @since 2.15
* @deprecated Since 2.16. Use
* {@link #constructUsingEnumNamingStrategy(DeserializationConfig, AnnotatedClass, EnumNamingStrategy)}.
*/
@Deprecated
public static EnumResolver constructUsingEnumNamingStrategy(DeserializationConfig config,
Class<?> enumCls, EnumNamingStrategy enumNamingStrategy) {
return _constructUsingEnumNamingStrategy(config, enumCls, enumNamingStrategy);
Expand All @@ -276,7 +279,10 @@ public static EnumResolver constructUsingEnumNamingStrategy(DeserializationConfi
* {@link EnumResolver#constructUsingEnumNamingStrategy(DeserializationConfig, Class, EnumNamingStrategy)}
*
* @since 2.15
* @deprecated Since 2.16. Use
* {@link #_constructUsingEnumNamingStrategy(DeserializationConfig, AnnotatedClass, EnumNamingStrategy)}.
*/
@Deprecated
private static EnumResolver _constructUsingEnumNamingStrategy(
DeserializationConfig config, Class<?> enumCls0, EnumNamingStrategy enumNamingStrategy)
{
Expand All @@ -297,6 +303,47 @@ private static EnumResolver _constructUsingEnumNamingStrategy(
_enumDefault(ai, enumCls), isIgnoreCase, false);
}

/**
* Factory method for constructing an {@link EnumResolver} with {@link EnumNamingStrategy} applied.
*
* @since 2.16
*/
public static EnumResolver constructUsingEnumNamingStrategy(DeserializationConfig config,
AnnotatedClass annotatedClass, EnumNamingStrategy enumNamingStrategy)
{
return _constructUsingEnumNamingStrategy(config, annotatedClass, enumNamingStrategy);
}

/**
* Internal method for
* {@link EnumResolver#constructUsingEnumNamingStrategy(DeserializationConfig, AnnotatedClass, EnumNamingStrategy)}
*
* @since 2.16
*/
private static EnumResolver _constructUsingEnumNamingStrategy(
DeserializationConfig config, AnnotatedClass annotatedClass, EnumNamingStrategy enumNamingStrategy)
{
// prepare data
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
final boolean isIgnoreCase = config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
final Class<?> enumCls0 = annotatedClass.getRawType();
final Class<Enum<?>> enumCls = _enumClass(enumCls0);
final Enum<?>[] enumConstants = _enumConstants(enumCls0);
final Enum<?> defaultEnum = _enumDefault(ai, annotatedClass, enumConstants);

// finally build
// from last to first, so that in case of duplicate values, first wins
HashMap<String, Enum<?>> map = new HashMap<>();
for (int i = enumConstants.length; --i >= 0; ) {
Enum<?> anEnum = enumConstants[i];
String translatedExternalValue = enumNamingStrategy.convertEnumToExternalName(anEnum.name());
map.put(translatedExternalValue, anEnum);
}

return new EnumResolver(enumCls, enumConstants, map,
defaultEnum, isIgnoreCase, false);
}

/**
* Method used when actual String serialization is indicated using @JsonValue
* on a method in Enum class.
Expand Down

0 comments on commit c22be66

Please sign in to comment.