Java to Zig Migration
Treat migration as preservation of observable contracts, not translation of
syntax or production-file counts. Java remains the compatibility oracle until
the complete agreed denominator is proven in Zig.
Compose with the Zig skill pack
This skill owns migration governance. Load the target-language skills needed by
the repository instead of embedding a second Zig manual here:
- use
$zig-0.16 for Zig 0.16 projects and $zig-0.15 only when the pinned
toolchain requires it;
- use
$zig-project-structure and $zig-build-system for modules, build.zig,
package paths, targets, and cross-compilation;
- use
$zig-testing and $zig-java-migration-testing for test implementation
and completion evidence;
- use
$zig-concurrency, $zig-json, $zig-http, or $zig-crypto only when
those contracts are in scope;
- finish with
$zig-code-review and $zig-tiger-style where compatible with
source parity.
The source-side migration contract is shared with rust-java-migration and
future kotlin-java-migration and swift-java-migration skills. Only the
target-language profile changes. Read
Cross-language migration profile
before changing denominators, dispositions, document schemas, or completion
claims.
Non-negotiable outcome
A completed migration has all of the following:
- Every in-scope Java object, member, overload, parameter, exception, comment,
test case, fixture, script, example, and externally observable behavior has a
traceable Zig disposition.
- Every required disposition has real Zig logic; declarations, imports,
generated API lists, empty bodies,
unreachable, placeholder errors, and
compile-only facades do not count.
- The complete Java test suite and the complete Zig lossless-port suite pass.
- Every concrete source case has a Java/Zig golden or live differential result
of
MATCH; no mismatch, harness failure, skip, or not-run case remains.
- Zig-specific allocator, ownership, error-set,
defer/errdefer, target,
ABI, concurrency, and build-graph risks have additional tests.
- Real-host, load, security, rollout, and rollback claims remain separate from
functional parity and require their own evidence.
Coverage is a diagnostic signal. It is never the migration denominator or proof
that Java and Zig produce the same result.
Establish baselines and scope
Record before editing:
- immutable Java SHA/tag and Zig SHA;
- JDK/build-tool versions, Zig version, target triple, optimization mode, and
build options;
- source and target roots;
- public API, serialization, protocol, error, lifecycle, concurrency, and side
effect contracts;
- source test roots and every non-standard fixture/resource/script/data root;
- generated sources, platform-only behavior, approved exclusions, and blockers;
- exact commands for Java, Zig, differential, host, and non-functional gates.
If .codegraph/ exists, use CodeGraph before text search to trace public entry
points, dynamic registries, callbacks, lifecycle edges, and test-to-production
paths. Otherwise use deterministic source inventories and compiler/build data.
Maintain four current migration documents
Keep one current set per source module:
迁移路线图.md
对象级对照表.md
语义迁移对照表.md
对象名称一致性检查.md
All four documents must carry the same Java baseline, Zig baseline, audit date,
scope, and completion state. Historical design material belongs after an
explicit historical marker; it cannot override current generated facts.
Use these states precisely:
| State |
Meaning |
Completion effect |
MISSING |
no Zig counterpart |
blocks |
MISPLACED |
counterpart exists at the wrong agreed path |
blocks |
STUB |
placeholder or non-functional body |
blocks |
PARTIAL |
some contracts are absent |
blocks |
UNVERIFIED |
implementation exists without required evidence |
blocks |
IMPLEMENTED |
structure, behavior, and required evidence complete |
handled |
DEPENDENCY_REUSED |
pinned Zig dependency plus adapter and local tests |
handled |
PLATFORM_NA |
proven JVM/platform-only contract |
outside denominator |
ZIG_EXTENSION |
target-only additive capability |
outside source denominator |
Map Java structure to Zig
Default structural rule:
- one Java class/interface/enum/record/annotation maps to one primary
.zig
file;
- nested types and builders may remain with their owning type;
- package directories become
snake_case directories using the documented
repository path algorithm;
root.zig files define module imports and public re-exports, not a warehouse
of migrated objects;
- type names use
TitleCase; functions and local values use Zig camelCase;
preserve Java member names separately in registries, serializers, protocols,
or compatibility metadata when observable;
- do not use one
compat.zig, generated registry, or re-export facade to inflate
object completion.
Preserve Java method parameters in order and meaning. Zig API naming may be
idiomatic, but the object and semantic ledgers must record the exact Java symbol
to Zig symbol mapping. Read
Java-to-Zig semantic mappings
before implementing ownership, exceptions, async behavior, generics, reflection,
annotations, serialization, or service discovery.
Implement in dependency batches
- Freeze the complete object, method, test, and asset denominator.
- Resolve architecture-wide decisions: allocator ownership, error model,
cancellation, serialization, registry/SPI, synchronization, and ABI.
- Implement the full dependency-ordered batch without alternating migration and
final acceptance object by object.
- Recompute the full inventory from current Java and Zig sources.
- Reconcile all four documents in one pass.
- Run the unified engineering and semantic verification pipeline.
Avoid permanent compatibility layers that merely preserve Java class shapes.
Use Zig-native structs, tagged unions, error unions, comptime, explicit
allocators, and modules where they preserve the same contract. Keep adapters at
real Java compatibility boundaries such as serialized names, plugin IDs,
protocol fields, or host APIs.
Dependency replacement
Do not select a Zig package because its name resembles a Java library. Record:
- exact Java responsibility and observable contract;
- candidate package URL, version/commit, license, Zig compatibility, target
support, maintenance and security evidence;
- exact upstream symbol and local call point;
- adapter behavior for errors, lifecycle, cancellation, threading, ordering,
allocation, and serialization;
- focused spike, shared conformance test, and real-host result;
- rollback or replacement strategy.
DEPENDENCY_REUSED is valid only after the exact declared dependency path is
executed through local integration tests.
Whole-project acceptance module
Every repository/product-level completion claim requires a dedicated,
non-production <project>-test/ module. Register an explicit
zig build migration-test step in build.zig; keep the module out of published
package paths and production artifacts. It owns:
- complete source-suite and immutable source-asset replay;
- public cross-module and binding/adapter workflows;
- Java/Zig golden or live differential execution;
- aggregate machine-readable evidence.
Local test blocks inside production modules prove focused implementation
behavior only. They do not replace <project>-test. The whole-project gate is
all source cases MATCH with zero failed, skipped, ignored, or not-run cases.
Unified verification
Run after the batch is frozen:
- formatting and repository-specific lint/style checks;
zig build, zig build test, explicit target/optimization matrices, and
cross-compilation checks;
- complete mirrored source tests in
<project>-test;
- complete pinned Java and Zig suites plus every-case differential comparison;
- real script/example replay;
- allocator/leak, lifecycle, concurrency, cancellation, and deterministic
scheduling tests;
- malformed input, fuzz/property, load/soak, and security tests;
- real host/binding/ABI integration;
- gray rollout and rollback rehearsal when production replacement is claimed.
Read Verification and acceptance for
evidence levels and completion reporting.
Red lines
- Do not simplify Java semantics to make Zig implementation easier.
- Do not omit overloads, parameter variants, exception behavior, side effects,
lifecycle rules, tests, resources, or comments from the denominator.
- Do not count file existence, imports, exports, build success, or API manifests
as implementation.
- Do not use
unreachable, placeholder errors, empty tests, or ignored errors as
migrated logic.
- Do not treat allocator leaks, borrowed-slice lifetime changes, error-set
collapse, target omissions, or ABI changes as internal details.
- Do not modify copied source fixtures in place; preserve byte-identical copies
and generate target-specific derivatives separately.
- Do not accept two independently green suites as parity.
- Do not use sampled differential cases, pass thresholds, coverage percentages,
or “representative” scripts for a completion claim.
- Do not hide
MISSING, PARTIAL, STUB, MISPLACED, or UNVERIFIED rows in
historical appendices or alternate documents.
Completion criteria
- The current object denominator contains no incomplete state.
- Every Java object/member/test/case/asset is traceable and losslessly handled.
- Every copied source asset has matching SHA-256.
- Java and Zig complete suites pass from pinned baselines.
- Full per-case differential output is 100%
MATCH with zero harness failure or
not-run case.
<project>-test owns the complete command and aggregate artifact.
- Zig-specific allocator, error, target, ABI, concurrency, and build obligations
pass.
- Real-host and production-readiness claims have separate evidence.
- Final reporting distinguishes structural, implementation, behavioral,
integration, and production-readiness completion.
Resources
- Cross-language migration profile
- Java-to-Zig semantic mappings
- Verification and acceptance
- Migration roadmap template
- Object mapping template
- Semantic mapping template
- Name consistency template
1---2name: zig-java-migration3description: Evidence-driven, lossless Java-to-Zig migration and incomplete-port audit. Use when migrating a Java repository, module, framework, API, tests, fixtures, examples, or runtime behavior to Zig; when checking whether a Java-to-Zig port is actually complete; or when planning AgentScope Java to AgentScope Zig parity. Requires complete source-object and contract inventory, idiomatic Zig implementation, exact source-test preservation, differential verification, and explicit production-readiness evidence.4---56# Java to Zig Migration78Treat migration as preservation of observable contracts, not translation of9syntax or production-file counts. Java remains the compatibility oracle until10the complete agreed denominator is proven in Zig.1112## Compose with the Zig skill pack1314This skill owns migration governance. Load the target-language skills needed by15the repository instead of embedding a second Zig manual here:1617- use `$zig-0.16` for Zig 0.16 projects and `$zig-0.15` only when the pinned18 toolchain requires it;19- use `$zig-project-structure` and `$zig-build-system` for modules, `build.zig`,20 package paths, targets, and cross-compilation;21- use `$zig-testing` and `$zig-java-migration-testing` for test implementation22 and completion evidence;23- use `$zig-concurrency`, `$zig-json`, `$zig-http`, or `$zig-crypto` only when24 those contracts are in scope;25- finish with `$zig-code-review` and `$zig-tiger-style` where compatible with26 source parity.2728The source-side migration contract is shared with `rust-java-migration` and29future `kotlin-java-migration` and `swift-java-migration` skills. Only the30target-language profile changes. Read31[Cross-language migration profile](references/cross-language-migration-profile.md)32before changing denominators, dispositions, document schemas, or completion33claims.3435## Non-negotiable outcome3637A completed migration has all of the following:38391. Every in-scope Java object, member, overload, parameter, exception, comment,40 test case, fixture, script, example, and externally observable behavior has a41 traceable Zig disposition.422. Every required disposition has real Zig logic; declarations, imports,43 generated API lists, empty bodies, `unreachable`, placeholder errors, and44 compile-only facades do not count.453. The complete Java test suite and the complete Zig lossless-port suite pass.464. Every concrete source case has a Java/Zig golden or live differential result47 of `MATCH`; no mismatch, harness failure, skip, or not-run case remains.485. Zig-specific allocator, ownership, error-set, `defer`/`errdefer`, target,49 ABI, concurrency, and build-graph risks have additional tests.506. Real-host, load, security, rollout, and rollback claims remain separate from51 functional parity and require their own evidence.5253Coverage is a diagnostic signal. It is never the migration denominator or proof54that Java and Zig produce the same result.5556## Establish baselines and scope5758Record before editing:5960- immutable Java SHA/tag and Zig SHA;61- JDK/build-tool versions, Zig version, target triple, optimization mode, and62 build options;63- source and target roots;64- public API, serialization, protocol, error, lifecycle, concurrency, and side65 effect contracts;66- source test roots and every non-standard fixture/resource/script/data root;67- generated sources, platform-only behavior, approved exclusions, and blockers;68- exact commands for Java, Zig, differential, host, and non-functional gates.6970If `.codegraph/` exists, use CodeGraph before text search to trace public entry71points, dynamic registries, callbacks, lifecycle edges, and test-to-production72paths. Otherwise use deterministic source inventories and compiler/build data.7374## Maintain four current migration documents7576Keep one current set per source module:77781. `迁移路线图.md`792. `对象级对照表.md`803. `语义迁移对照表.md`814. `对象名称一致性检查.md`8283All four documents must carry the same Java baseline, Zig baseline, audit date,84scope, and completion state. Historical design material belongs after an85explicit historical marker; it cannot override current generated facts.8687Use these states precisely:8889| State | Meaning | Completion effect |90|---|---|---|91| `MISSING` | no Zig counterpart | blocks |92| `MISPLACED` | counterpart exists at the wrong agreed path | blocks |93| `STUB` | placeholder or non-functional body | blocks |94| `PARTIAL` | some contracts are absent | blocks |95| `UNVERIFIED` | implementation exists without required evidence | blocks |96| `IMPLEMENTED` | structure, behavior, and required evidence complete | handled |97| `DEPENDENCY_REUSED` | pinned Zig dependency plus adapter and local tests | handled |98| `PLATFORM_NA` | proven JVM/platform-only contract | outside denominator |99| `ZIG_EXTENSION` | target-only additive capability | outside source denominator |100101## Map Java structure to Zig102103Default structural rule:104105- one Java class/interface/enum/record/annotation maps to one primary `.zig`106 file;107- nested types and builders may remain with their owning type;108- package directories become `snake_case` directories using the documented109 repository path algorithm;110- `root.zig` files define module imports and public re-exports, not a warehouse111 of migrated objects;112- type names use `TitleCase`; functions and local values use Zig `camelCase`;113 preserve Java member names separately in registries, serializers, protocols,114 or compatibility metadata when observable;115- do not use one `compat.zig`, generated registry, or re-export facade to inflate116 object completion.117118Preserve Java method parameters in order and meaning. Zig API naming may be119idiomatic, but the object and semantic ledgers must record the exact Java symbol120to Zig symbol mapping. Read121[Java-to-Zig semantic mappings](references/java-to-zig-semantic-mappings.md)122before implementing ownership, exceptions, async behavior, generics, reflection,123annotations, serialization, or service discovery.124125## Implement in dependency batches1261271. Freeze the complete object, method, test, and asset denominator.1282. Resolve architecture-wide decisions: allocator ownership, error model,129 cancellation, serialization, registry/SPI, synchronization, and ABI.1303. Implement the full dependency-ordered batch without alternating migration and131 final acceptance object by object.1324. Recompute the full inventory from current Java and Zig sources.1335. Reconcile all four documents in one pass.1346. Run the unified engineering and semantic verification pipeline.135136Avoid permanent compatibility layers that merely preserve Java class shapes.137Use Zig-native structs, tagged unions, error unions, comptime, explicit138allocators, and modules where they preserve the same contract. Keep adapters at139real Java compatibility boundaries such as serialized names, plugin IDs,140protocol fields, or host APIs.141142## Dependency replacement143144Do not select a Zig package because its name resembles a Java library. Record:145146- exact Java responsibility and observable contract;147- candidate package URL, version/commit, license, Zig compatibility, target148 support, maintenance and security evidence;149- exact upstream symbol and local call point;150- adapter behavior for errors, lifecycle, cancellation, threading, ordering,151 allocation, and serialization;152- focused spike, shared conformance test, and real-host result;153- rollback or replacement strategy.154155`DEPENDENCY_REUSED` is valid only after the exact declared dependency path is156executed through local integration tests.157158## Whole-project acceptance module159160Every repository/product-level completion claim requires a dedicated,161non-production `<project>-test/` module. Register an explicit162`zig build migration-test` step in `build.zig`; keep the module out of published163package paths and production artifacts. It owns:164165- complete source-suite and immutable source-asset replay;166- public cross-module and binding/adapter workflows;167- Java/Zig golden or live differential execution;168- aggregate machine-readable evidence.169170Local `test` blocks inside production modules prove focused implementation171behavior only. They do not replace `<project>-test`. The whole-project gate is172all source cases `MATCH` with zero failed, skipped, ignored, or not-run cases.173174## Unified verification175176Run after the batch is frozen:1771781. formatting and repository-specific lint/style checks;1792. `zig build`, `zig build test`, explicit target/optimization matrices, and180 cross-compilation checks;1813. complete mirrored source tests in `<project>-test`;1824. complete pinned Java and Zig suites plus every-case differential comparison;1835. real script/example replay;1846. allocator/leak, lifecycle, concurrency, cancellation, and deterministic185 scheduling tests;1867. malformed input, fuzz/property, load/soak, and security tests;1878. real host/binding/ABI integration;1889. gray rollout and rollback rehearsal when production replacement is claimed.189190Read [Verification and acceptance](references/verification-and-acceptance.md) for191evidence levels and completion reporting.192193## Red lines194195- Do not simplify Java semantics to make Zig implementation easier.196- Do not omit overloads, parameter variants, exception behavior, side effects,197 lifecycle rules, tests, resources, or comments from the denominator.198- Do not count file existence, imports, exports, build success, or API manifests199 as implementation.200- Do not use `unreachable`, placeholder errors, empty tests, or ignored errors as201 migrated logic.202- Do not treat allocator leaks, borrowed-slice lifetime changes, error-set203 collapse, target omissions, or ABI changes as internal details.204- Do not modify copied source fixtures in place; preserve byte-identical copies205 and generate target-specific derivatives separately.206- Do not accept two independently green suites as parity.207- Do not use sampled differential cases, pass thresholds, coverage percentages,208 or “representative” scripts for a completion claim.209- Do not hide `MISSING`, `PARTIAL`, `STUB`, `MISPLACED`, or `UNVERIFIED` rows in210 historical appendices or alternate documents.211212## Completion criteria213214- The current object denominator contains no incomplete state.215- Every Java object/member/test/case/asset is traceable and losslessly handled.216- Every copied source asset has matching SHA-256.217- Java and Zig complete suites pass from pinned baselines.218- Full per-case differential output is 100% `MATCH` with zero harness failure or219 not-run case.220- `<project>-test` owns the complete command and aggregate artifact.221- Zig-specific allocator, error, target, ABI, concurrency, and build obligations222 pass.223- Real-host and production-readiness claims have separate evidence.224- Final reporting distinguishes structural, implementation, behavioral,225 integration, and production-readiness completion.226227## Resources228229- [Cross-language migration profile](references/cross-language-migration-profile.md)230- [Java-to-Zig semantic mappings](references/java-to-zig-semantic-mappings.md)231- [Verification and acceptance](references/verification-and-acceptance.md)232- [Migration roadmap template](assets/迁移路线图.md)233- [Object mapping template](assets/对象级对照表.md)234- [Semantic mapping template](assets/语义迁移对照表.md)235- [Name consistency template](assets/对象名称一致性检查.md)