reposting from <#C0B8MA7FA|getting-started> : is t...
# scripting
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:
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.
Oh sorry, I found something:kotlin jsr223 local example artifact. Still looking for an example
scroll back 2-3 messages
thats a example using the new scripting in kotlin 1.3
also see the topic
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
There is no officially supported mechanism for accessing bytecode in memory. You can probably hack something similar to
, 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.
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.
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
to compile script and then use
to load class.
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
There is no thin version, but there is a relocated one -
. Unfortunately the there is no corresponding version of the
, so you cannot directly use it now. There is an issue about it -, and I plan to do something about it in the future.
I am thinking of using it with a shaded jar or dependencies. what version of guava are you using?
25.1, as far as I can tell.
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:;[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.
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.
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.