如何在 Kotlin 中使用 StringDef 或 IntDef?

Ely*_*lye 5 android annotations kotlin

参考https://developer.android.com/reference/android/support/annotation/StringDef https://developer.android.com/reference/android/support/annotation/IntDef

我可以轻松创建我的编译验证,将我的字符串参数限制为特定类型的字符串(在 Java 中)

例如

import android.support.annotation.StringDef;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.SOURCE;

@Retention(SOURCE)
@StringDef({
    "allow_one",
    "okay_two"
})
public @interface AllowedString { }
Run Code Online (Sandbox Code Playgroud)

所以如果我有

class TestAnnotation(@AllowedString private val name: String) {
    fun printName(@AllowedString name: String) {}
}
Run Code Online (Sandbox Code Playgroud)

当我编码时

val testAnnotation = TestAnnotation("not_allowed")
Run Code Online (Sandbox Code Playgroud)

Android Studio 将标记错误,not_allowed因为它不在列表中。

如果我将AllowedString注释界面转换为 Kotlin ,如下所示,它将不再起作用。为什么?

import android.support.annotation.StringDef
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy.SOURCE

@Retention(SOURCE)
@StringDef("allow_one", "okay_two")
annotation class AllowedString
Run Code Online (Sandbox Code Playgroud)

为什么?