private static Object tryToMakeCompatibleArray ( final Class < ? > expectedComponentClz , final Class < ? > inputComponentClz , Object param ) throws VoltTypeException { int inputLength = Array . getLength ( param ) ; if ( inputComponentClz == expectedComponentClz ) { return param ; } // if it's an empty array, let it through // this is a bit ugly as it might hide passing // arrays of the wrong type, but it "does the right thing" // more often that not I guess... else if ( inputLength == 0 ) { return Array . newInstance ( expectedComponentClz , 0 ) ; } // hack to make strings work with input as bytes else if ( isByteArrayClass ( inputComponentClz ) && ( expectedComponentClz == String . class ) ) { String [ ] values = new String [ inputLength ] ; for ( int i = 0 ; i < inputLength ; i ++ ) { try { values [ i ] = new String ( ( byte [ ] ) Array . get ( param , i ) , "UTF-8" ) ; } catch ( UnsupportedEncodingException ex ) { throw new VoltTypeException ( "tryScalarMakeCompatible: Unsupported encoding:" + expectedComponentClz . getName ( ) + " to provided " + inputComponentClz . getName ( ) ) ; } } return values ; } // hack to make varbinary work with input as hex string else if ( ( inputComponentClz == String . class ) && ( expectedComponentClz == byte [ ] . class ) ) { byte [ ] [ ] values = new byte [ inputLength ] [ ] ; for ( int i = 0 ; i < inputLength ; i ++ ) { values [ i ] = Encoder . hexDecode ( ( String ) Array . get ( param , i ) ) ; } return values ; } else if ( ( inputComponentClz == String . class ) && ( expectedComponentClz == Byte [ ] . class ) ) { Byte [ ] [ ] boxvalues = new Byte [ inputLength ] [ ] ; for ( int i = 0 ; i < inputLength ; i ++ ) { boxvalues [ i ] = ArrayUtils . toObject ( Encoder . hexDecode ( ( String ) Array . get ( param , i ) ) ) ; } return boxvalues ; } else { /* * Arrays can be quite large so it doesn't make sense to silently do the conversion * and incur the performance hit. The client should serialize the correct invocation * parameters */ throw new VoltTypeException ( "tryScalarMakeCompatible: Unable to match parameter array:" + expectedComponentClz . getName ( ) + " to provided " + inputComponentClz . getName ( ) ) ; } }
Factored out code to handle array parameter types .