Can I apply one annotation to several targets in Kotlin?

According to the documentation: https://kotlinlang.org/docs/reference/annotations.html

You can apply multiple annotations to the same purpose of using the site, but is there a way to apply the same annotation to several purposes of using the site?

My use case decorates classes with SimpleXML annotations. To use immutable data class, you must annotate both the field and the constructor parameter:

data class Data( @field:Element(name = "ID") @param:Element(name = "ID") val id: Int, @param:Element(name = "TEXT") @field:Element(name = "TEXT") val text: String)

For data classes with many fields, you can easily get 3 times more annotations than the actual code, and it would be nice to eliminate duplication. This is especially annoying when you have to use complex type annotation ElementUnion, which can be several lines in itself.

+4
source share

Source: https://habr.com/ru/post/1688628/


All Articles