99
1010import java .lang .reflect .Constructor ;
1111import java .lang .reflect .InvocationTargetException ;
12- import java .sql .Time ;
1312import java .sql .Timestamp ;
1413import java .text .ParseException ;
1514import java .text .SimpleDateFormat ;
@@ -28,7 +27,7 @@ public final class DefaultMappers {
2827 public static final FallbackMapper FALLBACK = new FallbackMapper ();
2928
3029 public static Map <Class <?>, MapperTypeAdapter > create () {
31- Map <Class <?>, MapperTypeAdapter > map = new HashMap <>();
30+ Map <Class <?>, MapperTypeAdapter > map = new LinkedHashMap <>();
3231
3332 for (MapperTypeAdapter adapter : new MapperTypeAdapter []{
3433 ABSTRACT ,
@@ -64,20 +63,20 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
6463 }
6564 if (type .equals (char .class ) || type .equals (Character .class )) {
6665 if (type .isPrimitive () && element .isNull ())
67- throw new MapperWrongTypeException (context .getField (). getName (), "number" , "null" );
66+ throw new MapperWrongTypeException (context .getFieldName (), "number" , "null" );
6867 String s = element .string (context .getMapper ().isStrict ());
6968 if (s .length () != 1 )
70- throw new MapperException ("Expected string of length 1 for field " + context .getField (). getName () + " but received " + s .length ());
69+ throw new MapperException ("Expected string of length 1 for field " + context .getFieldName () + " but received " + s .length ());
7170 return s .charAt (0 );
7271 }
7372 if (type .equals (Boolean .class ) || type .equals (boolean .class )) {
7473 if (type .isPrimitive () && element .isNull ())
75- throw new MapperWrongTypeException (context .getField (). getName (), "number" , "null" );
74+ throw new MapperWrongTypeException (context .getFieldName (), "number" , "null" );
7675 return element .bool (context .getMapper ().isStrict ());
7776 }
7877 if (Number .class .isAssignableFrom (type ) || type .isPrimitive ()) {
7978 if (type .isPrimitive () && element .isNull ())
80- throw new MapperWrongTypeException (context .getField (). getName (), "number" , "null" );
79+ throw new MapperWrongTypeException (context .getFieldName (), "number" , "null" );
8180 if (type .equals (int .class ) || type .equals (Integer .class ))
8281 return element .number (context .getMapper ().isStrict ()).intValue ();
8382 if (type .equals (long .class ) || type .equals (Long .class ))
@@ -95,9 +94,9 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
9594 return element .number (context .getMapper ().isStrict ());
9695 }
9796 } catch (AbstractCoercingException ex ) {
98- throw new MapperWrongTypeException (context .getField (). getName () , "string" , Helpers .typeName (element ));
97+ throw new MapperWrongTypeException (context .getField () != null ? context . getFieldName () : "" , "string" , Helpers .typeName (element ));
9998 }
100- throw new MapperWrongTypeException (context .getField (). getName (), "primitive" , Helpers .typeName (element ));
99+ throw new MapperWrongTypeException (context .getFieldName (), "primitive" , Helpers .typeName (element ));
101100 }
102101
103102 public Class <?>[] getSupportedTypes () {
@@ -136,7 +135,7 @@ public AbstractElement toAbstract(MapperContext context, Object value) throws Ma
136135 }
137136
138137 public Object fromAbstract (MapperContext context , AbstractElement element , Class <?> type ) throws MapperException {
139- if (type .equals (List .class ) || type .equals (AbstractList .class ))
138+ if (type .equals (List .class ) || type .equals (Collection . class ) || type . equals ( AbstractList .class ))
140139 type = ArrayList .class ;
141140 if (type .equals (Set .class ))
142141 type = HashSet .class ;
@@ -157,12 +156,13 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
157156 } catch (InstantiationException | IllegalAccessException e ) {
158157 throw new RuntimeException (e );
159158 } catch (AbstractCoercingException ex ) {
160- throw new MapperWrongTypeException (context .getField (). getName (), "array" , Helpers .typeName (element ));
159+ throw new MapperWrongTypeException (context .getFieldName (), "array" , Helpers .typeName (element ));
161160 }
162161 }
163162
164163 public Class <?>[] getSupportedTypes () {
165164 return new Class []{
165+ Collection .class ,
166166 List .class ,
167167 ArrayList .class ,
168168 CopyOnWriteArrayList .class ,
@@ -216,7 +216,7 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
216216 } catch (InstantiationException | IllegalAccessException e ) {
217217 throw new RuntimeException (e );
218218 } catch (AbstractCoercingException ex ) {
219- throw new MapperWrongTypeException (context .getField (). getName (), "object" , Helpers .typeName (element ));
219+ throw new MapperWrongTypeException (context .getFieldName (), "object" , Helpers .typeName (element ));
220220 }
221221 }
222222
@@ -284,7 +284,7 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
284284 return new Timestamp (date .getTime ());
285285 throw new MapperException ("Unsupported date type '" + type .getName () + "'" );
286286 } catch (ParseException | NumberFormatException | AbstractCoercingException ex ) {
287- throw new MapperException ("Failed to parse date '" + element .string () + "'" + (context .getField () != null ? (" for field '" + context .getField (). getName () + "'" ) : "" ));
287+ throw new MapperException ("Failed to parse date '" + element .string () + "'" + (context .getField () != null ? (" for field '" + context .getFieldName () + "'" ) : "" ));
288288 }
289289 }
290290
@@ -311,13 +311,13 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
311311 if (type .equals (AbstractElement .class ))
312312 return element ;
313313 if (type .equals (AbstractNull .class ) && !(element instanceof AbstractNull ))
314- throw new MapperWrongTypeException (context .getField (). getName (), "null" , Helpers .typeName (element ));
314+ throw new MapperWrongTypeException (context .getFieldName (), "null" , Helpers .typeName (element ));
315315 if (type .equals (AbstractPrimitive .class ) && !(element instanceof AbstractPrimitive ))
316- throw new MapperWrongTypeException (context .getField (). getName (), "primitive" , Helpers .typeName (element ));
316+ throw new MapperWrongTypeException (context .getFieldName (), "primitive" , Helpers .typeName (element ));
317317 if (type .equals (AbstractObject .class ) && !(element instanceof AbstractObject ))
318- throw new MapperWrongTypeException (context .getField (). getName (), "object" , Helpers .typeName (element ));
318+ throw new MapperWrongTypeException (context .getFieldName (), "object" , Helpers .typeName (element ));
319319 if (type .equals (AbstractArray .class ) && !(element instanceof AbstractArray ))
320- throw new MapperWrongTypeException (context .getField (). getName (), "array" , Helpers .typeName (element ));
320+ throw new MapperWrongTypeException (context .getFieldName (), "array" , Helpers .typeName (element ));
321321 return element ;
322322 }
323323
@@ -368,7 +368,7 @@ public AbstractElement toAbstract(MapperContext context, Object value) throws Ma
368368 public Object fromAbstract (MapperContext context , AbstractElement element , Class type ) throws MapperException {
369369 if (type .isEnum ()) {
370370 if (!element .isString ())
371- throw new MapperWrongTypeException (context .getField (). getName (), "string" , Helpers .typeName (element ));
371+ throw new MapperWrongTypeException (context .getFieldName (), "string" , Helpers .typeName (element ));
372372 try {
373373 return Enum .valueOf (type , element .string ());
374374 } catch (IllegalArgumentException ex ) {
@@ -378,7 +378,7 @@ public Object fromAbstract(MapperContext context, AbstractElement element, Class
378378 if (type .equals (UUID .class ))
379379 return UUID .fromString (element .string ());
380380 if (!element .isObject ())
381- throw new MapperWrongTypeException (context .getField (). getName (), "object" , Helpers .typeName (element ));
381+ throw new MapperWrongTypeException (context .getFieldName (), "object" , Helpers .typeName (element ));
382382 MapperTypeSpec spec = MapperTypeSpec .get (type );
383383 if (spec == null )
384384 throw new MapperException ("Unmappable type '" + type .getName () + "'" );
0 commit comments