diff options
Diffstat (limited to 'stubs/src/kotlin/proxy/KotlinProxyOverrides.java')
| -rw-r--r-- | stubs/src/kotlin/proxy/KotlinProxyOverrides.java | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java index d40417e..8064d19 100644 --- a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java +++ b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java @@ -8,6 +8,10 @@ import org.jetbrains.kotlin.config.JvmTarget; import org.jetbrains.kotlin.config.Services; import org.jetbrains.kotlin.utils.KotlinPaths; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Map; + /** * Per-method custom return values for the generated stubs. * @@ -19,29 +23,42 @@ import org.jetbrains.kotlin.utils.KotlinPaths; * * 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. + * args == null. + * + * instance is the receiver — the stub instance the method was called on — so + * you can keep per-compiler state (identity-keyed; see performanceManagers). + * It is null for static methods. 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; + /** + * Per-compiler-instance state, keyed by the stub receiver instance. + * Identity semantics (each K2JVMCompiler instance is its own entry), + * synchronized because daemon compilations may run on different threads. + */ + private static final Map<Object, Compiler> compilers = + Collections.synchronizedMap(new IdentityHashMap<>()); private KotlinProxyOverrides() { } public static Object invoke(String member, String ownerDotName, - String returnDotName, Object[] args) { + String returnDotName, Object[] args, + Object instance) { if ("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) { + Compiler compiler = compilers.computeIfAbsent(instance, (key) -> new Compiler()); + if ("createPerformanceManager".equals(member)) { K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0]; // Services services = (Services) args[1]; - sPerformanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget())); - return sPerformanceManager; + compiler.performanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget())); + return compiler.performanceManager; } if ("getPlatform".equals(member)) { - return sPerformanceManager.getTargetPlatform(); + return compiler.performanceManager.getTargetPlatform(); } if ("doExecute".equals(member)) { K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0]; @@ -53,4 +70,8 @@ public final class KotlinProxyOverrides { } return NO_VALUE; } + + private static class Compiler { + ProxyPerformanceManager performanceManager; + } } |
