Set contextClassLoader for the script evaluation

#KT-31661
This commit is contained in:
Ilya Chernikov
2019-09-05 13:01:09 +02:00
parent 939d76fd2a
commit 92778cc5b5
3 changed files with 31 additions and 9 deletions
@@ -89,7 +89,13 @@ open class BasicJvmScriptEvaluator : ScriptEvaluator {
val ctor = java.constructors.single()
return ctor.newInstance(*args.toArray())
val saveClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = this.java.classLoader
return try {
ctor.newInstance(*args.toArray())
} finally {
Thread.currentThread().contextClassLoader = saveClassLoader
}
}
}
@@ -15,18 +15,30 @@ class MainKtsIT {
@Test
fun testResolveJunit() {
runWithKotlinc(
"$TEST_DATA_ROOT/hello-resolve-junit.main.kts", listOf("Hello, World!"),
classpath = listOf(
File("dist/kotlinc/lib/kotlin-main-kts.jar").also {
Assert.assertTrue("kotlin-main-kts.jar not found, run dist task: ${it.absolutePath}", it.exists())
}
)
)
runWithKotlincAndMainKts("$TEST_DATA_ROOT/hello-resolve-junit.main.kts", listOf("Hello, World!"))
}
@Test
fun testImport() {
runWithK2JVMCompiler("$TEST_DATA_ROOT/import-test.main.kts", listOf("Hi from common", "Hi from middle", "sharedVar == 5"))
}
@Test
fun testThreadContextClassLoader() {
runWithKotlincAndMainKts("$TEST_DATA_ROOT/context-classloader.main.kts", listOf("MainKtsConfigurator"))
}
}
fun runWithKotlincAndMainKts(
scriptPath: String,
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0
) = runWithKotlinc(
scriptPath, expectedOutPatterns, expectedExitCode,
classpath = listOf(
File("dist/kotlinc/lib/kotlin-main-kts.jar").also {
Assert.assertTrue("kotlin-main-kts.jar not found, run dist task: ${it.absolutePath}", it.exists())
}
)
)
@@ -0,0 +1,4 @@
val klass = java.lang.Thread.currentThread().contextClassLoader.loadClass("org.jetbrains.kotlin.mainKts.MainKtsConfigurator")
println(klass.simpleName)