android - 在 kotlin 中定义 StringDef

标签 android kotlin annotations

我正在尝试在 kotlin 中定义一个 StringDef:

@Retention(AnnotationRetention.SOURCE)
@StringDef(NORTH, SOUTH)
annotation class FilterType {

    companion object {
        const val NORTH = "NORTH"
        const val SOUTH = "SOUTH"
    }
}

我认为上面的代码有问题。
// I can send anything to this method, when using my kotlin stringDef
private fun takeString(@DirectionJava.Direction filterType: String) {

我想要下面的 java 的 kotlin 等价物:
public class DirectionJava {

    public static final String NORTH = "NORTH";
    public static final String SOUTH = "SOUTH";

    @Retention(RetentionPolicy.SOURCE)
    @StringDef({
            NORTH,
            SOUTH,
    })
    public @interface Direction {

    }
}

从 kotlin 调用 java 定义的 StringDef 工作
// This works as expected, the Java-written string def 
// restricts what I can pass to this method
private fun takeString(@DirectionJava.Direction filterType: String) {

我哪里出错了,你如何在 Kotlin 中定义 StringDef?

最佳答案

根据jetbrains issue , Lint kotlin 中枚举注释的检查插件正在开发中,还不稳定。检查 android 支持注解,例如 @Nullable , @StringRes , @DrawableRes , @IntRange , ...(用java编写)工作正常,用户定义的枚举注释没有正确检查。所以,似乎我们应该在 java 中定义它们,然后在 kotlin 中使用。

关于android - 在 kotlin 中定义 StringDef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53777697/

相关文章:

c# - 在 C# 中为 android 制作应用程序?

android - 轮盘赌选择

gradle - IntelliJ Kotlin 多平台项目 Gradle 同步很长

kotlin - 检测kapt中的单例

java - 为持久性注释继承的属性

android - 点燃火 : Content browsing

java - 单独的字符串输入android

java - Kotlin 可以与 openjdk 一起使用吗?

java - 通过注解获取java bean属性值

java - 无法自动启动 bean tomcat IntelliJ