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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package kotlin.proxy;
import org.jetbrains.kotlin.cli.common.ExitCode;
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
import org.jetbrains.kotlin.config.JvmTarget;
import org.jetbrains.kotlin.config.Services;
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms;
import org.jetbrains.kotlin.progress.CompilationCanceledStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;
/**
* 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.
*
* 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();
/**
* 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,
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];
compiler.performanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget()));
return compiler.performanceManager;
}
if ("getPlatform".equals(member)) {
return JvmPlatforms.INSTANCE.getDefaultJvmPlatform();
}
if ("doExecutePhased".equals(member)) {
K2JVMCompilerArguments arguments = (K2JVMCompilerArguments) args[0];
Services services = (Services) args[1];
MessageCollector messageCollector = (MessageCollector) args[2];
CompilationCanceledStatus canceledStatus = services.get(CompilationCanceledStatus.class);
ArrayList<String> cmd = new ArrayList<String>();
cmd.add("/home/the_jk/source/kotlin_proxy/img/kotlin-native-image-linux-x86_64-2.4.21/bin/kotlinc-native-image.sh");
cmd.addAll(GeneratedJvmArguments.buildCommandArgs(arguments));
cmd.addAll(arguments.getFreeArgs());
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.environment().put("JAVA_HOME", arguments.getJdkHome());
try {
Process process = builder.start();
int exitvalue = process.waitFor();
/*
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
*/
if (exitvalue == 0) {
return ExitCode.OK;
}
messageCollector.report(CompilerMessageSeverity.ERROR, "Shit is broken, yo!", null);
return ExitCode.COMPILATION_ERROR;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
return NO_VALUE;
}
private static class Compiler {
ProxyPerformanceManager performanceManager;
}
}
|