Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For some reason Kotlin Reflect does see my custom annotation on member property but doesn't see Jackson one. Though Jackson annotation is detected and used by Jackson itself. Minimum snippet (run in REPL thus Line_5$A classname):

annotation class A
 
 class C {
     @A
     @com.fasterxml.jackson.annotation.JsonProperty
     var x: Int = 0
 }
 
 C::class.memberProperties.forEach { println(it.annotations.map{it.annotationClass}) }

Output: [class Line_5$A]

Expected: [class Line_5$A, class com.fasterxml.jackson.annotation.JsonProperty]

How could I have access to Jackson annotation here?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

Ok, found out that Jackson annotation was attached to Java field, not Kotlin property. So this workaround works:

println(C::class.memberProperties.first().javaField!!.declaredAnnotations.get(0).annotationClass)

Output: class com.fasterxml.jackson.annotation.JsonProperty

UPD: And if data class is used then this:

import kotlin.reflect.full.primaryConstructor
 
 data class C (
     @com.fasterxml.jackson.annotation.JsonProperty
     var x: Int = 0
 )
 
 val prop = C::x
 println(C::class.primaryConstructor
     ?.parameters
     ?.find { it.name == prop.name }
     ?.annotations
     ?.first()
     ?.annotationClass
 )

Output: class com.fasterxml.jackson.annotation.JsonProperty


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...