1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## What this is
kotlin-proxy replaces the Kotlin compiler inside an AGP/Gradle build with an
ABI-identical **stub** of `org.jetbrains.kotlin.cli.jvm.K2JVMCompiler` that lives
on the **Kotlin daemon's** classpath. The stub logs every call (and the
compiler arguments it receives) and returns JVM defaults / reflectively-built
real return objects — it does **not** actually compile. This is **pure
replacement**: no passthrough, no fallback. The build is *supposed* to go red;
the point is to observe exactly what AGP/KGP hands the compiler before
implementing an alternative compile inside the stub.
`README.md` is the deep reference (bytecode format, log format, full option
table). Read it before changing generation behavior.
## Layout
- `tools/gen_class.py` — self-contained Python stub generator (the core). Emits
`.class` files **directly** (not Java source) by copying the real class's
erased structure verbatim.
- `stubs/src/kotlin/proxy/KotlinProxyLog.java` — the one *real* class in the jar;
every generated stub calls into it.
- `jars/defs.txt`, `jars/scan.txt`, `jars/fallback.txt` — jar list files feeding
the generator (one jar path per line, `#` = comment).
- `dist/kotlin-proxy-compiler-stubs.jar` — the build artifact (gitignored).
- `test_project/` — a minimal AGP app (AGP 9.4.1, Gradle 9.8.1, single file
`Hello.kt`) that consumes the jar to prove the stub runs in the daemon.
## Building (3 steps, from repo root; JDK 11+ and Python 3)
```bash
# 1. Generate the stub classes (WIPES --out first — put nothing you need there)
python3 tools/gen_class.py \
--scan-jars jars/scan.txt --def-jars jars/defs.txt --fallback-jars jars/fallback.txt \
--seed-only --no-closure \
--seed 'org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt' \
--out stubs/classes_min
# 2. Compile the logger into the SAME tree (MUST run after step 1, which wiped it)
javac -nowarn -proc:none -d stubs/classes_min stubs/src/kotlin/proxy/KotlinProxyLog.java
# 3. Package
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
The result is 4 class files + manifest: the three seeded stubs plus
`kotlin/proxy/KotlinProxyLog`. The seed trio are **the only** classes stubbed
(the "trio" mode); their superclasses/interfaces (`CLICompiler`,
`K2JVMCompilerArguments`, …) are *not* stubbed and resolve from the real jars on
the daemon classpath.
## Testing / verifying (there is no unit-test suite)
The "test" is the test project. From `test_project/`:
```bash
./gradlew :app:compileDebugKotlin
```
Expected outcome: **red** (the stub returns defaults, so the real `CLICompiler`
machinery NPEs after calling into the stub). That is the intended state. To
confirm the stub actually ran *inside the out-of-process daemon*, read the marker
file `/tmp/kotlin-proxy-marker.txt` — a daemon run shows thread names
`RMI TCP Connection(N)` and `LOAD …/dist/kotlin-proxy-compiler-stubs.jar` lines.
The marker path is fixed regardless of the daemon's working dir or missing
`-Dkotlin.proxy.log`, so it is the reliable proof.
Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class`.
## Architecture notes (why it works)
- **Bytecode, not source:** emitting Java and fighting `javac`'s generics checker
is infeasible (erasure bridges the JVM needs for linkage but `javac` rejects).
So `gen_class.py` copies the real class's erased structure — exact
super/interfaces, every field, every method (incl. synthetic `$default` and
erasure bridges) with its exact name + descriptor + access flags — and omits
the `Signature` attribute entirely (no generics, no clashes). The JVM links by
name + erased descriptor, both copied verbatim, so the stub is ABI-identical.
- **Straight-line bodies only:** method bodies have no branches, so no
`StackMapTable` frames are needed. `<clinit>` calls `KotlinProxyLog.loaded`;
constructors call `super()` (enums call `Enum.<init>("stub",0)`); concrete
methods log and then return a default — but reference-class returns
(except `String`) materialize a *real* instance via `newInstance` so the daemon
can proceed (e.g. `createArguments()` yields a real `K2JVMCompilerArguments`).
Param-less erasure bridges forward to their concrete sibling instead of
logging+null (the daemon dispatches through bridges).
- **Classpath shadowing (the subtle part):** KGP seeds the daemon classpath from
the `kotlinCompilerClasspath` configuration via hidden `defaultDependencies`.
Adding *any* regular dependency to that configuration suppresses those
defaults. So `test_project/app/build.gradle.kts` re-declares the real compiler
modules as regular deps **with the stub jar first**, making the daemon load
`K2JVMCompiler` from the stub. The root `build.gradle.kts` also puts the stub
first on the *buildscript* classpath. Both are intentional.
## Gotchas
- **Machine-specific absolute paths.** `jars/defs.txt`, `jars/scan.txt`,
`jars/fallback.txt` point into this machine's Gradle cache
(`/home/the_jk/.gradle/caches/…`), and `test_project/app/build.gradle.kts:51`
hardcodes `/home/the_jk/source/kotlin_proxy/dist/…`. On another machine or a
fresh checkout you must regenerate the jar lists and fix that path, or the
build will find no jars / no stub.
- **Version coupling.** The Kotlin version is pinned in three places and must
all match: `jars/defs.txt` (the 2.2.10 set), `jars/fallback.txt` (newer KGP
variants, def-lookup only), and the `realCompilerJars` list in
`test_project/app/build.gradle.kts`. Bumping the version means updating all
three (and re-seeding the daemon classpath list).
- **`--seed-only` + `--no-closure`** produces exactly the seed set (the daemon
jar). The default mode is a full transitive closure from the AGP scan jars +
KGP plugin entry points — a much larger jar; only use it when you actually
want to stub a broader surface.
- **Build artifacts are gitignored:** `dist/` (whole dir) and
`stubs/classes_min/` are not tracked. If you need the jar and it's missing,
run the 3 build steps above.
|