We have a wrapper class for checking the Android SDK version, and it uses the ChecksSdkIntAtLeast
annotation. Here's a trimmed example of the class with just one getter and the logging removed:
class DeviceApi(
private val deviceOsVersion: Int
) {
@get:ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M)
val isApi23AndAbove get() = deviceOsVersion >= Build.VERSION_CODES.M
}
We have tests for the getter itself, but I was hoping to write tests to verify that the getter also has the annotation in place with the correct value, to avoid the compiler warnings that show up if it isn't included. However, no matter what I try, I can't seem to find the annotation via reflection.
Here's two basic examples that work if I do this for a Kotlin annotation class
annotation that just don't find the public @interface
annotations provided by androidx.annotation
:
@Test
fun kotlin_reflection_example() {
assertThat(DeviceApi::isApi23AndAbove.getter.hasAnnotation<ChecksSdkIntAtLeast>()).isTrue
}
@Test
fun java_reflection_example() {
assertThat(DeviceApi::isApi23AndAbove.javaGetter!!.getAnnotation(ChecksSdkIntAtLeast::class.java)).isNotNull
}
I've also tried various examples I've seen online for looking for members and methods from the class and debugged to inspect the state of various things, but I've been unable to get any of the code to see the annotation. I want to know if I'm missing something obvious or if I found a scenario that just isn't supported by kotlin-reflect
- and if so, if there's an alternative.
Aucun commentaire:
Enregistrer un commentaire