"""Offline Kotlin 2.x semantic verifier for Chapter 03 (environment)."""
import json
import re
import sys
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Kotlin 2.x semantic verifier (kotlinc absent)"
def check_jdk_compat(jdk_major: int) -> bool:
"""Kotlin 2.2 requires JDK 8+ (JDK 1.6 is rejected)."""
return jdk_major >= 8
def kotlin_file_to_jvm_facade(package: str, filename: str) -> str:
stem = filename[:-3] if filename.endswith(".kt") else filename
facade = stem[0].upper() + stem[1:] + "Kt"
return f"{package}.{facade}" if package else facade
def run_cases() -> list[dict]:
cases: list[dict] = []
# Case 1: JDK 8+ requirement and playground/IDEA minimal program
site_name = "蓝色天空网"
url = "www.mbsky.com"
actual_1 = {
"jdk6_supported": check_jdk_compat(6),
"jdk8_supported": check_jdk_compat(8),
"jdk17_supported": check_jdk_compat(17),
"jdk21_supported": check_jdk_compat(21),
"playground_lines": [f"Hello, {site_name}!", f"网址:{url}"],
"idea_line": f"项目创建成功,目标站点:{url}",
}
expected_1 = {
"jdk6_supported": False,
"jdk8_supported": True,
"jdk17_supported": True,
"jdk21_supported": True,
"playground_lines": ["Hello, 蓝色天空网!", "网址:www.mbsky.com"],
"idea_line": "项目创建成功,目标站点:www.mbsky.com",
}
cases.append({
"name": "jdk_version_and_playground_idea_model",
"summary": "Verify Kotlin 2.2 JDK 8+ compatibility and Playground/IDEA startup outputs.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Gradle Kotlin DSL (build.gradle.kts + settings.gradle.kts + MainKt facade)
build_kts = (
'plugins {\n'
' kotlin("jvm") version "2.2.0"\n'
' application\n'
'}\n'
'group = "com.mbsky"\n'
'version = "1.0.0"\n'
'repositories { mavenCentral() }\n'
'dependencies { testImplementation(kotlin("test")) }\n'
'application { mainClass.set("com.mbsky.MainKt") }\n'
'kotlin { jvmToolchain(17) }\n'
)
settings_kts = 'rootProject.name = "kotlin-demo"\n'
plugin_ver = re.search(r'kotlin\("jvm"\)\s+version\s+"([^"]+)"', build_kts)
main_cls = re.search(r'mainClass\.set\("([^"]+)"\)', build_kts)
toolchain = re.search(r'jvmToolchain\((\d+)\)', build_kts)
root_name = re.search(r'rootProject\.name\s*=\s*"([^"]+)"', settings_kts)
computed_facade = kotlin_file_to_jvm_facade("com.mbsky", "Main.kt")
actual_2 = {
"kotlin_version": plugin_ver.group(1) if plugin_ver else None,
"main_class": main_cls.group(1) if main_cls else None,
"computed_facade_matches": computed_facade == (main_cls.group(1) if main_cls else ""),
"jvm_toolchain": int(toolchain.group(1)) if toolchain else None,
"root_project_name": root_name.group(1) if root_name else None,
}
expected_2 = {
"kotlin_version": "2.2.0",
"main_class": "com.mbsky.MainKt",
"computed_facade_matches": True,
"jvm_toolchain": 17,
"root_project_name": "kotlin-demo",
}
cases.append({
"name": "gradle_kotlin_dsl_and_layout",
"summary": "Parse Gradle build.gradle.kts and settings.gradle.kts and verify Main.kt -> com.mbsky.MainKt facade mapping.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Maven pom.xml parsing via xml.etree.ElementTree
pom_xml = """<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mbsky</groupId>
<artifactId>kotlin-demo</artifactId>
<version>1.0.0</version>
<properties>
<kotlin.version>2.2.0</kotlin.version>
<maven.compiler.release>8</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
</plugin>
</plugins>
</build>
</project>"""
ns = {"m": "http://maven.apache.org/POM/4.0.0"}
root = ET.fromstring(pom_xml)
actual_3 = {
"kotlin_version": root.findtext("m:properties/m:kotlin.version", namespaces=ns),
"compiler_release": root.findtext("m:properties/m:maven.compiler.release", namespaces=ns),
"encoding": root.findtext("m:properties/m:project.build.sourceEncoding", namespaces=ns),
"source_dir": root.findtext("m:build/m:sourceDirectory", namespaces=ns),
"stdlib_artifact": root.findtext("m:dependencies/m:dependency/m:artifactId", namespaces=ns),
"plugin_artifact": root.findtext("m:build/m:plugins/m:plugin/m:artifactId", namespaces=ns),
}
expected_3 = {
"kotlin_version": "2.2.0",
"compiler_release": "8",
"encoding": "UTF-8",
"source_dir": "src/main/kotlin",
"stdlib_artifact": "kotlin-stdlib",
"plugin_artifact": "kotlin-maven-plugin",
}
cases.append({
"name": "maven_pom_xml_verification",
"summary": "Parse Maven pom.xml with stdlib ElementTree and check kotlin-stdlib, kotlin-maven-plugin, and UTF-8 settings.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Android Studio Kotlin DSL plugins block vs legacy Groovy apply plugin
root_android_kts = (
'plugins {\n'
' id("com.android.application") version "8.7.0" apply false\n'
' id("org.jetbrains.kotlin.android") version "2.2.0" apply false\n'
'}\n'
)
app_android_kts = (
'plugins {\n'
' id("com.android.application")\n'
' id("org.jetbrains.kotlin.android")\n'
'}\n'
'android {\n'
' namespace = "com.mbsky.demo"\n'
' compileSdk = 35\n'
' defaultConfig { minSdk = 21; targetSdk = 35; versionCode = 1; versionName = "1.0" }\n'
'}\n'
)
actual_4 = {
"root_apply_false_count": root_android_kts.count("apply false"),
"app_uses_plugins_dsl": "plugins {" in app_android_kts and "apply plugin:" not in app_android_kts,
"compile_sdk": int(re.search(r"compileSdk\s*=\s*(\d+)", app_android_kts).group(1)),
"min_sdk": int(re.search(r"minSdk\s*=\s*(\d+)", app_android_kts).group(1)),
"eclipse_recommended": False,
}
expected_4 = {
"root_apply_false_count": 2,
"app_uses_plugins_dsl": True,
"compile_sdk": 35,
"min_sdk": 21,
"eclipse_recommended": False,
}
cases.append({
"name": "android_studio_dsl_and_eclipse_deprecation",
"summary": "Verify Android Kotlin DSL plugins block, SDK constraints, and Eclipse plugin deprecation status.",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
return cases
def main() -> None:
payload = {"version": VERSION, "cases": run_cases()}
sys.stdout.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。
入口文件:projects/kotlin-environment-lab/explore-environment.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-environment.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Kotlin 2.x 语义验证器(本地未安装 kotlinc/JVM Kotlin 工具链)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联43章) · 含当前章节或实例 · mbskycom202612a2132.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2144.zip