From aefb3dbf687b46775bca1b600317bc053648b211 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 5 Nov 2026 20:41:25 +0100 Subject: First steps for overrides --- stubs/src/kotlin/proxy/KotlinProxyLog.java | 30 ++++++++++-- stubs/src/kotlin/proxy/KotlinProxyOverrides.java | 56 ++++++++++++++++++++++ .../src/kotlin/proxy/ProxyPerformanceManager.java | 11 +++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 stubs/src/kotlin/proxy/KotlinProxyOverrides.java create mode 100644 stubs/src/kotlin/proxy/ProxyPerformanceManager.java (limited to 'stubs/src') diff --git a/stubs/src/kotlin/proxy/KotlinProxyLog.java b/stubs/src/kotlin/proxy/KotlinProxyLog.java index 50e347a..3afdb38 100644 --- a/stubs/src/kotlin/proxy/KotlinProxyLog.java +++ b/stubs/src/kotlin/proxy/KotlinProxyLog.java @@ -51,9 +51,33 @@ public final class KotlinProxyLog { } /** - * Called by stub methods that declare a reference return type (other than - * String). Reflectively instantiates the real, un-stubbed class of that - * return type so the caller (the daemon) can keep going: e.g. + * Return value for stub methods with a reference return type (other than + * String). Asks {@link KotlinProxyOverrides#invoke} first — per-method + * custom implementations, passed the full argument list — and falls back + * to materializing a real no-arg instance of the declared return type. + * The generated body checkcasts the result to the declared type. + * + * The generated bytecode pushes the args array FIRST (member, owner and + * return type last) so its stack stays shallow (peak 4) — keep this + * parameter order in sync with RETURN_VALUE_DESC in gen_class.py. + */ + public static Object returnValue(Object[] args, String member, + String ownerDotName, String returnDotName) { + Object v = KotlinProxyOverrides.invoke(member, ownerDotName, + returnDotName, args); + if (v != KotlinProxyOverrides.NO_VALUE) { + log("OVERRIDE " + ownerDotName + "." + member + + " -> " + dump(v)); + return v; + } + return newInstance(returnDotName); + } + + /** + * Default return-value materialization (the fallback of + * {@link #returnValue}). Reflectively instantiates the real, un-stubbed + * class of the declared return type so the caller (the daemon) can keep + * going: e.g. * K2JVMCompiler.createArguments() returns a genuine K2JVMCompilerArguments, * which lets parseCommandLineArguments run and doExecute be reached with the * fully-populated args (the boundary we want to log). Returns null when the diff --git a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java new file mode 100644 index 0000000..d40417e --- /dev/null +++ b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java @@ -0,0 +1,56 @@ +package kotlin.proxy; + +import org.jetbrains.kotlin.cli.common.ExitCode; +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; +import org.jetbrains.kotlin.com.intellij.openapi.Disposable; +import org.jetbrains.kotlin.config.CompilerConfiguration; +import org.jetbrains.kotlin.config.JvmTarget; +import org.jetbrains.kotlin.config.Services; +import org.jetbrains.kotlin.utils.KotlinPaths; + +/** + * Per-method custom return values for the generated stubs. + * + * Every stub method with a reference return type (other than String) asks + * here for its return value (via {@link KotlinProxyLog#returnValue}) before + * falling back to the default — a real no-arg instance of the declared return + * type, or null. Implement the methods you want the stub to actually do + * something for; return {@link #NO_VALUE} for everything else. + * + * args[i] is the i-th parameter of the call: reference types as-is, + * primitives boxed (Integer, Long, Boolean, ...). A param-less method gets + * args == null. Rebuild the jar (steps 2-3) after editing. + */ +public final class KotlinProxyOverrides { + + /** Return this from invoke() to keep the default return value. */ + public static final Object NO_VALUE = new Object(); + + private static ProxyPerformanceManager sPerformanceManager; + + private KotlinProxyOverrides() { + } + + public static Object invoke(String member, String ownerDotName, + String returnDotName, Object[] args) { + if ("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) { + if ("createPerformanceManager".equals(member)) { + K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0]; + // Services services = (Services) args[1]; + sPerformanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget())); + return sPerformanceManager; + } + if ("getPlatform".equals(member)) { + return sPerformanceManager.getTargetPlatform(); + } + if ("doExecute".equals(member)) { + K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0]; + CompilerConfiguration config = (CompilerConfiguration) args[1]; + Disposable disposable = (Disposable) args[2]; + KotlinPaths paths = (KotlinPaths) args[3]; + return ExitCode.COMPILATION_ERROR; + } + } + return NO_VALUE; + } +} diff --git a/stubs/src/kotlin/proxy/ProxyPerformanceManager.java b/stubs/src/kotlin/proxy/ProxyPerformanceManager.java new file mode 100644 index 0000000..1e2e354 --- /dev/null +++ b/stubs/src/kotlin/proxy/ProxyPerformanceManager.java @@ -0,0 +1,11 @@ +package kotlin.proxy; + +import org.jetbrains.kotlin.config.JvmTarget; +import org.jetbrains.kotlin.util.PerformanceManager; +import org.jetbrains.kotlin.platform.jvm.JvmPlatforms; +; +class ProxyPerformanceManager extends PerformanceManager { + ProxyPerformanceManager(JvmTarget targetVersion) { + super(JvmPlatforms.INSTANCE.jvmPlatformByTargetVersion(targetVersion), "proxy perf manager"); + } +} -- cgit v1.3.1