Ant task: pass kotlin-runtime to javac's classpath; simplify tests
'kotlinCompiler' test is deleted because it's a very roundabout way to compile Kotlin and Java sources, and there's no point in testing it. 'javacCompiler' test is deleted for similar reasons: using javac's "compiler" option is not an advertised way to use Kotlin Ant task, and anyway it will work with any CompilerAdapter instance, so it's hard to break it (apart from deleting "withKotlin" adapter). There are tests which check compilation/running of the same code but with the recommended method. The pretext for deletion of these tests is to make Ant tests generated, and that would be easier if they wouldn't depend on any outer variables
This commit is contained in:
@@ -21,10 +21,36 @@ import org.jetbrains.kotlin.preloading.ClassPreloadingUtils
|
||||
import java.io.File
|
||||
import java.lang.ref.SoftReference
|
||||
import java.net.JarURLConnection
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
object KotlinAntTaskUtil {
|
||||
private var classLoaderRef = SoftReference<ClassLoader?>(null)
|
||||
|
||||
private val libPath: File by Delegates.lazy {
|
||||
// Find path of kotlin-ant.jar in the filesystem and find kotlin-compiler.jar in the same directory
|
||||
val resourcePath = "/" + javaClass.getName().replace('.', '/') + ".class"
|
||||
val jarConnection = javaClass.getResource(resourcePath).openConnection() as? JarURLConnection
|
||||
?: throw UnsupportedOperationException("Kotlin compiler Ant task should be loaded from the JAR file")
|
||||
val antTaskJarPath = File(jarConnection.getJarFileURL().toURI())
|
||||
|
||||
antTaskJarPath.getParentFile()
|
||||
}
|
||||
|
||||
val compilerJar: File by Delegates.lazy {
|
||||
File(libPath, "kotlin-compiler.jar").assertExists()
|
||||
}
|
||||
|
||||
val runtimeJar: File by Delegates.lazy {
|
||||
File(libPath, "kotlin-runtime.jar").assertExists()
|
||||
}
|
||||
|
||||
private fun File.assertExists(): File {
|
||||
if (!this.exists()) {
|
||||
throw IllegalStateException("${getName()} is not found in the directory of Kotlin Ant task")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
synchronized fun getOrCreateClassLoader(): ClassLoader {
|
||||
val cached = classLoaderRef.get()
|
||||
if (cached != null) return cached
|
||||
@@ -32,18 +58,7 @@ object KotlinAntTaskUtil {
|
||||
val myLoader = javaClass.getClassLoader()
|
||||
if (myLoader !is AntClassLoader) return myLoader
|
||||
|
||||
// Find path of kotlin-ant.jar in the filesystem and find kotlin-compiler.jar in the same directory
|
||||
val resourcePath = "/" + javaClass.getName().replace('.', '/') + ".class"
|
||||
val jarConnection = javaClass.getResource(resourcePath).openConnection() as? JarURLConnection
|
||||
?: throw UnsupportedOperationException("Kotlin compiler Ant task should be loaded from the JAR file")
|
||||
val antTaskJarPath = File(jarConnection.getJarFileURL().toURI())
|
||||
|
||||
val compilerJarPath = File(antTaskJarPath.getParent(), "kotlin-compiler.jar")
|
||||
if (!compilerJarPath.exists()) {
|
||||
throw IllegalStateException("kotlin-compiler.jar is not found in the directory of Kotlin Ant task")
|
||||
}
|
||||
|
||||
val classLoader = ClassPreloadingUtils.preloadClasses(listOf(compilerJarPath), 4096, myLoader, null)
|
||||
val classLoader = ClassPreloadingUtils.preloadClasses(listOf(compilerJar), 4096, myLoader, null)
|
||||
classLoaderRef = SoftReference(classLoader)
|
||||
|
||||
return classLoader
|
||||
|
||||
@@ -87,6 +87,11 @@ public class KotlinCompilerAdapter extends Javac13 {
|
||||
// Javac13#execute passes everything in compileList to javac, which doesn't recognize .kt files
|
||||
compileList = filterOutKotlinSources(compileList);
|
||||
|
||||
if (compileClasspath == null) {
|
||||
compileClasspath = new Path(getProject());
|
||||
}
|
||||
compileClasspath.add(new Path(getProject(), KotlinAntTaskUtil.INSTANCE$.getRuntimeJar().getAbsolutePath()));
|
||||
|
||||
return compileList.length == 0 || super.execute();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
<project name="Ant Task Test" default="build">
|
||||
|
||||
<path id="classpath">
|
||||
<fileset dir="${idea.sdk}/lib" includes="annotations.jar"/>
|
||||
<fileset dir="${kotlin.home}" includes="kotlin-runtime.jar"/>
|
||||
</path>
|
||||
|
||||
<typedef name="withKotlin" classname="org.jetbrains.kotlin.ant.KotlinCompilerAdapter"/>
|
||||
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml" classpath="${kotlin.lib}/kotlin-ant.jar"/>
|
||||
|
||||
<target name="build">
|
||||
<delete dir="${temp}/classes" failonerror="false"/>
|
||||
<mkdir dir="${temp}/classes"/>
|
||||
<javac destdir="${temp}/classes" includeAntRuntime="false" srcdir="${test.data}/root1">
|
||||
<classpath refid="classpath"/>
|
||||
<withKotlin externalannotations="${test.data}/root1/b/">
|
||||
<externalannotations path="${test.data}/root1/a/"/>
|
||||
</withKotlin>
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
OUT:
|
||||
Buildfile: [TestData]/build.xml
|
||||
|
||||
build:
|
||||
[mkdir] Created dir: [Temp]/classes
|
||||
[javac] Compiling 2 source files to [Temp]/classes
|
||||
[javac] Compiling [[TestData]/root1] => [[Temp]/classes]
|
||||
[javac] Running javac...
|
||||
[jar] Building jar: [Temp]/hello.jar
|
||||
|
||||
BUILD SUCCESSFUL
|
||||
Total time: [time]
|
||||
|
||||
Return code: 0
|
||||
@@ -1,18 +0,0 @@
|
||||
<project name="Ant Task Test" default="build">
|
||||
|
||||
<path id="classpath">
|
||||
<fileset dir="${kotlin.home}" includes="kotlin-runtime.jar"/>
|
||||
</path>
|
||||
|
||||
<target name="build">
|
||||
<delete dir="${temp}/classes" failonerror="false"/>
|
||||
<mkdir dir="${temp}/classes"/>
|
||||
<javac destdir="${temp}/classes" includeantruntime="false" compiler="org.jetbrains.kotlin.ant.KotlinCompilerAdapter">
|
||||
<src path="${test.data}/root1"/>
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
<jar destfile="${temp}/hello.jar">
|
||||
<fileset dir="${temp}/classes"/>
|
||||
</jar>
|
||||
</target>
|
||||
</project>
|
||||
@@ -1,5 +0,0 @@
|
||||
OUT:
|
||||
Hello, a!
|
||||
Java
|
||||
|
||||
Return code: 0
|
||||
@@ -1,7 +0,0 @@
|
||||
package hello
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
for (s in arrayListOf("a"))
|
||||
println("Hello, $s!")
|
||||
j.Java().f();
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package j;
|
||||
|
||||
import java.lang.String;
|
||||
import java.lang.System;
|
||||
|
||||
public class Java {
|
||||
public void f() {
|
||||
System.out.println("Java");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
hello.HelloPackage.main(new String[] {});
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
OUT:
|
||||
Buildfile: [TestData]/build.xml
|
||||
|
||||
build:
|
||||
[mkdir] Created dir: [Temp]/classes
|
||||
[kotlinc] Compiling [[TestData]/root1] => [[Temp]/classes]
|
||||
[javac] Compiling 1 source file to [Temp]/classes
|
||||
[jar] Building jar: [Temp]/hello.jar
|
||||
|
||||
BUILD SUCCESSFUL
|
||||
Total time: [time]
|
||||
|
||||
Return code: 0
|
||||
@@ -1,24 +0,0 @@
|
||||
<project name="Ant Task Test" default="build">
|
||||
|
||||
<path id="classpath">
|
||||
<fileset dir="${idea.sdk}/lib" includes="annotations.jar"/>
|
||||
<fileset dir="${kotlin.home}" includes="kotlin-runtime.jar"/>
|
||||
</path>
|
||||
|
||||
<taskdef name="kotlinc" classname="org.jetbrains.kotlin.ant.Kotlin2JvmTask"/>
|
||||
|
||||
<target name="build">
|
||||
<delete dir="${temp}/classes" failonerror="false"/>
|
||||
<mkdir dir="${temp}/classes"/>
|
||||
<kotlinc output="${temp}/classes" src="${test.data}/root1" externalannotations="${test.data}/root1/b/">
|
||||
<classpath refid="classpath"/>
|
||||
<externalannotations path="${test.data}/root1/a/"/>
|
||||
</kotlinc>
|
||||
<javac destdir="${temp}/classes" includeAntRuntime="false" srcdir="${test.data}/root1">
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
<jar destfile="${temp}/hello.jar">
|
||||
<fileset dir="${temp}/classes"/>
|
||||
</jar>
|
||||
</target>
|
||||
</project>
|
||||
@@ -1,5 +0,0 @@
|
||||
OUT:
|
||||
Hello, a!
|
||||
Java Hello
|
||||
|
||||
Return code: 0
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
<root>
|
||||
<item name='j.Java java.lang.String f()'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
</root>
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
<root>
|
||||
<item name='j.Java java.lang.String f2()'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
</root>
|
||||
@@ -1,9 +0,0 @@
|
||||
package hello
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
for (s in arrayListOf("a"))
|
||||
println("Hello, $s!")
|
||||
val java: String = j.Java().f()
|
||||
val hello: String = j.Java().f2()
|
||||
println("$java $hello")
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package j;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.String;
|
||||
import java.lang.System;
|
||||
|
||||
public class Java {
|
||||
public String f() {
|
||||
return "Java";
|
||||
}
|
||||
|
||||
public String f2() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
hello.HelloPackage.main(new String[] {});
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.integration;
|
||||
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.cli.CliBaseTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -33,8 +29,8 @@ public abstract class AntTaskBaseTest extends KotlinIntegrationTestBase {
|
||||
protected static final int SUCCESSFUL = 0;
|
||||
protected static final int FAILED = 1;
|
||||
|
||||
protected void doAntTest(int expectedExitCode, String... extraArgs) throws Exception {
|
||||
assertEquals("Compilation failed", expectedExitCode, runAnt("build.log", "build.xml", extraArgs));
|
||||
protected void doAntTest(int expectedExitCode) throws Exception {
|
||||
assertEquals("Compilation failed", expectedExitCode, runAnt("build.log", "build.xml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,20 +39,18 @@ public abstract class AntTaskBaseTest extends KotlinIntegrationTestBase {
|
||||
.replaceAll("Total time: .+\n", "Total time: [time]\n");
|
||||
}
|
||||
|
||||
private int runAnt(String logName, String scriptName, String... extraArgs) throws Exception {
|
||||
String[] basicArgs = {
|
||||
private int runAnt(@NotNull String logName, @NotNull String scriptName) throws Exception {
|
||||
return runJava(
|
||||
logName,
|
||||
"-jar", getAntHome() + File.separator + "lib" + File.separator + "ant-launcher.jar",
|
||||
"-Dkotlin.lib=" + getCompilerLib(),
|
||||
"-Dtest.data=" + getTestDataDir(),
|
||||
"-Dtemp=" + tmpdir.getTmpDir(),
|
||||
"-f", scriptName
|
||||
};
|
||||
List<String> strings = new ArrayList<String>();
|
||||
strings.addAll(Arrays.asList(basicArgs));
|
||||
strings.addAll(Arrays.asList(extraArgs));
|
||||
return runJava(logName, ArrayUtil.toStringArray(strings));
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getAntHome() {
|
||||
return getKotlinProjectHome().getAbsolutePath() + File.separator + "dependencies" + File.separator + "ant-1.8";
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
|
||||
return new File(new File(ANT_TASK_TEST_DATA_BASE_DIR, "jvm"), name.getMethodName());
|
||||
}
|
||||
|
||||
private void doJvmAntTest(String... extraJavaArgs) throws Exception {
|
||||
doAntTest(SUCCESSFUL, extraJavaArgs);
|
||||
private void doJvmAntTest() throws Exception {
|
||||
doAntTest(SUCCESSFUL);
|
||||
|
||||
String classpath = UtilsPackage.join(Arrays.asList(
|
||||
getOutputFileByName(JVM_OUT_FILE).getAbsolutePath(),
|
||||
@@ -50,17 +50,6 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
|
||||
runJava("hello.run", "-cp", classpath, "hello.HelloPackage");
|
||||
}
|
||||
|
||||
private static String getClassPathForAnt() {
|
||||
return UtilsPackage.join(Arrays.asList(
|
||||
getCompilerLib() + File.separator + "kotlin-compiler.jar",
|
||||
getCompilerLib() + File.separator + "kotlin-ant.jar"
|
||||
), File.pathSeparator);
|
||||
}
|
||||
|
||||
private static String getIdeaSdkHome() {
|
||||
return getKotlinProjectHome().getAbsolutePath() + File.separator + "ideaSDK";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void helloWorld() throws Exception {
|
||||
doJvmAntTest();
|
||||
@@ -111,23 +100,8 @@ public class AntTaskJvmTest extends AntTaskBaseTest {
|
||||
doJvmAntTest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void javacCompiler() throws Exception {
|
||||
doJvmAntTest("-cp", getClassPathForAnt(),
|
||||
"-Dkotlin.home", getCompilerLib().getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void externalAnnotations() throws Exception {
|
||||
doJvmAntTest("-cp", getClassPathForAnt(),
|
||||
"-Didea.sdk", getIdeaSdkHome(),
|
||||
"-Dkotlin.home", getCompilerLib().getAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kotlinCompiler() throws Exception {
|
||||
doJvmAntTest("-cp", getClassPathForAnt(),
|
||||
"-Didea.sdk", getIdeaSdkHome(),
|
||||
"-Dkotlin.home", getCompilerLib().getAbsolutePath());
|
||||
doJvmAntTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user