https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
e

elect

10/24/2023, 2:32 PM
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

mbonnin

10/24/2023, 2:34 PM
Are you talking some kind of multiplatform reflection?
e

elect

10/24/2023, 2:34 PM
yep
m

mbonnin

10/24/2023, 2:35 PM
kotlinx.serialization uses findAssociatedObject
e

elect

10/24/2023, 2:35 PM
I wonder if I can exploit, at runtime, the descriptor generated at compile time
m

mbonnin

10/24/2023, 2:36 PM
The descriptor is in the companion object, I don't think you can enumerate those
e

elect

10/24/2023, 2:36 PM
that
findAssociatedObject
looks to be only native and js?
m

mbonnin

10/24/2023, 2:37 PM
On the JVM you can use plain reflection/classpath scanning I guess?
e

elect

10/24/2023, 2:37 PM
so I need 2 implementations 😕
m

mbonnin

10/24/2023, 2:37 PM
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

elect

10/24/2023, 2:38 PM
yeah, someone mentioned that, but I have no idea where to start
m

mbonnin

10/24/2023, 2:41 PM
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

elect

10/24/2023, 2:41 PM
I need that at runtime
m

mbonnin

10/24/2023, 2:42 PM
Ah oh yea then KSP won't help
e

elect

10/24/2023, 6:58 PM
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

mbonnin

10/24/2023, 9:00 PM
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

elect

10/24/2023, 9:05 PM
I'd need the list of all the classes, in third dependencies too.. do you think it'll work?
m

mbonnin

10/24/2023, 9:07 PM
Yep, KSP scans the compile classpaths of your sources
e

elect

10/24/2023, 9:21 PM
eh, the problem is on native, there is no classpath over there
m

mbonnin

10/24/2023, 9:32 PM
I would expect this to work
KSP looks in the .klibs or whatever
The Kotlin compiler knows about all those symbols
e

elect

10/24/2023, 11:10 PM
this is a library, do you think this will work on consumers side as well?
m

mbonnin

10/25/2023, 8:24 AM
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
3 Views