"""Offline Eclipse Navigate menu (Open Type, Type Hierarchy, Open Resource) verifier for Ch27 (navigation)."""
import fnmatch
import io
import json
import zipfile
VERSION = "CPython 3.12.14 offline Eclipse Navigate menu & wildcard/hierarchy 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: Open Type wildcard matching (* for 0+ chars, ? for 1 char)
types = [
"java.lang.String",
"java.lang.StringBuilder",
"java.util.ArrayList",
"java.util.LinkedList",
"com.example.OrderDao",
"com.example.OrderDto",
]
star_matches = sorted(t.split(".")[-1] for t in types if fnmatch.fnmatch(t.split(".")[-1], "*List"))
qmark_matches = sorted(t.split(".")[-1] for t in types if fnmatch.fnmatch(t.split(".")[-1], "OrderD?o"))
cases.append(
check(
"open_type_wildcard_matching",
"Verify Open Type supports '*' (0+ chars) and '?' (single char) wildcard matching on Java type names",
{
"star_List": ["ArrayList", "LinkedList"],
"qmark_OrderD_o": ["OrderDao", "OrderDto"],
},
{
"star_List": star_matches,
"qmark_OrderD_o": qmark_matches,
},
)
)
# Case 2: Class File Editor fallback & Attach Source from JDK src.zip
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zf.writestr("java/lang/String.java", "package java.lang;\npublic final class String {}\n")
buf.seek(0)
source_attached = False
editor_before = "ClassFileEditor(bytecode)" if not source_attached else "JavaSourceEditor"
with zipfile.ZipFile(buf, "r") as zf:
attached_source = zf.read("java/lang/String.java").decode("utf-8")
source_attached = "public final class String" in attached_source
editor_after = "JavaSourceEditor" if source_attached else "ClassFileEditor(bytecode)"
cases.append(
check(
"class_file_editor_and_attach_source",
"Verify missing source falls back to Class File Editor bytecode view and Attach Source resolves JDK src.zip",
{
"editor_before_attach": "ClassFileEditor(bytecode)",
"editor_after_attach": "JavaSourceEditor",
"resolved_declaration": True,
},
{
"editor_before_attach": editor_before,
"editor_after_attach": editor_after,
"resolved_declaration": "public final class String" in attached_source,
},
)
)
# Case 3: Open Type in Hierarchy resolves inheritance chain and selected class members
hierarchy = {
"Object": {"super": None, "fields": [], "methods": ["equals", "hashCode", "toString"]},
"AbstractList": {"super": "Object", "fields": ["modCount"], "methods": ["add", "iterator"]},
"ArrayList": {"super": "AbstractList", "fields": ["elementData", "size"], "methods": ["get", "set", "size"]},
}
chain = []
cur = "ArrayList"
while cur:
chain.append(cur)
cur = hierarchy[cur]["super"]
cases.append(
check(
"type_hierarchy_inheritance_view",
"Verify Open Type in Hierarchy displays superclass chain plus fields and methods of selected class",
{
"inheritance_chain": ["ArrayList", "AbstractList", "Object"],
"selected_fields": ["elementData", "size"],
"selected_methods": ["get", "set", "size"],
},
{
"inheritance_chain": chain,
"selected_fields": hierarchy["ArrayList"]["fields"],
"selected_methods": hierarchy["ArrayList"]["methods"],
},
)
)
# Case 4: Open Resource matches workspace files across all extensions with * and ?
resources = [
"pom.xml",
"application-dev.yml",
"application-uat.yml",
"application-prod.yml",
"logback.xml",
"Main.java",
]
xml_files = sorted(r for r in resources if fnmatch.fnmatch(r, "*.xml"))
env3_files = sorted(r for r in resources if fnmatch.fnmatch(r, "application-???.yml"))
cases.append(
check(
"open_resource_wildcard_matching",
"Verify Open Resource locates workspace files by '*' and '?' wildcard patterns",
{
"xml_files": ["logback.xml", "pom.xml"],
"three_letter_env_yml": ["application-dev.yml", "application-uat.yml"],
},
{
"xml_files": xml_files,
"three_letter_env_yml": env3_files,
},
)
)
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-navigation-lab/explore-navigation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-navigation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2280.zip