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:
parent
82b5c636d0
commit
1b065c8d50
1 changed files with 13 additions and 14 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue