Description
I am trying to use Kotlin Scripting inside a Quarkus Application. However the Quarkus RuntimeClassloader seems to be insufficient to load the classes into the Scripting Engine. That is basically because it only loads classes that has been indexed at startup time.
I also filed a ticket with quarkus:
quarkusio/quarkus#43271 (comment)
And i created a minimal reproducer for the problem:
https://github.com/ckosmowski/kotlin-script-reproducer
1.) Start the Main class directly -> The script works. It outputs "Hello World!" and "A Model From Outside"
2.) run quarkus dev -> The output will be "Hello World" and a longish output with the message " ERROR Unresolved reference 'printName'. " at its end.
Something to add, there is a way that you can usually replace the used classloader like this:
val result = host.eval(StringScriptSource("""
printName()
""".trimIndent()), ScriptWithMavenDepsConfiguration, ScriptEvaluationConfiguration {
implicitReceivers(modelFromOutside)
jvm {
baseClassLoader(FilteringClassLoader(Thread.currentThread().contextClassLoader, listOf("org.example", "java.lang")))
}
})
FilterClassLoader:
class FilteringClassLoader(
parent: ClassLoader,
val allowedPackages: List<String>
) : ClassLoader(parent) {
override fun loadClass(name: String, resolve: Boolean): Class<*> {
if (allowedPackages.any { name.startsWith(it) }) {
return super.loadClass(name, resolve)
}
throw ClassNotFoundException("Class $name is not allowed")
}
}
I hoped that this does the trick but however this works in the plain java version but not with quarkus dev.
Is there a way that you kotlin people could assist here, or work together with quarkus?