1818import java .util .List ;
1919import java .util .Map ;
2020import java .util .Set ;
21+ import java .util .stream .Collectors ;
2122
2223import org .apache .commons .beanutils .BeanMap ;
2324import org .apache .commons .beanutils .BeanUtils ;
@@ -107,13 +108,14 @@ public static Object convert(Object source, Class<?> target) throws ConversionEx
107108 // Don't convert NaN values
108109 return source ;
109110 }
110- if ( target . isInstance ( source )) {
111- return source ;
112- }
113- if ( target . isAssignableFrom ( source . getClass ())) {
111+ final Class <?> sourceClass = source . getClass ();
112+ log . info ( "Source: {} target: {}" , sourceClass , target ) ;
113+ if ( target . isInstance ( source ) || target . isAssignableFrom ( sourceClass )) {
114+ log . info ( "Source: {} is already an instance of: {}" , source , target );
114115 return source ;
115116 }
116117 if (target .isArray ()) {
118+ log .info ("Source: {} to target array: {}" , source , target );
117119 return convertToArray (source , target );
118120 }
119121 if (target .equals (String .class )) {
@@ -128,20 +130,37 @@ public static Object convert(Object source, Class<?> target) throws ConversionEx
128130 if (target .equals (Map .class )) {
129131 return convertBeanToMap (source );
130132 }
131- if (target .equals (List .class ) || target .equals (Collection .class )) {
132- if (source .getClass ().equals (LinkedHashMap .class )) {
133- return convertMapToList ((LinkedHashMap <?, ?>) source );
134- } else if (source .getClass ().isArray ()) {
135- return convertArrayToList ((Object []) source );
133+ if (sourceClass .equals (LinkedHashMap .class )) {
134+ return convertMapToList ((LinkedHashMap <?, ?>) source );
135+ } else if (sourceClass .isArray ()) {
136+ if (List .class .isAssignableFrom (target )) {
137+ log .info ("Source: {} to target list: {}" , source , target );
138+ return Arrays .stream ((Object []) source ).collect (Collectors .toCollection (ArrayList ::new ));
139+ } else if (Set .class .isAssignableFrom (target )) {
140+ log .info ("Source: {} to target set: {}" , source , target );
141+ // special handling for sets when the source is a list
142+ if (source instanceof List ) {
143+ return ((List <?>) source ).stream ().collect (Collectors .toCollection (HashSet ::new ));
144+ }
145+ return Arrays .stream ((Object []) source ).collect (Collectors .toCollection (HashSet ::new ));
136146 }
137147 }
138- if (target .equals (Set .class ) && source .getClass ().isArray ()) {
139- return convertArrayToSet ((Object []) source );
140- }
141- if (target .equals (Set .class ) && source instanceof List ) {
142- return new HashSet ((List ) source );
148+ if (Map .class .isAssignableFrom (sourceClass )) {
149+ return convertMapToBean ((Map ) source , target );
143150 }
144- if (source instanceof Map ) {
151+ // handle immutable collections
152+ final String sourceClassName = sourceClass .getName ();
153+ if (sourceClassName .equals ("java.util.ImmutableCollections$ListN" )) {
154+ if (Set .class .isAssignableFrom (target )) {
155+ return ((List <?>) source ).stream ().collect (Collectors .toCollection (HashSet ::new ));
156+ }
157+ return ((List <?>) source ).stream ().collect (Collectors .toCollection (ArrayList ::new ));
158+ } else if (sourceClassName .equals ("java.util.ImmutableCollections$SetN" )) {
159+ if (Set .class .isAssignableFrom (target )) {
160+ return ((Set <?>) source ).stream ().collect (Collectors .toCollection (HashSet ::new ));
161+ }
162+ return ((Set <?>) source ).stream ().collect (Collectors .toCollection (ArrayList ::new ));
163+ } else if (sourceClassName .equals ("java.util.ImmutableCollections$MapN" )) {
145164 return convertMapToBean ((Map ) source , target );
146165 }
147166 throw new ConversionException (String .format ("Unable to preform conversion from %s to %s" , source , target ));
@@ -185,9 +204,7 @@ public static Object convertToArray(Object source, Class<?> target) throws Conve
185204 }
186205
187206 public static List <Object > convertMapToList (Map <?, ?> map ) {
188- List <Object > list = new ArrayList <Object >(map .size ());
189- list .addAll (map .values ());
190- return list ;
207+ return List .of (map .values ());
191208 }
192209
193210 /**
@@ -333,11 +350,7 @@ public static Class<?>[] convertParams(Object[] source) {
333350 * on failure
334351 */
335352 public static List <?> convertArrayToList (Object [] source ) throws ConversionException {
336- List <Object > list = new ArrayList <Object >(source .length );
337- for (Object element : source ) {
338- list .add (element );
339- }
340- return list ;
353+ return Arrays .stream (source ).collect (Collectors .toCollection (ArrayList ::new ));
341354 }
342355
343356 /**
@@ -387,11 +400,7 @@ public static Object convertMapToBean(Map<String, ? extends Object> source, Clas
387400 * @return Set
388401 */
389402 public static Set <?> convertArrayToSet (Object [] source ) {
390- Set <Object > set = new HashSet <Object >();
391- for (Object element : source ) {
392- set .add (element );
393- }
394- return set ;
403+ return Arrays .stream (source ).collect (Collectors .toCollection (HashSet ::new ));
395404 }
396405
397406 /**
0 commit comments