From 82be14ae069ebd670048b7ea24245a6e25b2b182 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 10 Dec 2015 01:41:21 +0300 Subject: [PATCH] Use the Java Compiler API. --- TODO.md | 2 +- .../beust/kobalt/plugin/java/JavaCompiler.kt | 32 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/TODO.md b/TODO.md index 4ef41659..b53c7375 100644 --- a/TODO.md +++ b/TODO.md @@ -9,7 +9,6 @@ Android: General -- [ ] Compile with javax.tool - [ ] Apt should run from serviceloader - [ ] Auto add variant - [ ] The test runner only selects classes with a parameterless constructor, which works for JUnit but not for TestNG @@ -43,6 +42,7 @@ General Done: +- [x] Compile with javax.tool - [x] Android: multiple -source/-target flags - [x] Dokka: allow multiple format outputs e.g `outputFormat("html", "javadoc")` - [x] Finish abstracting everything in `JvmCompilerPlugin` diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt index e71c3cbe..42d8c3cf 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -9,12 +9,14 @@ import com.beust.kobalt.api.Project import com.beust.kobalt.internal.ICompilerAction import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn -import com.beust.kobalt.plugin.android.forward import com.google.inject.Inject import com.google.inject.Singleton import java.io.File +import java.io.PrintWriter +import javax.tools.DiagnosticCollector +import javax.tools.JavaFileObject +import javax.tools.ToolProvider @Singleton class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { @@ -25,31 +27,25 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { return TaskResult() } - info.outputDir.mkdirs() - val allArgs = arrayListOf( - executable.absolutePath, "-d", KFiles.makeDir(info.directory!!, info.outputDir.path).path) if (info.dependencies.size > 0) { allArgs.add("-classpath") - allArgs.add(info.dependencies.map {it.jarFile.get()}.joinToString(File.pathSeparator)) + allArgs.add(info.dependencies.map { it.jarFile.get() }.joinToString(File.pathSeparator)) } allArgs.addAll(info.compilerArgs) - allArgs.addAll(info.sourceFiles) - val pb = ProcessBuilder(allArgs) -// info.directory?.let { -// pb.directory(File(it)) -// } - pb.inheritIO() - val line = allArgs.joinToString(" ") - log(1, " Compiling ${info.sourceFiles.size} files") - log(2, " Compiling ${line.forward()}") - val process = pb.start() - val errorCode = process.waitFor() + val compiler = ToolProvider.getSystemJavaCompiler() + val fileManager = compiler.getStandardFileManager(null, null, null) + val fileObjects = fileManager.getJavaFileObjectsFromFiles(info.sourceFiles.map { File(it) }) + val dc = DiagnosticCollector() + val classes = arrayListOf() + val task = compiler.getTask(PrintWriter(System.out), fileManager, dc, allArgs, classes, fileObjects) + val result = task.call() - return if (errorCode == 0) TaskResult(true, "Compilation succeeded") + return if (result) TaskResult(true, "Compilation succeeded") else TaskResult(false, "There were errors") + } }