检查 kotlin 数据中变量的注释 class

Check annotation on a variable in kotlin data class

我必须检查 kotlin 数据中的特定变量 class 是否存在注释。

注释class

annotation class Test(
    val identifier: String
)

数据class

data class Player(
    val name: String,
    @property:Test("stance")
    val stance: String,
    @property:Test("check")
    val check: String
)

我必须检查此播放器对象中的特定变量是否存在测试注释。

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>()

}

在此感谢您的帮助,TIA

您应该使用 :: 运算符来创建成员引用

player::check.hasAnnotation<Test>()

https://kotlinlang.org/docs/reflection.html#property-references