package kotlin.proxy; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * Central call log for kotlin-proxy stubs. * * Every stub method/constructor and every stub class load is recorded here, * in order, so you can see exactly which stubs AGP/KGP touch. */ public final class KotlinProxyLog { private KotlinProxyLog() { } /** * String-based overload used by the bytecode-generated stubs (they pass * the dotted class name rather than a Class literal). Resolves the * already-loaded class to recover its code source location. */ public static void loaded(String dotName) { String from = "?"; try { Class c = Class.forName(dotName, false, KotlinProxyLog.class.getClassLoader()); if (c.getProtectionDomain().getCodeSource() != null && c.getProtectionDomain().getCodeSource().getLocation() != null) { from = c.getProtectionDomain().getCodeSource().getLocation().toString(); } } catch (Throwable t) { // ignore } log("LOAD " + dotName + " <- " + from); } public static void call(String member, String dotName) { log("CALL " + dotName + "." + member); } /** * 3-arg overload used by the bytecode stubs to log the boundary: the stub * passes its first (reference) parameter - typically the compiler * K2JVMCompilerArguments - so we can see exactly what AGP/KGP handed the * (replaced) compiler. */ public static void call(String member, String dotName, Object arg) { log("CALL " + dotName + "." + member + " arg=" + dump(arg)); } /** * 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 * type has no usable no-arg constructor (interfaces, enums, abstract, etc.). */ public static Object newInstance(String dotName) { try { Class c = Class.forName(dotName, false, KotlinProxyLog.class.getClassLoader()); return c.getDeclaredConstructor().newInstance(); } catch (Throwable t) { log("newInstance FAIL " + dotName + " : " + t + " | loader=" + KotlinProxyLog.class.getClassLoader()); return null; } } /** Reflectively summarize an arbitrary arg object for the log. */ private static String dump(Object o) { if (o == null) { return "null"; } try { // Compiler-argument types expose toCommonList(); show it verbatim. try { java.lang.reflect.Method m = o.getClass().getMethod("toCommonList"); Object list = m.invoke(o); if (list != null) { return list.toString(); } } catch (Throwable ignore) { } // Otherwise dump the non-null declared fields across the hierarchy. StringBuilder sb = new StringBuilder(o.getClass().getSimpleName()).append('{'); for (Class c = o.getClass(); c != null && c != Object.class; c = c.getSuperclass()) { for (java.lang.reflect.Field f : c.getDeclaredFields()) { if (java.lang.reflect.Modifier.isStatic(f.getModifiers())) { continue; } try { f.setAccessible(true); Object v = f.get(o); if (v == null) { continue; } sb.append(f.getName()).append('=').append(v).append(' '); } catch (Throwable ignore) { } } } return sb.append('}').toString(); } catch (Throwable t) { try { return o.toString(); } catch (Throwable t2) { return ""; } } } private static void log(String line) { try { String marker = System.getProperty("kotlin.proxy.marker", "/tmp/kotlin-proxy-marker.txt"); synchronized (KotlinProxyLog.class) { BufferedWriter mw = new BufferedWriter(new java.io.FileWriter(marker, true)); mw.write(Thread.currentThread().getName() + " | " + line + "\n"); mw.close(); } } catch (Exception e) { // ignore } } }