From 306ac73eb7baae9df1b3bde3c419b35b59414254 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Mon, 5 Oct 2015 14:31:00 +0300 Subject: [PATCH] Actual KotlinCompilerAdapter conversion --- .../kotlin/ant/KotlinCompilerAdapter.kt | 160 ++++++++---------- .../kotlin/ant/KotlinCompilerBaseTask.kt | 4 +- 2 files changed, 71 insertions(+), 93 deletions(-) diff --git a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.kt b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.kt index b1a7112e83f..6cb77bf15d6 100644 --- a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.kt +++ b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerAdapter.kt @@ -14,152 +14,130 @@ * limitations under the License. */ -package org.jetbrains.kotlin.ant; +package org.jetbrains.kotlin.ant -import kotlin.KotlinPackage; -import kotlin.jvm.functions.Function1; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.MagicNames; -import org.apache.tools.ant.taskdefs.Javac; -import org.apache.tools.ant.taskdefs.compilers.Javac13; -import org.apache.tools.ant.taskdefs.condition.AntVersion; -import org.apache.tools.ant.types.Commandline; -import org.apache.tools.ant.types.Path; -import org.jetbrains.annotations.NotNull; +import org.apache.tools.ant.BuildException +import org.apache.tools.ant.MagicNames +import org.apache.tools.ant.Project.MSG_WARN +import org.apache.tools.ant.taskdefs.compilers.Javac13 +import org.apache.tools.ant.taskdefs.condition.AntVersion +import org.apache.tools.ant.types.Commandline +import org.apache.tools.ant.types.Path +import java.io.File +import java.util.* -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +class KotlinCompilerAdapter : Javac13() { -import static org.apache.tools.ant.Project.MSG_WARN; + var externalAnnotations: Path? = null -public class KotlinCompilerAdapter extends Javac13 { - private static final List KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts"); + var moduleName: String? = null - private Path externalAnnotations; + var additionalArguments: MutableList = ArrayList(0) - private String moduleName; - - public List additionalArguments = new ArrayList(0); - - public void setExternalAnnotations(Path externalAnnotations) { - this.externalAnnotations = externalAnnotations; - } - - public void setModuleName(String moduleName) { - this.moduleName = moduleName; - } - - public Path createExternalAnnotations() { + fun createExternalAnnotations(): Path { if (externalAnnotations == null) { - externalAnnotations = new Path(getProject()); + externalAnnotations = Path(getProject()) } - return externalAnnotations.createPath(); + return externalAnnotations!!.createPath() } - public Commandline.Argument createCompilerArg() { - Commandline.Argument argument = new Commandline.Argument(); - additionalArguments.add(argument); - return argument; + fun createCompilerArg(): Commandline.Argument { + val argument = Commandline.Argument() + additionalArguments.add(argument) + return argument } - @Override - public String[] getSupportedFileExtensions() { - List result = KotlinPackage.plus(Arrays.asList(super.getSupportedFileExtensions()), KOTLIN_EXTENSIONS); - //noinspection SSBasedInspection - return result.toArray(new String[result.size()]); + override fun getSupportedFileExtensions(): Array { + return super.getSupportedFileExtensions() + KOTLIN_EXTENSIONS } - @Override - public boolean execute() throws BuildException { - Javac javac = getJavac(); + @Throws(BuildException::class) + override fun execute(): Boolean { + val javac = javac - checkAntVersion(); + checkAntVersion() - Kotlin2JvmTask kotlinc = new Kotlin2JvmTask(); - kotlinc.setFailOnError(javac.getFailonerror()); - kotlinc.setOutput(javac.getDestdir()); + val kotlinc = Kotlin2JvmTask() + kotlinc.failOnError = javac.failonerror + kotlinc.output = javac.destdir - Path classpath = javac.getClasspath(); + val classpath = javac.classpath if (classpath != null) { - kotlinc.setClasspath(classpath); + kotlinc.setClasspath(classpath) } // We use the provided src dir instead of compileList, because the latter is insane: // it is constructed only of sources which are newer than classes with the same name - kotlinc.setSrc(javac.getSrcdir()); + kotlinc.src = javac.srcdir - kotlinc.setExternalAnnotations(externalAnnotations); + kotlinc.externalAnnotations = externalAnnotations if (moduleName == null) { - moduleName = AntPackage.getDefaultModuleName(javac); + moduleName = javac.defaultModuleName } - kotlinc.setModuleName(moduleName); + kotlinc.moduleName = moduleName - kotlinc.getAdditionalArguments().addAll(additionalArguments); + kotlinc.additionalArguments.addAll(additionalArguments) // Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files - File[] compileListForJavac = filterOutKotlinSources(compileList); + val compileListForJavac = filterOutKotlinSources(compileList) - boolean hasKotlinFilesInSources = compileListForJavac.length < compileList.length; + val hasKotlinFilesInSources = compileListForJavac.size() < compileList.size() if (hasKotlinFilesInSources) { - kotlinc.execute(); - if (!Integer.valueOf(0).equals(kotlinc.getExitCode())) { + kotlinc.execute() + if (kotlinc.exitCode != 0) { // Don't run javac if failOnError = false and there were errors on Kotlin sources - return false; + return false } } else { // This is needed for addRuntimeToJavacClasspath, where kotlinc arguments will be used. - kotlinc.fillArguments(); + kotlinc.fillArguments() } - javac.log("Running javac..."); + javac.log("Running javac...") - compileList = compileListForJavac; + compileList = compileListForJavac - addRuntimeToJavacClasspath(kotlinc); + addRuntimeToJavacClasspath(kotlinc) - return compileList.length == 0 || super.execute(); + return compileList.isEmpty() || super.execute() } - private void addRuntimeToJavacClasspath(@NotNull Kotlin2JvmTask kotlinc) { - for (String arg : kotlinc.getArgs()) { + private fun addRuntimeToJavacClasspath(kotlinc: Kotlin2JvmTask) { + for (arg in kotlinc.args) { // If "-no-stdlib" was specified explicitly, probably the user also wanted the javac classpath to not have it - if ("-no-stdlib".equals(arg)) return; + if ("-no-stdlib" == arg) return } if (compileClasspath == null) { - compileClasspath = new Path(getProject()); + compileClasspath = Path(getProject()) } - compileClasspath.add(new Path(getProject(), KotlinAntTaskUtil.INSTANCE$.getRuntimeJar().getAbsolutePath())); + compileClasspath.add(Path(getProject(), KotlinAntTaskUtil.runtimeJar.absolutePath)) } - private void checkAntVersion() { - AntVersion checkVersion = new AntVersion(); - checkVersion.setAtLeast("1.8.2"); + private fun checkAntVersion() { + val checkVersion = AntVersion() + checkVersion.atLeast = "1.8.2" if (!checkVersion.eval()) { - getJavac().log(" task requires Ant of version at least 1.8.2 to operate reliably. " + - "Please upgrade or, as a workaround, make sure you have at least one Java source and " + - "the output directory is clean before running this task. " + - "You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN); + javac.log(" task requires Ant of version at least 1.8.2 to operate reliably. " + + "Please upgrade or, as a workaround, make sure you have at least one Java source and " + + "the output directory is clean before running this task. " + + "You have: " + getProject().getProperty(MagicNames.ANT_VERSION), MSG_WARN) } } - @NotNull - private static File[] filterOutKotlinSources(@NotNull File[] files) { - List nonKotlinSources = KotlinPackage.filterNot(files, new Function1() { - @Override - public Boolean invoke(File file) { - for (String extension : KOTLIN_EXTENSIONS) { - if (file.getPath().endsWith("." + extension)) return true; - } - return false; - } - }); + companion object { + private val KOTLIN_EXTENSIONS = Arrays.asList("kt", "kts") - return nonKotlinSources.toArray(new File[nonKotlinSources.size()]); + private fun filterOutKotlinSources(files: Array): Array { + return files.filterNot { + for (extension in KOTLIN_EXTENSIONS) { + if (it.path.endsWith("." + extension)) return@filterNot true + } + false + }.toTypedArray() + } } } diff --git a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerBaseTask.kt b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerBaseTask.kt index 0197d962892..e03d719f40c 100644 --- a/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerBaseTask.kt +++ b/ant/src/org/jetbrains/kotlin/ant/KotlinCompilerBaseTask.kt @@ -27,7 +27,7 @@ import java.io.PrintStream public abstract class KotlinCompilerBaseTask : Task() { protected abstract val compilerFqName: String - protected val args: MutableList = arrayListOf() + public val args: MutableList = arrayListOf() public var src: Path? = null public var output: File? = null @@ -40,7 +40,7 @@ public abstract class KotlinCompilerBaseTask : Task() { public val additionalArguments: MutableList = arrayListOf() - internal var exitCode: Int? = null + public var exitCode: Int? = null public fun createSrc(): Path { val srcPath = src