samedi 19 octobre 2019

How to add annotation on main constructor parameters in kotlin

Having the following class:

data class TestMsg(
    @Parse(";") 
    val someArray: Array<String>
)

And trying to get the annotation with

TestMsg::class.primaryConstructor!!.parameters.forEach{
    println(it.findAnnotation<Parse>())
}

There is no annotation found. I had to move the annotation front of the parameter for make it working

data class TestMsg(
    @Parse(";") val someArray: Array<String>
)

is it a parsing error of jetbrains or is it the normal behavior?


EDIT

You can find the annotation right here:

@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.TYPE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class Parse(
    val delimiter: String
)




Aucun commentaire:

Enregistrer un commentaire