async function applyHCR()

in src/extension.ts [172:230]


async function applyHCR(hcrStatusBar: NotificationBar) {
    const debugSession: vscode.DebugSession | undefined = vscode.debug.activeDebugSession;
    if (!debugSession) {
        return;
    }

    if (debugSession.configuration.noDebug) {
        vscode.window.showWarningMessage("Failed to apply the changes because hot code replace is not supported by run mode, "
            + "would you like to restart the program?", YES_BUTTON, NO_BUTTON).then((res) => {
            if (res === YES_BUTTON) {
                vscode.commands.executeCommand("workbench.action.debug.restart");
            }
        });

        return;
    }

    const autobuildConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("java.autobuild");
    if (!autobuildConfig.enabled) {
        const ans = await vscode.window.showWarningMessage(
            "The hot code replace feature requires you to enable the autobuild flag, do you want to enable it?",
            "Yes", "No");
        if (ans === "Yes") {
            await autobuildConfig.update("enabled", true);
            // Force an incremental build to avoid auto build is not finishing during HCR.
            try {
                await commands.executeJavaExtensionCommand(commands.JAVA_BUILD_WORKSPACE, false);
            } catch (err) {
                // do nothing.
            }
        }
    }

    hcrStatusBar.show("$(sync~spin)Applying code changes...");
    const start = new Date().getTime();
    const response = await debugSession.customRequest("redefineClasses");
    const elapsed = new Date().getTime() - start;
    const humanVisibleDelay = elapsed < 150 ? 150 : 0;
    if (humanVisibleDelay) {
        await new Promise((resolve) => {
            setTimeout(resolve, humanVisibleDelay);
        });
    }

    if (response && response.errorMessage) {
        // The detailed error message is handled by hotCodeReplace#handleHotCodeReplaceCustomEvent
        hcrStatusBar.clear();
        return;
    }

    if (!response || !response.changedClasses || !response.changedClasses.length) {
        hcrStatusBar.clear();
        vscode.window.showWarningMessage("Cannot find any changed classes for hot replace!");
        return;
    }

    const changed = response.changedClasses.length;
    hcrStatusBar.show("$(check)" + `${changed} changed class${changed > 1 ? "es are" : " is"} reloaded`, 5 * 1000);
}