molikuner
01/20/2023, 7:55 PMscriptExecutionWrapper
for ScriptEvaluationConfiguration
. I though I could use this to do some custom action based on the result of the script. This works when using the following function from within a simple main:
fun File.evaluate(args: List<String>): ResultWithDiagnostics<EvaluationResult> {
return BasicJvmScriptingHost().evalWithTemplate<CustomScript>(this.toScriptSource()) {
constructorArgs(args.toTypedArray())
}
}
Unfortunately, it fails when trying to use the script definition for standalone scripting like this:
kotlinc -cp kt-custom-scripting-1.0-SNAPSHOT-jar-with-dependencies.jar -script example.custom.kts
I can see, that the script itself gets executed, but afterwards there is a ClassCastException
, due to different class loaders. I can also verify that, using this scriptExecutionWrapper:
scriptExecutionWrapper<Any> { script ->
val executionClassLoader = script::class.java.classLoader
println("executionClassLoader: $executionClassLoader")
val scriptInstance = script()
val scriptInstanceClassLoader = scriptInstance::class.java.classLoader
println("scriptInstanceClassLoader: $scriptInstanceClassLoader")
scriptInstance as CustomScript // this fails due to different ClassLoader
// some action, that I want to do
println(scriptInstance.someProperty)
scriptInstance
}
Here is the output, whey trying to run this:
executionClassLoader: java.net.URLClassLoader@3e08ff24
Hello from custom script!
scriptInstanceClassLoader: org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader@75af9a18
error: class Example_custom cannot be cast to class com.molikuner.custom.scripting.CustomScript (Example_custom is in unnamed module of loader org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader @75af9a18; com.molikuner.custom.scripting.CustomScript is in unnamed module of loader java.net.URLClassLoader @3a50b75c) (example.custom.kts): java.lang.ClassCastException: class Example_custom cannot be cast to class com.molikuner.custom.scripting.CustomScript (Example_custom is in unnamed module of loader org.jetbrains.kotlin.scripting.compiler.plugin.impl.CompiledScriptClassLoader @75af9a18; com.molikuner.custom.scripting.CustomScript is in unnamed module of loader java.net.URLClassLoader @3a50b75c)
The script itself contains this:
someProperty = "foo"
println("Hello from custom script!")
Is there any way to work around this or is there an other way to run a custom action after the script was executed including having access to the data of the script?ilya.chernikov
01/24/2023, 12:54 PMkotlinc
.
Please, file an issue to our YT, and we'll try to consider support for it eventually.molikuner
01/24/2023, 1:43 PM