1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Clean up the streams.

This commit is contained in:
Cedric Beust 2016-06-07 22:40:05 -08:00
parent 82b5c636d0
commit 1b065c8d50

View file

@ -94,27 +94,26 @@ class KotlinCompiler @Inject constructor(
val classLoader = ParentLastClassLoader(compilerJar)
val compiler = classLoader.loadClass("org.jetbrains.kotlin.cli.common.CLICompiler")
val compilerMain = compiler.declaredMethods.filter {
it.name == "doMainNoExit" && it.parameterTypes.size == 2
}[0]
val kCompiler = classLoader.loadClass("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler")
//
// In order to capture the error stream, I need to invoke CLICompiler.exec(), which
// is the first method that accepts a PrintStream for the errors in parameter
//
val baos = ByteArrayOutputStream()
val ps = PrintStream(baos)
val execMethod = compiler.declaredMethods.filter {
it.name == "exec" && it.parameterTypes.size == 2
}[0]
val exitCode = execMethod.invoke(kCompiler.newInstance(), ps, allArgs.toTypedArray())
val errorString = baos.toString(Charset.defaultCharset().toString())
ByteArrayOutputStream().use { baos ->
PrintStream(baos).use { ps ->
val execMethod = compiler.declaredMethods.filter {
it.name == "exec" && it.parameterTypes.size == 2
}[0]
val exitCode = execMethod.invoke(kCompiler.newInstance(), ps, allArgs.toTypedArray())
val errorString = baos.toString(Charset.defaultCharset().toString())
// The return value is an enum
val nameMethod = exitCode.javaClass.getMethod("name")
val success = "OK" == nameMethod.invoke(exitCode).toString()
TaskResult(success, errorString)
// The return value is an enum
val nameMethod = exitCode.javaClass.getMethod("name")
val success = "OK" == nameMethod.invoke(exitCode).toString()
TaskResult(success, errorString)
}
}
} else {
val exitCode = CLICompiler.doMainNoExit(K2JVMCompiler(), allArgs.toTypedArray())
TaskResult(exitCode == ExitCode.OK)