Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在kotlin中注释注释属性?_Kotlin_Annotations - Fatal编程技术网

如何在kotlin中注释注释属性?

如何在kotlin中注释注释属性?,kotlin,annotations,Kotlin,Annotations,我正在尝试在批注属性上放置批注。我的理解是,我应该能够在代码中访问它——但我不能。我错过了什么 package com.example.annotations import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import kotlin.reflect.full.findAnnotation class AnnotationIssueTest { @Test fun testAn

我正在尝试在批注属性上放置批注。我的理解是,我应该能够在代码中访问它——但我不能。我错过了什么

package com.example.annotations

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import kotlin.reflect.full.findAnnotation

class AnnotationIssueTest {
    @Test
    fun testAnnotations() {
        Assertions.assertNotNull(MyTestAnnotation::value.findAnnotation<PropertyMarker>())
    }

    @Test
    fun testRegularClass() {
        Assertions.assertNotNull(MyTestClass::value.findAnnotation<PropertyMarker>())
    }
}


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

annotation class MyTestAnnotation(
        @PropertyMarker val value: String
)

class MyTestClass(
        @PropertyMarker val value: String
)

当我运行给定的测试时,testAnnotations失败,而testRegularClass通过。这是一个bug还是我做错了什么?

出于某种原因,注释属性的注释没有进入字节码。但是,您可以改为注释属性获取程序:

class AnnotationIssueTest {
    @Test
    fun testAnnotations() {
        Assertions.assertNotNull(MyTestAnnotation::value.getter.findAnnotation<PropertyMarker>())
    }

    @Test
    fun testRegularClass() {
        Assertions.assertNotNull(MyTestClass::value.getter.findAnnotation<PropertyMarker>())
    }
}


@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY_GETTER)
annotation class PropertyMarker

annotation class MyTestAnnotation(
        @get:PropertyMarker val value: String
)

class MyTestClass(
        @get:PropertyMarker val value: String
)

由于某些原因,注释属性的注释不会进入字节码。但是,您可以改为注释属性获取程序:

class AnnotationIssueTest {
    @Test
    fun testAnnotations() {
        Assertions.assertNotNull(MyTestAnnotation::value.getter.findAnnotation<PropertyMarker>())
    }

    @Test
    fun testRegularClass() {
        Assertions.assertNotNull(MyTestClass::value.getter.findAnnotation<PropertyMarker>())
    }
}


@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY_GETTER)
annotation class PropertyMarker

annotation class MyTestAnnotation(
        @get:PropertyMarker val value: String
)

class MyTestClass(
        @get:PropertyMarker val value: String
)

谢谢这当然管用,但很难看。我发现了一个相关的错误记录,但似乎没有任何行动。谢谢这当然管用,但很难看。我发现了一个相关的错误记录,但似乎没有任何行动。