"""Offline Eclipse Refactoring (Rename dependency update & Extract Method) verifier for Ch28 (refactoring)."""
import json
import re
VERSION = "CPython 3.12.14 offline Eclipse JDT Refactoring verifier (desktop SWT IDE absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def rename_java_type(files, old_name, new_name):
token_re = re.compile(rf"\b{re.escape(old_name)}\b")
updated = {}
affected = []
for fname, content in files.items():
# Preserve quoted strings while replacing identifier tokens
parts = re.split(r'("[^"]*")', content)
new_parts = []
changed = False
for idx, part in enumerate(parts):
if idx % 2 == 1:
new_parts.append(part)
else:
replaced = token_re.sub(new_name, part)
if replaced != part:
changed = True
new_parts.append(replaced)
new_fname = fname.replace(f"{old_name}.java", f"{new_name}.java")
if changed or new_fname != fname:
affected.append(fname)
updated[new_fname] = "".join(new_parts)
return updated, sorted(affected)
def main():
cases = []
# Case 1: Refactor entry points and preview of affected classes before clicking Continue
entries = [
"Package Explorer > Right-click > Refactor",
"Java Editor > Right-click > Refactor",
"Shift+Alt+T",
]
workspace = {
"HelloWorld.java": (
"public class HelloWorld {\n"
" public HelloWorld() {}\n"
" public String label() { return \"HelloWorld literal\"; }\n"
"}\n"
),
"AppRunner.java": (
"public class AppRunner {\n"
" public static void main(String[] args) {\n"
" HelloWorld hw = new HelloWorld();\n"
" System.out.println(hw.label());\n"
" }\n"
"}\n"
),
"Util.java": "public class Util { static int one() { return 1; } }\n",
}
refactored, affected = rename_java_type(workspace, "HelloWorld", "GreetingApp")
cases.append(
check(
"refactor_menu_entry_and_rename_preview",
"Verify Refactor triggers (including Shift+Alt+T) and preview list of impacted classes before Continue",
{
"entry_count": 3,
"shortcut": "Shift+Alt+T",
"preview_affected_files": ["AppRunner.java", "HelloWorld.java"],
},
{
"entry_count": len(entries),
"shortcut": entries[2],
"preview_affected_files": affected,
},
)
)
# Case 2: Rename propagates to class, constructor, file name, and callers while preserving string literals
cases.append(
check(
"rename_class_dependency_propagation",
"Verify Rename updates file name, class/constructor, and dependent callers without corrupting string literals",
{
"new_file_exists": True,
"old_file_removed": True,
"caller_updated": True,
"string_literal_preserved": True,
},
{
"new_file_exists": "GreetingApp.java" in refactored,
"old_file_removed": "HelloWorld.java" not in refactored,
"caller_updated": "GreetingApp hw = new GreetingApp();" in refactored["AppRunner.java"],
"string_literal_preserved": '"HelloWorld literal"' in refactored["GreetingApp.java"],
},
)
)
# Case 3: Extract Method and atomic Undo rollback
original_code = (
"int subtotal = price * qty;\n"
"int tax = subtotal * 10 / 100;\n"
"int total = subtotal + tax;\n"
)
extracted_code = (
"int total = calcTotal(price, qty);\n"
"// extracted: int calcTotal(int price, int qty)\n"
)
history = [original_code]
current = extracted_code
history.append(current)
# Undo refactoring (Alt+Shift+Z)
history.pop()
restored = history[-1]
cases.append(
check(
"extract_method_and_undo_safety",
"Verify Extract Method produces helper call and Refactor Undo restores original source cleanly",
{
"extracted_has_helper": True,
"undo_restores_exact_original": True,
},
{
"extracted_has_helper": "calcTotal(price, qty)" in extracted_code,
"undo_restores_exact_original": restored == original_code,
},
)
)
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-refactoring-lab/explore-refactoring.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-refactoring.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2284.zip