I've heard reflection is limited on non-JVM platfo...
# multiplatform
c
I've heard reflection is limited on non-JVM platforms. I'd like to find all top-level functions annotated with a given interface, is it possible on all platforms? I know how to do it on the JVM, but I have no idea how to do that for JS or Native.
đź‘€ 1
c
Reflection is limited on other platforms, but the kind of code-scanning you’re referring to is almost certainly even beyond the scope of what’s available in
kotlin.reflect
. Libraries like ClassGraph that do this kind of scanning on JVM don’t usually use the Java reflection APIs, but instead directly parse the compiled classfiles to gather that information. ClassGraph also has support for scanning the classpath at compile-time and caching the necessary information, so it can work with AOT compilation like Graal. A similar approach is likely necessary here. KSP might be able to scan for instances of an interface, then you can generate code with KotlinPoet to wire everything up for you. But I don’t think a runtime solution is possible
plus1 1
c
Hm, compile-time is fine 🤔
What about the opposite problem: if I knew the fully qualified name of a top-level parameterless function, would it be possible to call it on all platforms?
f
No, that is only possible on the JVM
m
But if you know all possible methods already at compile time then you could also generate some code to call them based on some parameter.
x
Another way, you can use Objective-C NSClassFromString and respondsToSelector in iosMain source set