About

JVM codegen API

The Vary JVM codegen component is an experimental embedding API for tools that already have a type-checked Vary frontend model. It is not a parser, a type checker, or a replacement for the vary command. Its supported surface is the VaryCodegen facade and the request, result, diagnostic, compatibility, and artifact types used by that facade.

Installation

A release candidate is staged under one version across three coordinates. The frontend model is a transitive API dependency of codegen. The runtime is an explicit consumer dependency because generated classes call its ABI even though the generator itself does not load runtime classes.

dependencies {
    implementation("org.vary:codegen:v147-alpha.1")
    runtimeOnly("org.vary:runtime:v147-alpha.1")
}

Use the version from the candidate's release-manifest.json; do not mix versions. Release candidates live in an isolated staging repository and are not promoted as stable packages. The staged graph also contains org.vary:compiler-frontend at the same version.

Supported input contract

CodegenRequest requires a Program that has completed frontend type checking and the exact matching TypeInfo. Callers must not reuse semantic metadata from another program. The facade treats both objects as read-only. Each call creates an isolated generation session, so a VaryCodegen instance may be shared by concurrent callers.

This minimal program is also compiled by the clean external-consumer fixture:

val program = Program(emptyList(), SourceLocation("consumer.vary", 1, 1))
val request = CodegenRequest(program, TypeInfo())

when (val result = VaryCodegen().generate(request)) {
    is CodegenResult.Success -> result.artifacts.forEach { artifact ->
        check(artifact.bytes.take(4) == listOf(0xCA, 0xFE, 0xBA, 0xBE).map(Int::toByte))
    }
    is CodegenResult.Failure -> result.diagnostics.forEach { diagnostic ->
        println("${diagnostic.code}: ${diagnostic.message}")
    }
}

Generation is all-or-nothing. Success contains a non-empty, immutable artifact list and a main JVM class name. Failure contains one or more structured diagnostics and no artifacts. The facade converts ordinary backend exceptions into sanitized CG... diagnostics; fatal VM errors still propagate.

Options and lifecycle

Create a new immutable CodegenRequest for each operation. CodegenOptions selects the module name, classfile target, coverage instrumentation, API namespace behavior, project version, resource budgets, and compatibility versions. Defaults are suitable for the current toolchain, but an embedding tool should select its classfile target deliberately.

OptionContract
moduleNameSlash-separated valid JVM name; defaults to Main
classfileTargetJava 17, 21, or 25 classfile version
coverageModeDisabled unless generated coverage hooks are required
resourceLimitsBounds AST shape and generated classfile resources
frontendModelVersionMust equal CodegenFrontendCompatibility.MODEL_VERSION
runtimeAbiVersionMust equal CodegenRuntimeCompatibility.ABI_VERSION

An incompatible frontend model returns CG009. An incompatible runtime ABI returns CG008. These checks happen before generation or class loading.

Artifacts and diagnostics

Every GeneratedArtifact has a logical name, JVM internal name, kind, optional logical source identity, and defensive copy of its class bytes. The kinds cover the main module, ordinary classes, interfaces, HTTP or lifecycle resources, and synthetic helpers. Successful bytes have passed identity checks, configured budgets, collision checks, ASM dataflow verification, and deterministic sorting.

Diagnostic codes are stable enough for automation; messages are for people and may improve between compatible releases. Diagnostics can include a source location, artifact, method, descriptor, and sanitized cause. Do not parse the message to determine the failure type.

Compatibility and limits

The current candidate supports frontend model 1, runtime ABI 1, execution on JDK 25, and classfile targets 17, 21, and 25. A lower classfile target does not claim that the embedding API itself runs on that older JDK. The machine-readable policy is published as the compatibility.properties classifier and copied into the release metadata.

Codegen does not parse source, run type checking, write files, open networks, load generated classes, package application JARs, or promise source or binary compatibility outside the classified facade. Resource defaults cap AST nodes and depth, generated classes and bytes, JVM names, methods, locals, method bytes, and constant-pool entries. See codegen architecture and codegen security for the boundaries behind those limits.

← Varyonic architecture
Ecosystem →