Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get default value of kotlin fun parameter in annotation processing phase

Background: I have an annotation processor that builds retrofit interfaces by scanning spring annotations on controllers. I have it set up to work in either kotlin or java based spring applications, and it can generate either kotlin or java retrofit client interfaces.

Question: When running it against a kotlin based spring application, is there a way to pick up a default value on a controller function, whether reflectively or through some other means?

e.g. the controller function looks something like

@RequestMapping("/foo")
fun getSomething(@RequestParameter foo: String = "bar") {
...

}

and I want to be able to generate a retrofit interface method that looks something like

fun getSomething(@Header foo: String = "bar")

I am aware that the compiler under the hood actually creates multiple override methods for the jvm: https://discuss.kotlinlang.org/t/retrieve-default-parameter-value-via-reflection/7314

But I'm wondering if there's a way to capture these defaults during the annotation processing phase or if I just have to live without defaults in the generated kotlin client.

Long story short - is there a workaround that would let me capture these defaults?

like image 747
StormeHawke Avatar asked Oct 15 '22 14:10

StormeHawke


1 Answers

It would seem that you cannot do that, sadly :( https://discuss.kotlinlang.org/t/kotlin-reflection-and-default-values/2254 the idea is interesting though. I'm not sure if it's desirable to analyze bytecode for this?

It kind of also makes sense because the default value can also come from things like constants, which in turn can be calculated values. If the default param is set through a calculated constant there's no certain way to know what it will be before it's loaded on runtime.

like image 186
Rohde Fischer Avatar answered Oct 29 '22 00:10

Rohde Fischer