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

以下是Java代码:

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

此外,@interface似乎已被替换.

推荐答案

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

它需要枚举类型为AnnotationRetention的属性.因此,您可以这样做:

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

因此,只需@Retention个注释也可以.

Kotlin相关问答推荐

try 一次性插入多条记录时,JOOQ连接为空错误

TzdbZoneRulesProvider 在 java.time 中不工作

如何使用 Hilt 注入应用程序:ViewModel 中的上下文?

OnClickListener 未在 ConstraintLayout 上触发

Swift vs Kotlin 在排序数组上的表现

将 Kotlin 类属性设置器作为函数引用

Kotlin 1.2.21 + SimpleXml 2.3.0 - consume List error (must mark set get method)

使用最新的 com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2 库时未找到 RxTextView 和其他小部件

Kotlin 枚举中的循环引用

在构造函数中仅注入某些参数

runInTransaction 块内的挂起方法

requireNotNull vs sure !! 操作符

在kotlin中,如何模拟封装回调函数?

Kotlin:具有多个不同类型设置器的单个属性

用于代码生成的ANTLR工具版本4.7.1与当前运行时版本4.5.3不匹配

通过在 kotlin-gradle 中使用子项目Unresolved reference: implementation

Kotlin - 错误:Could not find or load main class _DefaultPackage

比较Kotlin的NaN

Kotlin中对象和数据类的区别是什么?

如何在 spring-boot Web 客户端中发送请求正文?