Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new EnumResolver.constructUsingMethod() via AnnotatedClass instead of Class<?> #4034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,7 @@ protected EnumResolver constructEnumResolver(Class<?> enumClass,
ClassUtil.checkAndFixAccess(jvAcc.getMember(),
config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
return EnumResolver.constructUsingMethod(config, enumClass, jvAcc);
return EnumResolver.constructUsingMethod(config, beanDesc.getClassInfo(), jvAcc);
}
return EnumResolver.constructFor(config, beanDesc.getClassInfo());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ private static EnumResolver _constructUsingEnumNamingStrategy(
* on a method in Enum class.
*
* @since 2.12
* @deprecated Since 2.16.
* Use {@link #constructUsingMethod(DeserializationConfig, AnnotatedClass, AnnotatedMember)} instead.
*/
@Deprecated
public static EnumResolver constructUsingMethod(DeserializationConfig config,
Class<?> enumCls0, AnnotatedMember accessor)
{
Expand Down Expand Up @@ -330,6 +333,44 @@ public static EnumResolver constructUsingMethod(DeserializationConfig config,
);
}

/**
* Method used when actual String serialization is indicated using @JsonValue
* on a method in Enum class.
*
* @since 2.16
*/
public static EnumResolver constructUsingMethod(DeserializationConfig config,
AnnotatedClass annotatedClass, AnnotatedMember accessor)
{
// 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);

// build
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
// from last to first, so that in case of duplicate values, first wins
for (int i = enumConstants.length; --i >= 0; ) {
Enum<?> en = enumConstants[i];
try {
Object o = accessor.getValue(en);
if (o != null) {
map.put(o.toString(), en);
}
} catch (Exception e) {
throw new IllegalArgumentException("Failed to access @JsonValue of Enum value "+en+": "+e.getMessage());
}
}
return new EnumResolver(enumCls, enumConstants, map,
defaultEnum, isIgnoreCase,
// 26-Sep-2021, tatu: [databind#1850] Need to consider "from int" case
_isIntType(accessor.getRawType())
);
}

public CompactStringObjectMap constructLookup() {
return CompactStringObjectMap.construct(_enumsById);
}
Expand Down