使用Kotlin创建自定义Dagger 2范围

时间:2017-12-31 05:23:31

标签: java android kotlin dagger-2

我正在尝试将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似乎已被替换。

1 个答案:

答案 0 :(得分:21)

您可能使用的Retention注释类来自Kotlin的库(来自包kotlin.annotation)。

它需要枚举类型AnnotationRetention的属性。所以,你可以这样做:

@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class CustomScope

顺便说一句,如果查看Annotations.kt文件,当您没有向其传递任何内容时,您会看到Retention注释将采用默认属性AnnotationRetention.RUNTIME。 / p>

因此,只有@Retention注释也会这样做。