蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse 实时语法诊断、Quick Fix 生成缺失方法与导入/异常修复离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-quick-fix.py
explore-quick-fix.py104 行
"""Offline Eclipse Quick Fix diagnostics & automated code patch verifier for Ch24 (quick-fix)."""
import json
import re

VERSION = "CPython 3.12.14 offline Eclipse Quick Fix diagnostic & patch verifier (desktop SWT IDE absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    # Case 1: Syntax & semantic diagnostic markers (red squiggle error vs yellow squiggle warning)
    person_src = "public class Person {\n    private String name;\n    public String getName() { return name; }\n}\n"
    caller_src = (
        "public class Demo {\n"
        "    void run(Person p) {\n"
        "        int unusedFlag = 1;\n"
        "        String id = p.getId();\n"
        "    }\n"
        "}\n"
    )
    defined_methods = set(re.findall(r"public\s+\w+\s+(\w+)\s*\(", person_src))
    called_methods = re.findall(r"p\.(\w+)\s*\(", caller_src)
    diagnostics = []
    for m in called_methods:
        if m not in defined_methods:
            diagnostics.append({"severity": "ERROR", "underline": "red", "ruler": "lightbulb_error", "symbol": m})
    if "unusedFlag" in caller_src and caller_src.count("unusedFlag") == 1:
        diagnostics.append({"severity": "WARNING", "underline": "yellow", "ruler": "lightbulb_warning", "symbol": "unusedFlag"})

    cases.append(
        check(
            "syntax_diagnostic_markers",
            "Verify Java editor distinguishes red-squiggle compile errors from yellow-squiggle warnings with ruler lightbulbs",
            [
                {"severity": "ERROR", "underline": "red", "ruler": "lightbulb_error", "symbol": "getId"},
                {"severity": "WARNING", "underline": "yellow", "ruler": "lightbulb_warning", "symbol": "unusedFlag"},
            ],
            diagnostics,
        )
    )

    # Case 2: Quick Fix (Ctrl+1 / Edit > Quick Fix / Problems view) creates missing Person.getId()
    proposal_title = "Create method 'getId()' in type 'Person'"
    patched_person = person_src.rstrip()
    assert patched_person.endswith("}")
    patched_person = (
        patched_person[:-1]
        + "    public String getId() {\n        return null;\n    }\n}\n"
    )
    defined_after = sorted(re.findall(r"public\s+\w+\s+(\w+)\s*\(", patched_person))
    triggers = ["hover_squiggle", "click_lightbulb", "Edit > Quick Fix", "Ctrl+1", "Problems view > Quick Fix"]

    cases.append(
        check(
            "quick_fix_create_method_proposal",
            "Verify Quick Fix generates and applies 'Create method getId() in type Person' across all trigger paths",
            {
                "proposal": "Create method 'getId()' in type 'Person'",
                "defined_methods_after": ["getId", "getName"],
                "trigger_count": 5,
            },
            {
                "proposal": proposal_title,
                "defined_methods_after": defined_after,
                "trigger_count": len(triggers),
            },
        )
    )

    # Case 3: Additional standard Quick Fix suggestions (missing import & surround with try/catch)
    import_fix = "Import 'List' (java.util)"
    try_catch_fix = "Surround with try/catch"
    snippet = "List<String> items = Files.readAllLines(path);"
    fixes = []
    if "List<" in snippet:
        fixes.append(import_fix)
    if "Files.readAllLines" in snippet:
        fixes.append(try_catch_fix)

    cases.append(
        check(
            "quick_fix_import_and_try_catch",
            "Verify Quick Fix proposes missing import resolution and try/catch wrapping for checked exceptions",
            ["Import 'List' (java.util)", "Surround with try/catch"],
            fixes,
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/eclipse-quick-fix-lab/explore-quick-fix.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-quick-fix.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2283.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习