public CompletableFuture handle()

in com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/SetVariableRequestHandler.java [61:137]


    public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
        SetVariableArguments setVarArguments = (SetVariableArguments) arguments;
        if (setVarArguments.value == null) {
            // Just exit out of editing if we're given an empty expression.
            return CompletableFuture.completedFuture(response);
        } else if (setVarArguments.variablesReference == -1) {
            throw AdapterUtils.createCompletionException(
                "SetVariablesRequest: property 'variablesReference' is missing, null, or empty",
                ErrorCode.ARGUMENT_MISSING);
        } else if (StringUtils.isBlank(setVarArguments.name)) {
            throw AdapterUtils.createCompletionException(
                "SetVariablesRequest: property 'name' is missing, null, or empty",
                ErrorCode.ARGUMENT_MISSING);
        }

        this.context = context;
        boolean showStaticVariables = DebugSettings.getCurrent().showStaticVariables;
        IVariableFormatter variableFormatter = context.getVariableFormatter();
        Map<String, Object> options = variableFormatter.getDefaultOptions();
        VariableUtils.applyFormatterOptions(options, setVarArguments.format != null && setVarArguments.format.hex);

        Object container = context.getRecyclableIdPool().getObjectById(setVarArguments.variablesReference);
        // container is null means the stack frame is continued by user manually.
        if (container == null) {
            throw AdapterUtils.createCompletionException(
                "Failed to set variable. Reason: Cannot set value because the thread is resumed.",
                ErrorCode.SET_VARIABLE_FAILURE);
        }

        String name = setVarArguments.name;
        Value newValue = null;
        String belongToClass = null;

        if (setVarArguments.name.contains("(")) {
            name = setVarArguments.name.replaceFirst(PATTERN, "$1");
            belongToClass = setVarArguments.name.replaceFirst(PATTERN, "$2");
        }

        try {
            Object containerObj = ((VariableProxy) container).getProxiedVariable();
            if (containerObj instanceof StackFrameReference) {
                StackFrameReference stackFrameReference = (StackFrameReference) containerObj;
                StackFrame sf = context.getStackFrameManager().getStackFrame(stackFrameReference);
                newValue = handleSetValueForStackFrame(name, belongToClass, setVarArguments.value,
                        showStaticVariables, sf, options);
            } else if (containerObj instanceof ObjectReference) {
                newValue = handleSetValueForObject(name, belongToClass, setVarArguments.value, (ObjectReference) containerObj, options);
            } else {
                throw AdapterUtils.createCompletionException(
                    String.format("SetVariableRequest: Variable %s cannot be found.", setVarArguments.variablesReference),
                    ErrorCode.SET_VARIABLE_FAILURE);
            }
        } catch (IllegalArgumentException | AbsentInformationException | InvalidTypeException
                | UnsupportedOperationException | ClassNotLoadedException e) {
            throw AdapterUtils.createCompletionException(
                String.format("Failed to set variable. Reason: %s", e.toString()),
                ErrorCode.SET_VARIABLE_FAILURE,
                e);
        }
        int referenceId = 0;
        if (newValue instanceof ObjectReference && VariableUtils.hasChildren(newValue, showStaticVariables)) {
            long threadId = ((VariableProxy) container).getThreadId();
            String scopeName = ((VariableProxy) container).getScope();
            VariableProxy varProxy = new VariableProxy(((VariableProxy) container).getThread(), scopeName, newValue, (VariableProxy) container, name);
            referenceId = context.getRecyclableIdPool().addObject(threadId, varProxy);
        }

        int indexedVariables = 0;
        if (newValue instanceof ArrayReference) {
            indexedVariables = ((ArrayReference) newValue).length();
        }
        response.body = new Responses.SetVariablesResponseBody(
                context.getVariableFormatter().typeToString(newValue == null ? null : newValue.type(), options), // type
                context.getVariableFormatter().valueToString(newValue, options), // value,
                referenceId, indexedVariables);
        return CompletableFuture.completedFuture(response);
    }