kotlin-tooling-java-to-kotlin

作者: kotlin

使用严谨的四步转换方法论将Java源文件转换为地道的Kotlin代码,每一步检查五个不变条件。支持框架感知转换,处理注解目标位置、库惯用语法和API保留。

npx skills add https://github.com/kotlin/kotlin-agent-skills --skill kotlin-tooling-java-to-kotlin

Java to Kotlin Conversion

Convert Java source files to idiomatic Kotlin using a disciplined 4-step conversion methodology with 5 invariants checked at each step. Supports framework-aware conversion that handles annotation site targets, library idioms, and API preservation.

Workflow

digraph j2k_workflow {
  rankdir=TB;
  "User specifies files" -> "Step 0: Scan & Detect";
  "Step 0: Scan & Detect" -> "Load framework guides";
  "Load framework guides" -> "Step 1: Convert";
  "Step 1: Convert" -> "Step 2: Write .kt";
  "Step 2: Write .kt" -> "Step 3: Git rename";
  "Step 3: Git rename" -> "Step 4: Verify";
  "Step 4: Verify" -> "Next file?" [label="pass"];
  "Step 4: Verify" -> "Fix issues" [label="fail"];
  "Fix issues" -> "Step 1: Convert";
  "Next file?" -> "Step 0: Scan & Detect" [label="batch: yes"];
  "Next file?" -> "Done" [label="no more files"];
}

Step 0: Scan & Detect Frameworks

Before converting, scan the Java file's import statements to detect which frameworks are in use. Load ONLY the matching framework reference files to keep context focused.

Framework Detection Table

Import prefixFramework guide
org.springframework.*SPRING.md
lombok.*LOMBOK.md
javax.persistence.*, jakarta.persistence.*, org.hibernate.*HIBERNATE.md
com.fasterxml.jackson.*JACKSON.md
io.micronaut.*MICRONAUT.md
io.quarkus.*, javax.enterprise.*, jakarta.enterprise.*QUARKUS.md
dagger.*, dagger.hilt.*DAGGER-HILT.md
io.reactivex.*, rx.*RXJAVA.md
org.junit.*, org.testng.*JUNIT.md
com.google.inject.*GUICE.md
retrofit2.*, okhttp3.*RETROFIT.md
org.mockito.*MOCKITO.md

If javax.inject.* is detected, check for Dagger/Hilt vs Guice by looking for other imports from those frameworks. If ambiguous, load both guides.

Step 1: Convert

Apply the conversion methodology from CONVERSION-METHODOLOGY.md.

This is a 4-step chain-of-thought process:

  1. Faithful 1:1 translation — exact semantics preserved
  2. Nullability & mutability audit — val/var, nullable types
  3. Collection type conversion — Java mutable → Kotlin types
  4. Idiomatic transformations — properties, string templates, lambdas

Five invariants are checked after each step. If any invariant is violated, revert to the previous step and redo.

Apply any loaded framework-specific guidance during step 4 (idiomatic transformations).

Step 2: Write Output

Write the converted Kotlin code to a .kt file with the same name as the original Java file, in the same directory.

Step 3: Preserve Git History

To preserve git blame history, use a two-phase approach:

# Phase 1: Rename (creates rename tracking)
git mv src/main/java/com/example/Foo.java src/main/kotlin/com/example/Foo.kt
git commit -m "Rename Foo.java to Foo.kt"

# Phase 2: Replace content (tracked as modification, not new file)
# Write the converted Kotlin content to Foo.kt
git commit -m "Convert Foo from Java to Kotlin"

If the project keeps Java and Kotlin in the same source root (e.g., src/main/java/), rename in place:

git mv src/main/java/com/example/Foo.java src/main/java/com/example/Foo.kt

If the project does not use Git, simply write the .kt file and delete the .java file.

Step 4: Verify

After conversion, verify using checklist.md:

  • Attempt to compile the converted file
  • Run existing tests
  • Check annotation site targets
  • Confirm no behavioral changes

Batch Conversion

When converting multiple files (a directory or package):

  1. List all .java files in the target scope
  2. Sort by dependency order — convert leaf dependencies first (files that don't import other files in the conversion set), then work up to files that depend on them
  3. Convert one file at a time — apply the full workflow (steps 0-4) for each
  4. Track progress — report which files are done, which remain
  5. Handle cross-references — after converting a file, update imports in other Java files if needed (e.g., if a class moved packages)

For large batches, consider converting in packages (bottom-up from leaf packages).

Common Pitfalls

See KNOWN-ISSUES.md for:

  • Kotlin keyword conflicts (when, in, is, object)
  • SAM conversion ambiguity
  • Platform types from Java interop
  • @JvmStatic / @JvmField / @JvmOverloads usage
  • Checked exceptions and @Throws
  • Wildcard generics → Kotlin variance

来自 kotlin 的更多技能

ci-cd-containerization-advisor
kotlin
为Kotlin加Spring应用设计可重现的构建、镜像和部署流水线,包括CI验证、分层容器、发布安全性等。
official
configuration-properties-profiles-kotlin-safe
kotlin
Design and diagnose Spring configuration, profiles, and `@ConfigurationProperties` binding for Kotlin applications. Use when property binding fails,…
official
dependency-conflict-resolver
kotlin
Diagnose and resolve Gradle and Spring classpath conflicts, version drift, and binary incompatibilities in Kotlin applications. Use when `NoSuchMethodError`,…
official
domain-decomposition-api-design-advisor
kotlin
在实施开始前,将业务范围分解为限界上下文、模块或服务边界、工作流以及API契约。适用于构建新的…
official
error-model-validation-architect
kotlin
为Kotlin加Spring服务设计和实现一致的API验证与错误处理行为。在定义错误负载、映射框架时使用…
official
gradle-kotlin-dsl-doctor
kotlin
Generate, debug, and repair Kotlin + Spring Gradle builds with minimal, compatible changes. Use when `build.gradle.kts` or `settings.gradle.kts` is failing,…
official
integration-resilience-engineer
kotlin
为Kotlin加Spring服务设计具有弹性能力的HTTP、消息传递和定时集成,包含明确的超时预算、重试机制、幂等性、熔断…
official
jackson-kotlin-serialization-specialist
kotlin
诊断并设计Kotlin与Jackson在Spring应用中的JSON序列化和反序列化行为。当DTO无法反序列化、默认…
official