vendredi 12 avril 2019

Retrieve kotlin annotations from java class

I have an interface which has annotation:

@Target(AnnotationTarget.PROPERTY)
annotation class Foo()

interface Bah {
    @Foo val prop: String
}

I'm implementing a jackson contextual deserializer, and I need to pick up this annotation from the methods in the interface.

   override fun createContextual(ctxt: DeserializationContext, property: BeanProperty?): JsonDeserializer<*> {
        val clzz = ctxt.contextualType.rawClass as Class<T>
        for (method in clzz.methods) {
            val anns = method.getAnnotationsByType(Foo::class.java)

ctxt.contextualType is a JavaType. I obtain clzz from it, which yields me a class of type Bah (i.e. the interface). I can iterate the methods, which include "prop"; however, prop has no annotations.

It DOES work if I modify the annotation site to look like this:

interface Bah {
    val prop: String
        @Foo() get

However, that's ugly. How can I modify things so that I can retrieve from the interface property directly?

Thanks





Aucun commentaire:

Enregistrer un commentaire