Android Kotlin注释-要求参数是特定类中的常量变量
我这里有一个函数Android Kotlin注释-要求参数是特定类中的常量变量,android,kotlin,annotations,Android,Kotlin,Annotations,我这里有一个函数过滤器 fun filter(category: String) { ... } 和一个包含许多常量字符串的类 object Constants { val CAT_SPORT = "CAT_SPORT" val CAT_CAR = "CAT_CAR" ... } 如何确保参数category是Constants中的常量字符串(或抛出警告) 我正在寻找类似@StringRes的东西 我知道Enum可以做到这一点,但现在不想进行代码重构。使用androi
过滤器
fun filter(category: String) {
...
}
和一个包含许多常量字符串的类
object Constants {
val CAT_SPORT = "CAT_SPORT"
val CAT_CAR = "CAT_CAR"
...
}
如何确保参数category
是Constants
中的常量字符串(或抛出警告)
我正在寻找类似@StringRes
的东西
我知道
Enum
可以做到这一点,但现在不想进行代码重构。使用androidx.annotation可以执行以下操作:
object Constants {
@Retention(AnnotationRetention.SOURCE)
@StringDef(CAT_SPORT, CAT_CAR)
annotation class Category
const val CAT_SPORT = "CAT_SPORT"
const val CAT_CAR = "CAT_CAR"
}
fun filter(@Constants.Category category: String) {
...
}
使用androidx.annotation可以执行以下操作:
object Constants {
@Retention(AnnotationRetention.SOURCE)
@StringDef(CAT_SPORT, CAT_CAR)
annotation class Category
const val CAT_SPORT = "CAT_SPORT"
const val CAT_CAR = "CAT_CAR"
}
fun filter(@Constants.Category category: String) {
...
}
也许你可以自己编程,使用注释处理器很容易。或者你可以自己编程,使用注释处理器很容易