fridgepolice
fridgepolice

Reputation: 389

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

Upvotes: 0

Views: 359

Answers (1)

yole
yole

Reputation: 97338

You can't. As the documentation says, annotations targeting a property are not visible from Java (because Java does not have the concept of properties).

Upvotes: 3

Related Questions