Kotlin data classes inspection from Annotation Processor

I’m trying to inspect data class at compile time.
What I need is to get a list of properties (filter them from other non-properties methods) in order to be able to pass them to the constructor.

But as annotation processor see Kotlin data class just like ordinary java class, I cannot properly determine properties and the order of them.
I tried to extract some information from generated @kotlin.Metadata annotation (with kotlin-reflect) library, but was unable to get ordered properties with corresponding java types.

Is there any guide on how to work with kotlin code FROM Annotation processor?
Are there any resources on how to use kotlin.Metadata annotation in compile time (and not runtime)?

It’s been several years, but if anyone happens upon this and wants a semi-solution, here is what I did (for kotlinpoet). I don’t think it works perfectly for every scenario and only works if you’re generating to kotlin.

You can find constructor information in the metadata, then use one of the constructors (dependent on your use case) and the properties needed are there. Whether or not they’re strongly ordered could matter for generating java code, but for kotlin code you can use named parameters in the constructor and circumvent the ordering entirely.

1 Like