@@ -59,84 +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- // NB: Regardless of whether the destination type is an array or collection,
65- // we still want to cast directly if doing so is possible. But note that in
66- // general, this check does not detect cases of incompatible generic
67- // parameter types. If this limitation becomes a problem in the future we
68- // can extend the logic here to provide additional signatures of canCast
69- // which operate on Types in general rather than only Classes. However, the
70- // logic could become complex very quickly in various subclassing cases,
71- // generic parameters resolved vs. propagated, etc.
72- final Class <?> c = GenericUtils .getClass (dest );
73- if (c != null && ConversionUtils .canCast (src , c )) return true ;
74-
75- // Handle array types, including generic array types.
76- if (isArray (dest )) return true ;
77-
78- // Handle parameterized collection types.
79- if (dest instanceof ParameterizedType && isCollection (dest )) {
80- return createCollection (GenericUtils .getClass (dest )) != null ;
81- }
82-
83- // This wasn't a collection or array, so convert it as a single element.
84- return canConvert (src , GenericUtils .getClass (dest ));
85- }
86-
87- @ Override
88- public boolean canConvert (final Class <?> src , final Class <?> dest ) {
89- // ensure type is well-behaved, rather than a primitive type
90- final Class <?> saneDest = ConversionUtils .getNonprimitiveType (dest );
91-
92- // OK if the existing object can be casted
93- if (ConversionUtils .canCast (src , saneDest )) return true ;
94-
95- // OK for numerical conversions
96- if (ConversionUtils .canCast (ConversionUtils .getNonprimitiveType (src ),
97- Number .class ) &&
98- (ClassUtils .isByte (dest ) || ClassUtils .isDouble (dest ) ||
99- ClassUtils .isFloat (dest ) || ClassUtils .isInteger (dest ) ||
100- ClassUtils .isLong (dest ) || ClassUtils .isShort (dest )))
101- {
102- return true ;
103- }
104-
105- // OK if string
106- if (saneDest == String .class ) return true ;
107-
108- if (ConversionUtils .canCast (src , String .class )) {
109- // OK if source type is string and destination type is character
110- // (in this case, the first character of the string would be used)
111- if (saneDest == Character .class ) return true ;
112-
113- // OK if source type is string and destination type is an enum
114- if (dest .isEnum ()) return true ;
115- }
116-
117- // OK if appropriate wrapper constructor exists
118- try {
119- return getConstructor (saneDest , src ) != null ;
120- }
121- catch (final Exception exc ) {
122- // TODO: Best not to catch blanket Exceptions here.
123- // no known way to convert
124- return false ;
125- }
126- }
127-
128- @ Override
129- public boolean canConvert (final Object src , final Type dest ) {
130- if (src == null ) return true ;
131- return canConvert (src .getClass (), dest );
132- }
133-
134- @ Override
135- public boolean canConvert (final Object src , final Class <?> dest ) {
136- if (src == null ) return true ;
137- return canConvert (src .getClass (), dest );
138- }
139-
14062 @ Override
14163 public Object convert (final Object src , final Type dest ) {
14264 // NB: Regardless of whether the destination type is an array or collection,
@@ -167,7 +89,6 @@ public Object convert(final Object src, final Type dest) {
16789
16890 @ Override
16991 public <T > T convert (final Object src , final Class <T > dest ) {
170- // TODO: Would be better to split up this method into some helpers.
17192 if (dest == null ) return null ;
17293 if (src == null ) return ConversionUtils .getNullValue (dest );
17394
@@ -360,4 +281,66 @@ private Collection<Object> createCollection(final Class<?> type) {
360281 return null ;
361282 }
362283 }
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+ }
363346}
0 commit comments