Ace*_*Yin 9 annotations kotlin
比方说,我有一个带注释的Kotlin类:
@Entity @Table(name="user") data class User (val id:Long, val name:String)
Run Code Online (Sandbox Code Playgroud)
如何从@Table注释中获取name属性的值?
fun <T> tableName(c: KClass<T>):String {
// i can get the @Table annotation like this:
val t = c.annotations.find { it.annotationClass == Table::class }
// but how can i get the value of "name" attribute from t?
}
Run Code Online (Sandbox Code Playgroud)
Jay*_*ard 13
你可以简单地说:
val table = c.annotations.find { it is Table } as? Table
println(table?.name)
Run Code Online (Sandbox Code Playgroud)
注意,我使用了is
运算符,因为注释具有RUNTIME
保留,因此它是Table
集合中注释的实际实例.但以下适用于任何注释:
val table = c.annotations.find { it.annotationClass == Table::class } as? Table
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6310 次 |
最近记录: |