summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md81
1 files changed, 68 insertions, 13 deletions
diff --git a/README.md b/README.md
index ff88a88..ed6dc0d 100644
--- a/README.md
+++ b/README.md
@@ -11,8 +11,9 @@ compile inside the stub. **Pure replacement: no passthrough, no fallback.**
kotlin-proxy/
├── tools/gen_class.py # bytecode stub generator (self-contained)
├── stubs/
-│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
-│ └── classes_min/ # generated .class files (build output)
+│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
+│ ├── src/kotlin/proxy/KotlinProxyOverrides.java # per-method custom return values
+│ └── classes_min/ # generated .class files (build output)
├── jars/
│ ├── defs.txt # 28 kotlin-2.2.10 jars (definition lookup)
│ ├── scan.txt # 6 AGP-9.4.1 jars (which kotlin classes AGP touches)
@@ -48,14 +49,21 @@ This emits exactly the three seeded classes into `stubs/classes_min/`:
`gen_class.py` **wipes `--out` first** and regenerates everything, so the
output directory must not contain anything you need.
-### 2. Compile the logger into the same tree
+### 2. Compile the real classes into the same tree
```bash
-javac -nowarn -proc:none -d stubs/classes_min stubs/src/kotlin/proxy/KotlinProxyLog.java
+javac -nowarn -proc:none -d stubs/classes_min \
+ -cp "$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':')" \
+ stubs/src/kotlin/proxy/KotlinProxyLog.java \
+ stubs/src/kotlin/proxy/KotlinProxyOverrides.java
```
-`KotlinProxyLog` is the one real class in the jar; every generated stub calls
-into it. (Step 1 wiped the directory, so `javac` must run **after** it.)
+`KotlinProxyLog` (the logger every stub calls) and `KotlinProxyOverrides`
+(the per-method return-value hook you implement against) are the only real
+classes in the jar. The `-cp` puts the real compiler jars on javac's
+classpath so your overrides may import compiler types (`CommonCompilerArguments`,
+`Services`, …). (Step 1 wiped the directory, so `javac` must run
+**after** it.)
### 3. Package the jar
@@ -63,12 +71,13 @@ into it. (Step 1 wiped the directory, so `javac` must run **after** it.)
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-Result: 4 class files + manifest
+Result: 5 class files + manifest
```
$ jar tf dist/kotlin-proxy-compiler-stubs.jar | grep -v '/$'
META-INF/MANIFEST.MF
kotlin/proxy/KotlinProxyLog.class
+kotlin/proxy/KotlinProxyOverrides.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt.class
@@ -141,12 +150,18 @@ The list files contain one jar path per line; `#` starts a comment.
- **Concrete methods:** call
`KotlinProxyLog.call(member, dotName)` — or the 3-arg overload passing the
**first reference parameter**, so the log shows the boundary argument
- (typically `K2JVMCompilerArguments`), then produce the return value:
- - reference-class returns (except `String`): `KotlinProxyLog.newInstance(retType)`
- + `checkcast` — reflectively instantiates the real (un-stubbed) return
- class so the daemon can proceed (e.g. `createArguments()` yields a real
- `K2JVMCompilerArguments`). Types without a usable no-arg ctor (interfaces,
- enums, abstract) yield `null`, as before.
+ (typically `K2JVMCompilerArguments`) — then produce the return value:
+ - reference-class returns (except `String`): build an `Object[]` of the
+ call's arguments (references as-is, primitives boxed — so position `i`
+ is parameter `i`; `null` for a param-less method), then ask
+ `KotlinProxyLog.returnValue(member, owner, retType, args)` + `checkcast`.
+ That first dispatches to `KotlinProxyOverrides.invoke` — your per-method
+ custom implementation, see below — and falls back to
+ `KotlinProxyLog.newInstance(retType)`, which reflectively instantiates
+ the real (un-stubbed) return class so the daemon can proceed (e.g.
+ `createArguments()` yields a real `K2JVMCompilerArguments`). Types
+ without a usable no-arg ctor (interfaces, enums, abstract) yield
+ `null`, as before.
- everything else: the JVM default (`0`, `null`, …).
- **Param-less erasure bridges:** forward to their concrete sibling (same name,
narrower return type) instead of logging + null — the daemon dispatches
@@ -155,6 +170,43 @@ The list files contain one jar path per line; `#` starts a comment.
real one: call the concrete method.
- **Abstract methods / interface methods:** no body.
+## Implementing a stub method (custom return values)
+
+`KotlinProxyOverrides` (a real class in the jar, `stubs/src/kotlin/proxy/`) is
+the extension point: every stub method with a reference-class return type
+(other than `String`) asks it for its return value **before** falling back to
+the default. Implement exactly the methods you want the stub to actually do
+something for; return `NO_VALUE` for everything else.
+
+```java
+public static Object invoke(String member, String ownerDotName,
+ String returnDotName, Object[] args) {
+ if ("createPerformanceManager".equals(member)
+ && "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) {
+ CommonCompilerArguments a = (CommonCompilerArguments) args[0];
+ Services services = (Services) args[1];
+ // build/return whatever this method should return, using the real args
+ return new MyPerformanceManager();
+ }
+ return NO_VALUE;
+}
+```
+
+- `member` / `ownerDotName` identify the method; `returnDotName` is its
+ **declared** return type (what the generated body `checkcast`s the result
+ to — your value must be assignable to it, or `ClassCastException`).
+- `args[i]` is the `i`-th parameter of the call, so your implementation works
+ on the **real** arguments the daemon handed the compiler: reference types
+ as-is, primitives boxed (`Integer`, `Long`, `Boolean`, …). A param-less
+ method gets `args == null`.
+- Returning `NO_VALUE` keeps the default (real no-arg instance, or null).
+ An override is logged as `OVERRIDE <owner>.<member> -> <dump>`.
+
+Rebuild with steps **2–3** after editing (no need to re-run the generator).
+Your overrides may reference the real compiler types — they resolve from the
+real jars on the daemon classpath; compile `KotlinProxyOverrides.java` with
+the jars from `jars/defs.txt` on `javac`'s classpath if you import them.
+
## Using the jar in a build
The daemon is a separate JVM. Its compiler classpath comes from the Gradle
@@ -223,6 +275,9 @@ RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompil
- `CALL <name>.<member> arg=<dump>` — boundary argument: compiler-argument
objects are dumped via `toCommonList()`; other objects via their
non-null fields across the hierarchy.
+- `OVERRIDE <name>.<member> -> <dump>` — a `KotlinProxyOverrides.invoke`
+ implementation supplied the return value (logged once, in
+ `KotlinProxyLog.returnValue`).
A `newInstance FAIL <type> : <exception>` line means a stub method's declared
return type has no usable no-arg constructor (e.g. `PerformanceManager`), so