Kotlin - how to get annotation attribute value

前端 未结 1 1688
傲寒
傲寒 2021-01-02 01:03

say, i have one Kotlin class with annotations:

@Entity @Table(name=\"user\") data class User (val id:Long, val name:String)

How can i get t

1条回答
  •  北海茫月
    2021-01-02 01:34

    You can simply:

    val table = c.annotations.find { it is Table } as? Table
    println(table?.name)
    

    Note, I used the is operator since the annotation has RUNTIME retention and therefore it is an actual instance of the Table annotation within the collection. But the following works for any annotation:

    val table = c.annotations.find { it.annotationClass == Table::class } as? Table
    

    0 讨论(0)
提交回复
热议问题