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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user