public static Object convertToArray ( Object source , Class < ? > target ) throws ConversionException { try { Class < ? > targetType = target . getComponentType ( ) ; if ( source . getClass ( ) . isArray ( ) ) { Object targetInstance = Array . newInstance ( targetType , Array . getLength ( source ) ) ; for ( int i = 0 ; i < Array . getLength ( source ) ; i ++ ) { Array . set ( targetInstance , i , convert ( Array . get ( source , i ) , targetType ) ) ; } return targetInstance ; } if ( source instanceof Collection < ? > ) { Collection < ? > sourceCollection = ( Collection < ? > ) source ; Object targetInstance = Array . newInstance ( target . getComponentType ( ) , sourceCollection . size ( ) ) ; Iterator < ? > it = sourceCollection . iterator ( ) ; int i = 0 ; while ( it . hasNext ( ) ) { Array . set ( targetInstance , i ++ , convert ( it . next ( ) , targetType ) ) ; } return targetInstance ; } throw new ConversionException ( "Unable to convert to array" ) ; } catch ( Exception ex ) { throw new ConversionException ( "Error converting to array" , ex ) ; } }
Convert to array