From 1b065c8d50e1b5277bd99aa368706400e33bdbf5 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 7 Jun 2016 22:40:05 -0800 Subject: [PATCH] Clean up the streams. --- .../kobalt/plugin/kotlin/KotlinCompiler.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt index 212176b0..7626cefc 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -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)