Default arguments, @JvmOverloads, and call function via reflection

Hello, I would like to ask, how call (by reflection) function with default parameters from java:

If I have function like:

@JvmOverloads
fun fun1(par1: Int = 1, par2: Int = 2, par3: Int = 3) { // }

So this will generate:

fun1()
fun1(par1)
fun1(par1, par2)
fun1(par1, par2, par3)

When I am invoking fun1 wihout parameters, it working …
And question is: How can I invoke, for example, fun1 only with par3 - witch function signature can I use?
Thank you very much.

for all the combinations, you need three method names.

@JvmOverloads
fun fun1(par1 : Int = 1, par2: Int = 2, par3: Int = 3) {...}
@JvmOverloads
fun fun2(par2: Int, par3: Int = 3)  = fun1(par2 = par2, par3 = par3)
fun fun3(par1: Int, par3: Int)  = fun1(par1 = par1, par3 = par3)
fun fun3(par3: Int)  = fun1(par3 = par3)

If you want the jvm-names in reversed order or any other order, you have bad luck and must implement it by yourself

1 Like

Thank you. In fact there are many classes which call API. Theese classes are CGLIBed on runtime, so I cannot make combination for every function with default values. I was afarid, that this will not be easy if even possiible.