Create Custom Dagger 2 Scope with Kotlin

删除回忆录丶 提交于 2020-08-24 06:10:19

问题


I'm trying to convert a Java code into Kotlin for custom dagger scope creation.

Here is Java code:

@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomScope {
}

Once converted into kotlin here is the result

@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME) annotation class CustomScope 

I have a type mismatch with @Retention(RetentionPolicy.RUNTIME).I have the following error message :Required Type is AnnotationRetention but RetentionPolicy type was found.

Also @interface seems to have been replaced.


回答1:


The Retention annotation class which you might have used is from the Kotlin's library (from the package kotlin.annotation).

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, 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.

So, just @Retention annotation will do too.



来源:https://stackoverflow.com/questions/48040243/create-custom-dagger-2-scope-with-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!