16
16
public class ClassNameIdResolver
17
17
extends TypeIdResolverBase
18
18
{
19
+ private final static String JAVA_UTIL_PKG = "java.util." ;
20
+
19
21
public ClassNameIdResolver (JavaType baseType , TypeFactory typeFactory ) {
20
22
super (baseType , typeFactory );
21
23
}
@@ -86,8 +88,8 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti
86
88
/* Internal methods
87
89
/**********************************************************
88
90
*/
89
-
90
- protected final String _idFrom (Object value , Class <?> cls , TypeFactory typeFactory )
91
+
92
+ protected String _idFrom (Object value , Class <?> cls , TypeFactory typeFactory )
91
93
{
92
94
// Need to ensure that "enum subtypes" work too
93
95
if (Enum .class .isAssignableFrom (cls )) {
@@ -96,8 +98,8 @@ protected final String _idFrom(Object value, Class<?> cls, TypeFactory typeFacto
96
98
}
97
99
}
98
100
String str = cls .getName ();
99
- if (str .startsWith ("java.util" )) {
100
- // 25-Jan-2009, tatu: There are some internal classes that we can not access as is.
101
+ if (str .startsWith (JAVA_UTIL_PKG )) {
102
+ // 25-Jan-2009, tatu: There are some internal classes that we cannot access as is.
101
103
// We need better mechanism; for now this has to do...
102
104
103
105
// Enum sets and maps are problematic since we MUST know type of
@@ -113,20 +115,20 @@ protected final String _idFrom(Object value, Class<?> cls, TypeFactory typeFacto
113
115
// not optimal: but EnumMap is not a customizable type so this is sort of ok
114
116
str = typeFactory .constructMapType (EnumMap .class , enumClass , valueClass ).toCanonical ();
115
117
} else {
116
- String end = str . substring ( 9 );
117
- if (( end . startsWith ( ".Arrays$" ) || end . startsWith ( ".Collections$" ))
118
- && str . indexOf ( "List" ) >= 0 ) {
119
- /* 17-Feb-2010, tatus: Another such case: result of
120
- * Arrays.asList() is named like so in Sun JDK...
121
- * Let's just plain old ArrayList in its place
122
- * NOTE: chances are there are plenty of similar cases
123
- * for other wrappers... (immutable, singleton, synced etc)
124
- */
125
- str = "java.util.ArrayList" ;
118
+ // 17-Feb-2010, tatus: Another such case: result of Arrays.asList() is
119
+ // named like so in Sun JDK... Let's just plain old ArrayList in its place.
120
+ // ... also, other similar cases exist...
121
+ String suffix = str . substring ( JAVA_UTIL_PKG . length ());
122
+ if ( isJavaUtilCollectionClass ( suffix , "List" )) {
123
+ str = ArrayList . class . getName ();
124
+ } else if ( isJavaUtilCollectionClass ( suffix , "Map" )){
125
+ str = HashMap . class . getName ();
126
+ } else if ( isJavaUtilCollectionClass ( suffix , "Set" )){
127
+ str = HashSet . class . getName () ;
126
128
}
127
129
}
128
130
} else if (str .indexOf ('$' ) >= 0 ) {
129
- /* Other special handling may be needed for inner classes, [JACKSON-584].
131
+ /* Other special handling may be needed for inner classes,
130
132
* The best way to handle would be to find 'hidden' constructor; pass parent
131
133
* value etc (which is actually done for non-anonymous static classes!),
132
134
* but that is just not possible due to various things. So, we will instead
@@ -135,10 +137,8 @@ protected final String _idFrom(Object value, Class<?> cls, TypeFactory typeFacto
135
137
*/
136
138
Class <?> outer = ClassUtil .getOuterClass (cls );
137
139
if (outer != null ) {
138
- /* one more check: let's actually not worry if the declared
139
- * static type is non-static as well; if so, deserializer does
140
- * have a chance at figuring it all out.
141
- */
140
+ // one more check: let's actually not worry if the declared static type is
141
+ // non-static as well; if so, deserializer does have a chance at figuring it all out.
142
142
Class <?> staticType = _baseType .getRawClass ();
143
143
if (ClassUtil .getOuterClass (staticType ) == null ) {
144
144
// Is this always correct? Seems like it should be...
@@ -154,4 +154,17 @@ protected final String _idFrom(Object value, Class<?> cls, TypeFactory typeFacto
154
154
public String getDescForKnownTypeIds () {
155
155
return "class name used as type id" ;
156
156
}
157
+
158
+ private static boolean isJavaUtilCollectionClass (String clz , String type )
159
+ {
160
+ if (clz .startsWith ("Collections$" )) {
161
+ // 02-Jan-2017, tatu: As per [databind#1868], may need to leave Unmodifiable variants as is
162
+ return (clz .indexOf (type ) > 0 )
163
+ && !clz .contains ("Unmodifiable" );
164
+ }
165
+ if (clz .startsWith ("Arrays$" )) {
166
+ return (clz .indexOf (type ) > 0 );
167
+ }
168
+ return false ;
169
+ }
157
170
}
0 commit comments