if I'd like to retrieve all the classes marked wit...
# multiplatform
e
if I'd like to retrieve all the classes marked with a specific annotation, shall I use the kotlinx.serialization for that (for the descriptor)?
m
Are you talking some kind of multiplatform reflection?
e
yep
m
kotlinx.serialization uses findAssociatedObject
e
I wonder if I can exploit, at runtime, the descriptor generated at compile time
m
The descriptor is in the companion object, I don't think you can enumerate those
e
that
findAssociatedObject
looks to be only native and js?
m
On the JVM you can use plain reflection/classpath scanning I guess?
e
so I need 2 implementations 😕
m
yep
Overall I find multiplatform reflection to be a bit awkward, maybe you can use KSP or some other form of meta-programming instead?
e
yeah, someone mentioned that, but I have no idea where to start
m
KSP isn't that hard, calling
resolver.getSymbolsWithAnnotation()
will get you all your classes. Then use KotlinPoet to generate a top level list of all of them
There are a couple ksp samples out there. Maybe this one?
e
I need that at runtime
m
Ah oh yea then KSP won't help
e
what if I use KSP for getting all classes with the annotation I want, generating at compile time some metadata to be read in runtime later on?
m
Yep, this is exactly what I was suggesting
The metadata would be the KotlinPoet generated top level property containing the list of all your
KClass
You could dump to a JSON too but it's the same underlying mechanism
e
I'd need the list of all the classes, in third dependencies too.. do you think it'll work?
m
Yep, KSP scans the compile classpaths of your sources
e
eh, the problem is on native, there is no classpath over there
m
I would expect this to work
KSP looks in the .klibs or whatever
The Kotlin compiler knows about all those symbols
e
this is a library, do you think this will work on consumers side as well?
m
If your users add the KSP processor to their build, sure
Means you have to ship both the runtime library and the build-time KSP processor, like moshi for an example