lundi 20 février 2023

Get annotation java annotation from parent interface in Kotlin

I declare a java annotation.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface JavaAnnotation{}

I use this java annotation in kotlin interface

Problem.kt

interface IMyClass {
        @JavaAnnotation
        fun method() {}
    }

class MyClass : IMyClass{
    override fun method() {}
}

class Problem {
    @Test
    internal fun shouldFindAnnotationFromParentInterface() {
        val myClass = MyClass()
        Assertions.assertTrue(myClass::class.declaredFunctions.find { it.name == "method" }!!.annotations.isEmpty().not())
    }
}

I see that java annotation uses @Inherited. I want to find the java annotation on the Kotlin MyClass::method. I get an empty array.

Could you help me? Thanks





Aucun commentaire:

Enregistrer un commentaire