5

Referring to https://developer.android.com/reference/android/support/annotation/StringDef https://developer.android.com/reference/android/support/annotation/IntDef

I could easily create my compile validation which limits my String parameter to specific type of String (in Java)

e.g.

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 { }

So if I have

class TestAnnotation(@AllowedString private val name: String) {
    fun printName(@AllowedString name: String) {}
}

And when I code

val testAnnotation = TestAnnotation("not_allowed")

Android Studio will flag error on the not_allowed as it is not in the list.

If I convert to Kotlin the AllowedString annotation interface as below, it doesn't works anymore. Why?

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

Why?

Elye
  • 53,639
  • 54
  • 212
  • 474
  • 1
    Seems to be a known issue: https://discuss.kotlinlang.org/t/intdef-and-stringdef-not-being-checked-at-compile-time/7029, https://discuss.kotlinlang.org/t/resource-annotations-and-intdef-and-stringdef/1679, https://stackoverflow.com/questions/37833395/kotlin-annotation-intdef – TheWanderer Oct 17 '18 at 01:44

0 Answers0