reposting from <#C0B8MA7FA|getting-started> : is t...
# scripting
o
reposting from #getting-started : is there a lib/functionality in kotlin to get a class compiled from string? something like this project but for kotlin: https://github.com/trung/InMemoryJavaCompiler
k
Not really what I meant simple smile . With scripting you can run strings as Kotlin code at runtime, and maybe that is a solution to the actual problem you want to solve.
o
Oh sorry, I found something:kotlin jsr223 local example artifact. Still looking for an example
n
scroll back 2-3 messages
thats a example using the new scripting in kotlin 1.3
also see the topic
o
the example above is for executing a script, what I am looking for it to load a class into current class loader so jvm can use it
so I need to get bytecode of kotlin class or generated class
like a plugin mechanism
i
There is no officially supported mechanism for accessing bytecode in memory. You can probably hack something similar to
kotlin.script.experimental.jvmhost.impl.KJvmCompilerImpl
, but we will not guarantee the stability of the internal APIs. I’d recommend you to create an issue in the YT about it, and we’ll consider such feature for the future releases.
o
I managed to do it with the rc. Will open a ticket. Does the compiler supports also java files? I want to allow users both languages.
i
No, java is not supported.
BTW, I just realized that my answer above is incomplete. While an access to the bytecode is not in the public API, loadable compiled script class is. So you can use
JvmScriptCompiler
to compile script and then use
CompiledScript.getClass
to load class.
o
Ok I did something similar I think. Will post a gist soon
@ilya.chernikov just found out that the compiler artifact bundle many dependencies in it. Is the a thin version? We hade class incompatibility with guava
i
There is no thin version, but there is a relocated one -
kotlin-compiler-embeddable
. Unfortunately the there is no corresponding version of the
kotlin-scripting-jvmhost
, so you cannot directly use it now. There is an issue about it - https://youtrack.jetbrains.com/issue/KT-27382, and I plan to do something about it in the future.
o
I am thinking of using it with a shaded jar or dependencies. what version of guava are you using?
i
25.1, as far as I can tell.
o
from digging inside a bit more, the problem looks a bit more subtle. The exception I get is
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.Lists.asList(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/util/List;
However, it seems that this method exists in all guava versions since 7 or so. It seems like your omitting methods that you're not using.
i
Yes, the compiler is proguarded too. I’m going to publish embeddable compiler compatible scripting in some near future, but for a moment the best would be to separate script compilation into an isolated classloader.
o
Its not a good solution for my use case because I want the classes loaded into regular classloader. However I used shaded jar and it solved the issue.