0

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

1
  • well... if val prop: String @Foo() get works (so basically you added AnnotationTarget.PROPERTY_GETTER?), then you could also just use @get:Foo val prop : String ... still the same "problem"... but maybe you like that more? Commented Apr 12, 2019 at 12:46

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

OK - so then what should it be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.