mercredi 7 octobre 2020

Kotlin Annotation Reflection to inject an argument to a function with a custom annotation

I'd love to create a new library for my purposes and am currently struggling with the technical approach needed to fulfill the requirements.

I want to have something like this:

class MyFoo {
  @Populate("myText")
  fun giveMeAValue(bar: Bar) {
    LOG.info(bar.message) // "myText"
  }
}

I'd like to hook on the function call of giveMeAValue or better said I want to hook logic on every function that is annotated with @Populate. If a call is being registered I'd like to execute some logic and pass a value for the bar parameter. Please ignore the fact that I might overwrite a value that has been passed already.

I was reading up for Reflection using Kotling (i.e. https://medium.com/tompee/kotlin-annotation-processor-and-code-generation-58bd7d0d333b) but could not find the thing I'd like to have here.

The hooking/proxy mechanism before the "real" function is being called. That's what I want to achieve.

Can anybody tell me what I am actually looking for? Is there a word for that concept or an article/guide describing this? I was not able to find anything. A code snippet/example would be awesome, too!





Aucun commentaire:

Enregistrer un commentaire