@@ -59,61 +59,6 @@ public class DefaultConverter extends AbstractConverter<Object, Object> {
5959
6060 // -- ConversionHandler methods --
6161
62- @ Override
63- public boolean canConvert (final Class <?> src , final Type dest ) {
64-
65- // Handle array types, including generic array types.
66- if (isArray (dest )) return true ;
67-
68- // Handle parameterized collection types.
69- if (dest instanceof ParameterizedType && isCollection (dest )) {
70- return createCollection (GenericUtils .getClass (dest )) != null ;
71- }
72-
73- return super .canConvert (src , dest );
74- }
75-
76- @ Override
77- public boolean canConvert (final Class <?> src , final Class <?> dest ) {
78- // ensure type is well-behaved, rather than a primitive type
79- final Class <?> saneDest = ConversionUtils .getNonprimitiveType (dest );
80-
81- // OK if the existing object can be casted
82- if (ConversionUtils .canCast (src , saneDest )) return true ;
83-
84- // OK for numerical conversions
85- if (ConversionUtils .canCast (ConversionUtils .getNonprimitiveType (src ),
86- Number .class ) &&
87- (ClassUtils .isByte (dest ) || ClassUtils .isDouble (dest ) ||
88- ClassUtils .isFloat (dest ) || ClassUtils .isInteger (dest ) ||
89- ClassUtils .isLong (dest ) || ClassUtils .isShort (dest )))
90- {
91- return true ;
92- }
93-
94- // OK if string
95- if (saneDest == String .class ) return true ;
96-
97- if (ConversionUtils .canCast (src , String .class )) {
98- // OK if source type is string and destination type is character
99- // (in this case, the first character of the string would be used)
100- if (saneDest == Character .class ) return true ;
101-
102- // OK if source type is string and destination type is an enum
103- if (dest .isEnum ()) return true ;
104- }
105-
106- // OK if appropriate wrapper constructor exists
107- try {
108- return getConstructor (saneDest , src ) != null ;
109- }
110- catch (final Exception exc ) {
111- // TODO: Best not to catch blanket Exceptions here.
112- // no known way to convert
113- return false ;
114- }
115- }
116-
11762 @ Override
11863 public Object convert (final Object src , final Type dest ) {
11964 // NB: Regardless of whether the destination type is an array or collection,
@@ -144,6 +89,8 @@ public Object convert(final Object src, final Type dest) {
14489
14590 @ Override
14691 public <T > T convert (final Object src , final Class <T > dest ) {
92+ if (dest == null ) return null ;
93+ if (src == null ) return ConversionUtils .getNullValue (dest );
14794
14895 // ensure type is well-behaved, rather than a primitive type
14996 final Class <T > saneDest = ConversionUtils .getNonprimitiveType (dest );
@@ -334,4 +281,66 @@ private Collection<Object> createCollection(final Class<?> type) {
334281 return null ;
335282 }
336283 }
284+
285+ // -- Deprecated API --
286+
287+ @ Override
288+ @ Deprecated
289+ public boolean canConvert (final Class <?> src , final Type dest ) {
290+
291+ // Handle array types, including generic array types.
292+ if (isArray (dest )) return true ;
293+
294+ // Handle parameterized collection types.
295+ if (dest instanceof ParameterizedType && isCollection (dest )) {
296+ return createCollection (GenericUtils .getClass (dest )) != null ;
297+ }
298+
299+ return super .canConvert (src , dest );
300+ }
301+
302+ @ Override
303+ @ Deprecated
304+ public boolean canConvert (final Class <?> src , final Class <?> dest ) {
305+
306+ if (src == null || dest == null ) return true ;
307+
308+ // ensure type is well-behaved, rather than a primitive type
309+ final Class <?> saneDest = ConversionUtils .getNonprimitiveType (dest );
310+
311+ // OK if the existing object can be casted
312+ if (ConversionUtils .canCast (src , saneDest )) return true ;
313+
314+ // OK for numerical conversions
315+ if (ConversionUtils .canCast (ConversionUtils .getNonprimitiveType (src ),
316+ Number .class ) &&
317+ (ClassUtils .isByte (dest ) || ClassUtils .isDouble (dest ) ||
318+ ClassUtils .isFloat (dest ) || ClassUtils .isInteger (dest ) ||
319+ ClassUtils .isLong (dest ) || ClassUtils .isShort (dest )))
320+ {
321+ return true ;
322+ }
323+
324+ // OK if string
325+ if (saneDest == String .class ) return true ;
326+
327+ if (ConversionUtils .canCast (src , String .class )) {
328+ // OK if source type is string and destination type is character
329+ // (in this case, the first character of the string would be used)
330+ if (saneDest == Character .class ) return true ;
331+
332+ // OK if source type is string and destination type is an enum
333+ if (dest .isEnum ()) return true ;
334+ }
335+
336+ // OK if appropriate wrapper constructor exists
337+ try {
338+ return getConstructor (saneDest , src ) != null ;
339+ }
340+ catch (final Exception exc ) {
341+ // TODO: Best not to catch blanket Exceptions here.
342+ // no known way to convert
343+ return false ;
344+ }
345+ }
337346}
0 commit comments