使用Kotlin创建自定义Dagger 2示波器 [英] Create Custom Dagger 2 Scope with Kotlin
问题描述
我正在尝试将Java代码转换为Kotlin以创建自定义匕首作用域。
I'm trying to convert a Java code into Kotlin for custom dagger scope creation.
这是Java代码:
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomScope {
}
一旦转换为Kotlin,这里就是结果
Once converted into kotlin here is the result
@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME) annotation class CustomScope
我与 @Retention(RetentionPolicy.RUNTIME)
的类型不匹配。我有以下错误消息:Required Type是AnnotationRetention但RetentionPolicy类型
I have a type mismatch with @Retention(RetentionPolicy.RUNTIME)
.I have the following error message :Required Type is AnnotationRetention but RetentionPolicy type was found.
@interface似乎也已被替换。
Also @interface seems to have been replaced.
推荐答案
您可能使用的 Retention
注释类来自Kotlin的库(来自软件包 kotlin.annotation
)
The Retention
annotation class which you might have used is from the Kotlin's library (from the package kotlin.annotation
).
它期望枚举类型为 A的属性nnotationRetention
。因此,您可以执行以下操作:
It expects a property of the enum type AnnotationRetention
. So, you can do something like this:
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class CustomScope
Btw,如果您查看 Annotations.kt
文件,您会看到 Retention
注释将采用默认属性 AnnotationRetention.RUNTIME
当您不传递任何内容时。
Btw, if you look at the Annotations.kt
file, you will see that that the Retention
annotation will take the default property AnnotationRetention.RUNTIME
when you don't pass anything to it.
因此,只需 @Retention
批注也是。
这篇关于使用Kotlin创建自定义Dagger 2示波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!