Use correct platform classloader on script evaluation in cli compiler

#KT-28475 fixed
also minor testing infrastruct refactoring
This commit is contained in:
Ilya Chernikov
2020-02-03 16:00:23 +01:00
parent 489c4ec5ec
commit 514282fb0a
3 changed files with 35 additions and 3 deletions
@@ -23,7 +23,7 @@ class JvmCliScriptEvaluationExtension : AbstractScriptEvaluationExtension() {
override fun ScriptEvaluationConfiguration.Builder.platformEvaluationConfiguration() { override fun ScriptEvaluationConfiguration.Builder.platformEvaluationConfiguration() {
jvm { jvm {
baseClassLoader(null) baseClassLoader(getPlatformClassLoader())
} }
} }
@@ -50,3 +50,9 @@ class JvmCliScriptEvaluationExtension : AbstractScriptEvaluationExtension() {
arguments is K2JVMCompilerArguments && (arguments.script || arguments.expression != null) arguments is K2JVMCompilerArguments && (arguments.script || arguments.expression != null)
} }
private fun getPlatformClassLoader(): ClassLoader? =
try {
ClassLoader::class.java.getDeclaredMethod("getPlatformClassLoader")?.invoke(null)?.let { it as? ClassLoader }
} catch (_: Exception) {
null
}
@@ -71,6 +71,19 @@ class ScriptingWithCliCompilerTest {
) )
} }
@Test
fun testJdkModules() {
// actually tests anything on JDKs 9+, on pre-9 it always works because JDK is not modularized anyway
runWithKotlinc(
arrayOf(
"-Xadd-modules=java.sql",
"-expression",
"println(javax.sql.DataSource::class.java)"
),
listOf("interface javax.sql.DataSource")
)
}
private fun getMainKtsClassPath(): List<File> { private fun getMainKtsClassPath(): List<File> {
return listOf( return listOf(
File("dist/kotlinc/lib/kotlin-main-kts.jar").also { File("dist/kotlinc/lib/kotlin-main-kts.jar").also {
@@ -26,6 +26,20 @@ fun runWithKotlinc(
workDirectory: File? = null, workDirectory: File? = null,
classpath: List<File> = emptyList(), classpath: List<File> = emptyList(),
additionalEnvVars: Iterable<Pair<String, String>>? = null additionalEnvVars: Iterable<Pair<String, String>>? = null
) {
runWithKotlinc(
arrayOf("-script", scriptPath),
expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars
)
}
fun runWithKotlinc(
compilerArgs: Array<String>,
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
workDirectory: File? = null,
classpath: List<File> = emptyList(),
additionalEnvVars: Iterable<Pair<String, String>>? = null
) { ) {
val executableName = "kotlinc" val executableName = "kotlinc"
// TODO: // TODO:
@@ -39,8 +53,7 @@ fun runWithKotlinc(
add("-cp") add("-cp")
add(classpath.joinToString(File.pathSeparator)) add(classpath.joinToString(File.pathSeparator))
} }
add("-script") addAll(compilerArgs)
add(scriptPath)
} }
val processBuilder = ProcessBuilder(args) val processBuilder = ProcessBuilder(args)
if (workDirectory != null) { if (workDirectory != null) {