Loading source at runtime

Greetings,

I have a need to have many source files that get compiled in memory and run at runtime.  I am successfully doing this with Groovy, but I am toying with the idea of switching to Kotlin.  From Jave, it would look something like this:

KotlinClass MyClass = new KotlinClass("MyClass.kotlin");

Method meth = MyClass.getMethod("myMethod", signature...);

meth.invoke(....);

So, the text file MyClass.kotlin contains the Kotlin source code to the class I wish to use.  I get a pointer to the method I seek in the class and execute it.  The ‘new KotlinClass(“MyClass.kotlin”)’ reads the source text, compiles it in memory, does not save it to a file, and returns a KotlinClass object which allows further use of the class.

If the answer is yes, I have a number of other questions regarding threads and re-loading.

Thank you.

Blake McBride

You can do so by bundling the Kotlin compiler with your program and running it to compile your source files. However, at this time there is no supported API for invoking the compiler (meaning that the API is very likely to change between updates of Koltin).

Thanks.  Just FYI, this feature, of Groovy, comes in very handy in large business applications.  It allows us to use Groovy as an extension language which can be modified without bringing the system down.  And since it is compiled, except for the initial load, it runs at full compiled speed.

1 Like

Yes, we know. To enable this kind of use, we plan to support the JSR-233 API for Kotlin.

Note that this is exactly how Kobalt works: call the Kotin compiler in order to compile `Build.kt` and introspect the resulting class files. It works great and takes about three seconds for each compilation. Not great but not insanely slow either. And Kobalt will only recompile if the build file is more recent than the class files, so most of the time, it's instantaneous

Invoking kotlinc has been stable so far but I’m aware it can change over a version, so I’m ready for that.

1 Like

should be JSR-223

Can anyone answer if this is supported now?

1 Like