[英]Check annotation on a variable in kotlin data class
I have to check if a particular variable inside a kotlin data class has an annotation present or not.我必须检查 kotlin 数据 class 中的特定变量是否存在注释。
Annotation class注解class
annotation class Test(
val identifier: String
)
Data class数据 class
data class Player(
val name: String,
@property:Test("stance")
val stance: String,
@property:Test("check")
val check: String
)
I have to check if a particular variable inside this player object has the Test annotation present or not.我必须检查此播放器 object 中的特定变量是否存在测试注释。
fun execute(player: Player) {
//while this works, but i have to check for a specific variable
player::class.members.forEach{
val testAnnotation = it.findAnnotation<Test>()
if(testAnnotation != null) {
// DO something
}
}
I need to do something like
player.check.hasAnnotation<Test>()
}
Would appreciate any help here, TIA非常感谢这里的任何帮助,TIA
You should use ::
operator to create a member reference您应该使用
::
运算符来创建成员引用
player::check.hasAnnotation<Test>()
https://kotlinlang.org/docs/reflection.html#property-references https://kotlinlang.org/docs/reflection.html#property-references
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.