jeudi 9 juin 2022

How to read kotlin annotation

I have annotation classes

annotation class Endpoint(val name: String)

@Target(AnnotationTarget.TYPE)
annotation class JsonObjectKey(val name: String)

These are used like so

@Endpoint("my-endpoint")
fun myEndpoint(): @JsonObjectKey("key") String {
    return "value"
}

With a method: java.lang.reflect.Method object representing myEndpoint, I am able to access the Endpoint-Annotation, but I fail to access the JsonObjectKey-Annotatation.

method.annotations only contains Endpoint

method.annotatedReturnType.annotations is empty

How to read JsonObjectKey (key in this scenario)? Intention is to generate JsonObject {"key": "value"}





Aucun commentaire:

Enregistrer un commentaire