https://kotlinlang.org logo
#announcements
Title
# announcements
t

Tuan Kiet

07/09/2019, 1:21 AM
Copy code
@Target(
    AnnotationTarget.FIELD,
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER
)
annotation class FieldName(
    val value: String
)

data class UserEntity(
    @FieldName("userName") val userName: String
)
somehow
UserEntity::userName.annotations
return an empty list. What did I do wrong?
c

Czar

07/09/2019, 5:26 AM
You're missing
AnnotationTarget.PROPERTY
. Without it the annotation is set on the Java field instead of Kotlin property:
println(UserEntity::class.java.declaredFields.find { it.name == "userName"}?.annotations?.map { it.annotationClass.jvmName })
t

Tuan Kiet

07/09/2019, 6:52 AM
I tried to add
AnnotationTarget.PROPERTY
,
UserEntity::userName.annotations
still return an empty list
c

Czar

07/09/2019, 8:58 AM
Screenshot_20190709_125756.png
t

Tuan Kiet

07/09/2019, 11:59 PM
I think I might discover a bug
c

Czar

07/10/2019, 5:56 AM
Looks like it's working for you too. What bug? Also, why do you annotate annotation's own value with itself?
Oh, I think you have put EntityA's annotation by mistake onto the annotation itself, and then EntityB's annotation onto EntityA 🙂
🤩 1
t

Tuan Kiet

07/10/2019, 9:17 AM
sorry my mistake
20 Views