Define StringDef in kotlin
I'm trying to define a StringDef in kotlin:
@Retention(AnnotationRetention.SOURCE)
@StringDef(NORTH, SOUTH)
annotation class FilterType {
companion object {
const val NORTH = "NORTH"
const val SOUTH = "SOUTH"
}
}
I think something is wrong in the code above.
// I can send anything to this method, when using my kotlin stringDef
private fun takeString(@DirectionJava.Direction filterType: String) {
I want the kotlin equivalent of the java below:
public class DirectionJava {
public static final String NORTH = "NORTH";
public static final String SOUTH = "SOUTH";
@Retention(RetentionPolicy.SOURCE)
@StringDef({
NORTH,
SOUTH,
})
public @interface Direction {
}
}
Calling the java defined StringDef works from kotlin
// This works as expected, the Java-written string def
// restricts what I can pass to this method
private fun takeString(@DirectionJava.Direction filterType: String) {
Where have I gone wrong, how do you define a StringDef in Kotlin?
Solution 1:
According to jetbrains issue, Lint
check plugin for enumerated annotations in kotlin is under development and is not stable yet. Checking android support annotations such as @Nullable
, @StringRes
, @DrawableRes
, @IntRange
, ... (which are written in java) works fine and user defined enumerated annotations are not checked properly. So, it seems that we should define them in java then use in kotlin.