@ SuppressWarnings ( "SuspiciousSystemArraycopy" ) public static Object [ ] fixVarArgs ( int length , Class < ? > varArgType , Object [ ] arguments ) { final Object [ ] result ; if ( arguments . length == length && varArgType . isInstance ( arguments [ length - 1 ] ) ) { return arguments ; } else { result = Arrays . copyOf ( arguments , length , Object [ ] . class ) ; } final Object varArgs ; Class < ? > varArgElementType = varArgType . getComponentType ( ) ; if ( varArgElementType . isPrimitive ( ) ) { varArgs = Boxing . unboxAll ( varArgElementType , arguments , length - 1 , - 1 ) ; } else { int varLen = arguments . length - length + 1 ; varArgs = Array . newInstance ( varArgElementType , varLen ) ; System . arraycopy ( arguments , length - 1 , varArgs , 0 , varLen ) ; } result [ length - 1 ] = varArgs ; return result ; }
Fixes an arguments array to fit a given length the last value is an array filled with varargs .