Kotlin JS full reflection API

Will Kotlin JS target get full reflection API support anytime soon?

I’m planning to write a tool that would currently use standalone Kotlin compiler (JVM) to create a program generating serialization code for JS target.

If JS target would support full reflection API it would render such (hacky) approach obsolete as I would be able to create object mapper directly in JS target code (well it wouldn’t generate code statically like in approach above, but performance isn’t an issue).

So is full reflection API support planned to be implemented in the near future (in few following months)?

4 Likes

We are going to support full reflection in Kotlin/JS in the future but now we don’t have any ETA about it, so it will not be implemented in near future.

Regarding the serialization issue – we already work on the solution for both JVM and JS. You can find more information in this post and checkout and play with current implementation from here.

2 Likes

Great to hear full reflection is coming to JS, please consider for a poll on desired features next time one is held.

Waiting for this one.

Kotlin JS does not seem to be getting much love… has there been any progress on this?

2 Likes

In one case I found, current Kotlin JS reflection has a different behavior from JVM.

Look here: https://stackoverflow.com/questions/51418597/different-behavior-of-kclass-between-kotlin-jvm-and-kotlin-js

Hello, pls try our kotlin2js reflection plugin, we need a feedback

We have now returned to our project and will try the use of your plugin… however there is no example for use with kotlin gradle script, only with groovy.
Not sure what to do with the

kotlin2JsReflection {
    generatedSourcesDir = file("${project.buildDir}/generated/decembrist")
}

block…as this will not run the same way in kotlin-dsl. The other additions to gradle seem feasible so far, but we are working multi-platform, as the use is within a multiplatform library that uses the reflection. It seems the JSReflection plugin specifically uses the js-kotlin plugin, and cannot work with multiplatform.

I’m also waiting for this.

Is there’s any estimate when the Kotlin/JS reflection will be worked on? Almost two years have since been passed since the original “we are going to work on it” post. :frowning:

While kotlinx.serialization is good for serializating things on the JS target, Reflection is also used for a lot of other things, which currently aren’t possible to do in Kotlin/JS due to the lack of reflection.

Even at least “more than basic” reflection support would be cool (getting members from a class, getting annotations in members/classes, calling members).

I think the biggest challenge is the lack of standardization of such a feature in Javascript. It is easy to add things to an object prototype (such as annotations), but there isn’t any standard way to mark something as annotation/metadata. It is not clear that there is a standard in Javascript. Newer versions of javascript do support reflection, so that should be available. Again, there is no single way in which packages exist, so loading a class based upon name is actually not straightforward (and may involve using require which is merely an optional library). As there is plenty to do for the team I can see why no-one has been in a rush to try to provide even a partial implementation. Btw. even serialization (when reflection based) needs the equivalent of Class.forName although it may assume it to be a Kotlin class and that it can be resolved locally in the standard Kotlin way (it needs that capability anyway for the transpiler to be able to create objects).

Kotlin team, please fix/actualize reflection API reference. There are a lot of members marked as supported, but in fact they are not.

@leaf, we’re aware about this problem with documentation and looking for the way how to solve it. You can follow its progress here: https://youtrack.jetbrains.com/issue/KT-32186.