summaryrefslogtreecommitdiff
path: root/stubs/src/kotlin/proxy/KotlinProxyNoLog.java
blob: 8b7e202aa068cf411fe3291bd25090194c3b1606 (plain)
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
package kotlin.proxy;

public final class KotlinProxyNoLog {
    private KotlinProxyNoLog() {
    }

    /**
     * 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 the receiver
     * (the stub instance the method was called on, null for statics) — 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,
     * return type and receiver last) so its stack stays shallow (peak 5) —
     * 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 instance) {
        Object v = KotlinProxyOverrides.invoke(member, ownerDotName,
                returnDotName, args, instance);
        if (v == KotlinProxyOverrides.NO_VALUE) {
            return newInstance(returnDotName);
        }
        return v;
    }

    /**
     * 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.).
     */
    private static Object newInstance(String dotName) {
        try {
            Class<?> c = Class.forName(dotName, false,
                    KotlinProxyNoLog.class.getClassLoader());
            return c.getDeclaredConstructor().newInstance();
        } catch (Throwable t) {
            return null;
        }
    }
}