vendredi 19 avril 2019

Tip: How to filter data class properties by kotlin annotation

Kotlin annotation isn't the same as Java annotations. So work with Kotlin reflection requires a bit different way compare to classic java. Here you can find a way of filtering properties of Kotlin data class by Kotlin annotations

Implimentation of Annotation

@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class Returnable

Dummy Data class

data class DataClass(
    val property: String
    @Returnable
    val annotatedProperty: String
)

Exaplem of filter

DataClass("false","true")::class.members.filter {
     it.findAnnotation<Returnable>() != null
}





Aucun commentaire:

Enregistrer un commentaire