Skip to content

Commit

Permalink
Introduced AnnotationIntrospector arguments while maintaining backw…
Browse files Browse the repository at this point in the history
…ards compatibility.

Also removed leftover imports that weren't needed anymore.
  • Loading branch information
Alejandro Rivera committed Feb 13, 2016
1 parent 7ee9f78 commit 7e6ba9e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1917,11 +1917,11 @@ protected EnumResolver constructEnumResolver(Class<?> enumClass,
if (config.canOverrideAccessModifiers()) {
ClassUtil.checkAndFixAccess(accessor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
return EnumResolver.constructUnsafeUsingMethod(enumClass, accessor);
return EnumResolver.constructUnsafeUsingMethod(enumClass, accessor, config.getAnnotationIntrospector());
}
// May need to use Enum.toString()
if (config.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING)) {
return EnumResolver.constructUnsafeUsingToString(enumClass);
return EnumResolver.constructUnsafeUsingToString(enumClass, config.getAnnotationIntrospector());
}
return EnumResolver.constructUnsafe(enumClass, config.getAnnotationIntrospector());
}
Expand Down
68 changes: 55 additions & 13 deletions src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.fasterxml.jackson.databind.util;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

import java.lang.reflect.Method;
import java.util.*;
Expand All @@ -13,6 +11,8 @@
*/
public class EnumResolver implements java.io.Serializable
{
private static final AnnotationIntrospector defaultAnnotationInstrospector = null;

private static final long serialVersionUID = 1L;

protected final Class<Enum<?>> _enumClass;
Expand Down Expand Up @@ -56,11 +56,22 @@ public static EnumResolver constructFor(Class<Enum<?>> enumCls, AnnotationIntros
return new EnumResolver(enumCls, enumValues, map, defaultEnum);
}

/**
* @deprecated Since 2.8, use {@link #constructUsingToString(Class, AnnotationIntrospector)} instead
*/
@Deprecated
public static EnumResolver constructUsingToString(Class<Enum<?>> enumCls)
{
return constructUsingToString(enumCls, defaultAnnotationInstrospector);
}

/**
* Factory method for constructing resolver that maps from Enum.toString() into
* Enum value
*
* @since 2.8
*/
public static EnumResolver constructUsingToString(Class<Enum<?>> enumCls)
public static EnumResolver constructUsingToString(Class<Enum<?>> enumCls, AnnotationIntrospector ai)
{
Enum<?>[] enumValues = enumCls.getEnumConstants();
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
Expand All @@ -70,12 +81,22 @@ public static EnumResolver constructUsingToString(Class<Enum<?>> enumCls)
map.put(e.toString(), e);
}

Enum<?> defaultEnum = new JacksonAnnotationIntrospector().findDefaultEnumValue(enumCls);
Enum<?> defaultEnum = ai.findDefaultEnumValue(enumCls);
return new EnumResolver(enumCls, enumValues, map, defaultEnum);
}
}

/**
* @deprecated Since 2.8, use {@link #constructUsingMethod(Class, Method, AnnotationIntrospector)} instead
*/
@Deprecated
public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls, Method accessor) {
return constructUsingMethod(enumCls, accessor, defaultAnnotationInstrospector);
}

public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls,
Method accessor)
/**
* @since 2.8
*/
public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls, Method accessor, AnnotationIntrospector ai)
{
Enum<?>[] enumValues = enumCls.getEnumConstants();
HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
Expand All @@ -92,7 +113,7 @@ public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls,
}
}

Enum<?> defaultEnum = ClassUtil.findFirstAnnotatedEnumValue(enumCls, JsonEnumDefaultValue.class);
Enum<?> defaultEnum = (ai != null) ? ai.findDefaultEnumValue(enumCls) : null;
return new EnumResolver(enumCls, enumValues, map, defaultEnum);
}

Expand All @@ -110,28 +131,49 @@ public static EnumResolver constructUnsafe(Class<?> rawEnumCls, AnnotationIntros
return constructFor(enumCls, ai);
}

/**
* @deprecated Since 2.8, use {@link #constructUnsafeUsingToString(Class, AnnotationIntrospector)} instead
*/
@Deprecated
public static EnumResolver constructUnsafeUsingToString(Class<?> rawEnumCls)
{
return constructUnsafeUsingToString(rawEnumCls, defaultAnnotationInstrospector);
}

/**
* Method that needs to be used instead of {@link #constructUsingToString}
* if static type of enum is not known.
*
* @since 2.8
*/
@SuppressWarnings({ "unchecked" })
public static EnumResolver constructUnsafeUsingToString(Class<?> rawEnumCls)
{
public static EnumResolver constructUnsafeUsingToString(Class<?> rawEnumCls, AnnotationIntrospector ai)
{
// oh so wrong... not much that can be done tho
Class<Enum<?>> enumCls = (Class<Enum<?>>) rawEnumCls;
return constructUsingToString(enumCls);
return constructUsingToString(enumCls, ai);
}

/**
* @deprecated Since 2.8, use {@link #constructUnsafeUsingMethod(Class, Method, AnnotationIntrospector)} instead.
*/
@Deprecated
public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls, Method accessor) {
return constructUnsafeUsingMethod(rawEnumCls, accessor, defaultAnnotationInstrospector);
}

/**
* Method used when actual String serialization is indicated using @JsonValue
* on a method.
*
* @since 2.8
*/
@SuppressWarnings({ "unchecked" })
public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls, Method accessor)
public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls, Method accessor, AnnotationIntrospector ai)
{
// wrong as ever but:
Class<Enum<?>> enumCls = (Class<Enum<?>>) rawEnumCls;
return constructUsingMethod(enumCls, accessor);
return constructUsingMethod(enumCls, accessor, ai);
}

public CompactStringObjectMap constructLookup() {
Expand Down

0 comments on commit 7e6ba9e

Please sign in to comment.