我正在尝试将Java代码转换为Kotlin以创建自定义匕首。
这是Java代码:
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomScope {
}
一旦转换成kotlin,结果就是
@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME) annotation class CustomScope
我的类型与@Retention(RetentionPolicy.RUNTIME)
不匹配。我有以下错误消息:必需类型是AnnotationRetention但找到了RetentionPolicy类型。
@interface似乎已被替换。
答案 0 :(得分:21)
您可能使用的Retention
注释类来自Kotlin的库(来自包kotlin.annotation
)。
它需要枚举类型AnnotationRetention
的属性。所以,你可以这样做:
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class CustomScope
顺便说一句,如果查看Annotations.kt
文件,当您没有向其传递任何内容时,您会看到Retention
注释将采用默认属性AnnotationRetention.RUNTIME
。 / p>
因此,只有@Retention
注释也会这样做。