public static void handleVarArgs()

in src/main/java/com/jetbrains/jdi/MethodImpl.java [347:411]


    public static void handleVarArgs(Method method, List<Value> arguments)
        throws ClassNotLoadedException, InvalidTypeException {
        int paramCount = method.argumentTypeNames().size();
        ArrayType lastParamType;
        if (method instanceof MethodImpl) {
            lastParamType = (ArrayType)((MethodImpl) method).argumentType(paramCount - 1);
        }
        else {
            lastParamType = (ArrayType) method.argumentTypes().get(paramCount - 1);
        }
        int argCount = arguments.size();
        if (argCount < paramCount - 1) {
            // Error; will be caught later.
            return;
        }
        if (argCount == paramCount - 1) {
            // It is ok to pass 0 args to the var arg.
            // We have to gen a 0 length array.
            ArrayReference argArray = lastParamType.newInstance(0);
            arguments.add(argArray);
            return;
        }
        Value nthArgValue = arguments.get(paramCount - 1);
        if (nthArgValue == null && argCount == paramCount) {
            // We have one varargs parameter and it is null
            // so we don't have to do anything.
            return;
        }
        // If the first varargs parameter is null, then don't
        // access its type since it can't be an array.
        Type nthArgType = (nthArgValue == null) ? null : nthArgValue.type();
        if (nthArgType instanceof ArrayTypeImpl) {
            if (argCount == paramCount &&
                ((ArrayTypeImpl)nthArgType).isAssignableTo(lastParamType)) {
                /*
                 * This is case 1.  A compatible array is being passed to the
                 * var args array param.  We don't have to do anything.
                 */
                return;
            }
        }

        /*
         * Case 2.  We have to verify that the n, n+1, ... args are compatible
         * with componentType, and do conversions if necessary and create
         * an array of componentType to hold these possibly converted values.
         */
        int count = argCount - paramCount + 1;
        ArrayReference argArray = lastParamType.newInstance(count);

        /*
         * This will copy arguments(paramCount - 1) ... to argArray(0) ...
         * doing whatever conversions are needed!  It will throw an
         * exception if an incompatible arg is encountered
         */
        argArray.setValues(0, arguments, paramCount - 1, count);
        arguments.set(paramCount - 1, argArray);

        /*
         * Remove the excess args
         */
        if (argCount > paramCount) {
            arguments.subList(paramCount, argCount).clear();
        }
    }