Eclipse 快速修复
讲解 Eclipse Java 编辑器的实时语法诊断标识(红/黄波浪线、Problems 视图、垂直标尺小灯泡)及 Quick Fix(Ctrl + 1)的多种唤起路径与自动修复动作。
学完这一章,你能够
- 讲解 Eclipse Java 编辑器的实时语法诊断标识(红/黄波浪线、Problems 视图、垂直标尺小灯泡)及 Quick Fix(Ctrl + 1)的多种唤起路径与自动修复动作。
实时语法诊断与错误/警告标识
在 Eclipse Java 编辑器中键入代码时,后台编译器会依据 Java 语法与类型规则持续执行实时检查。一旦发现编译错误或潜在警告,编辑器会通过四种视觉位置同步提醒开发者:用红色波浪线标出语法或类型错误;用黄色波浪线标出代码警告(如未使用变量);在 `Problems` 视图中集中列出全部错误与警告条目;并在编辑器左侧垂直标尺上显示黄色小灯泡及对应的错误/警告标记。
唤起快速修复(Quick Fix)并应用建议
针对检测出的问题,快速修复(Quick Fix)对话框会给出一条或多条可执行的代码修正方案。唤起该对话框的常用方式包括:将鼠标悬停在波浪线标记的代码上;直接单击左侧垂直标尺上的小灯泡图标;或者将光标停留在高亮代码处后选择 `Edit > Quick Fix` 菜单项,亦可直接按下高频快捷键 `Ctrl + 1`。
例如当调用 `person.getId()` 但 `Person` 类尚未声明该方法时,`getId` 会被标红高亮;唤起快速修复后只需选择 `Create method 'getId()' in type 'Person'`,Eclipse 便会自动在 `Person` 类中补齐方法骨架。此外,也可以在 `Problems` 视图中右键点击具体错误项并选择 `Quick Fix` 来批量或定向应用修复方案。本站离线实验脚本已对错误/警告诊断、`Person.getId()` 自动补全及导入/`try-catch` 修复建议完成校验。
运行 Eclipse 实时语法诊断、Quick Fix 生成缺失方法与导入/异常修复离线核验
"""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()
- 验证红/黄波浪线与标尺小灯泡诊断分级、`Create method 'getId()' in type 'Person'` 源码补丁生成,以及缺失导入和 `try/catch` 包裹建议。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:236d8808c80f069167bfad2f15c82aae173c69e70c2efd798547bb5375a11626
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:16e51e1feb8efbdb506da387c376e8800a2ac69a8cad24f612fd1f606efb38fc
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
列出在 Eclipse 中触发快速修复(Quick Fix)的三种常见方式,并说明红色波浪线与黄色波浪线的区别。
展开参考解答
三种方式包括:按 `Ctrl + 1`(或 `Edit > Quick Fix`)、点击左侧垂直标尺小灯泡、鼠标悬停于波浪线处(或在 `Problems` 视图右键选择 `Quick Fix`);红色波浪线表示阻断编译的错误,黄色波浪线表示代码警告。