Is it possible to have 2 annotation and generate 1 merge class based on the annotation in annotation processor

Let say I have 3 annotation class

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Combined (
   val id: String
)

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Response 

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Parameter

my data class:

@Combined("Request1")
@Parameter
data class param(
    val name: String
)

@Combined("Request1")
@Response
data class response(
    val data: String
)

I want to generate 1 class in annotation processor based on @Combined and similarity of value id in @Combine props. And the content of the class is based on @Parameter and @Response.

To make it clear, basically I want to group all @Combined class based on id. Then get the class that param and response then generate a class based on their property combined.

Is this possible?

Yes it is.

Using kapt you can querry all classes that have the Combined annotation and group them based on their field. After that you can check that you have 2 classes 1 with Request and 1 with the Parameter annoation.

After that you can simply generate a new file with the class you want. Kotlinpoet is a great tool for that.

Hi @Wasabi375 ! You’re everywhere in this forum!! Wish you have a great day. So basically it’s possible. thanks!

2 Likes