jeudi 5 décembre 2019

In Kotlin, restrict annotation target based on the type of the property

In Kotlin, as in Java, we can restrict the annotation to only be used in specific kinds of elements, with the @Target meta-annotation (ex. @Target(AnnotationTarget.VALUE_PARAMETER) so the annotation can only be applied to value parameters).

My use case is that I want to validate properties using reflection (ex. @MustNotBeEmpty name: String), and I'd like to restrict the kinds of annotations based on the type of property (ex. @MustBePositive number: Int can only be applied to "Int" properties). Comments about the feasibility of reflection-based validation are welcome as well.

Is there any way that this can be achieved in compilation time, or will I need to check this in runtime?





Aucun commentaire:

Enregistrer un commentaire