mercredi 22 mars 2023

Context parameter (in context receivers functionality) not showing via reflection

Via reflection, I need to distinguish between a method with a context and a method without a context (Kotlin 1.8.0).

Let's say we have following dummy context and a class with two functions - one with the context, second without.

  class Context(val a: Int)

  class Foo {
    context (Context)
    fun bar() = a
    
    fun baz() = 1
  }

In runtime, we don't know which method is which, so let's try to distinguish them via reflection.

  val foo = Foo()

  val barMethod = foo::class.memberFunctions.first { it.name == "bar" }
  println("barMethod has ${barMethod.parameters.size} parameter(s)")

  val bazMethod = foo::class.memberFunctions.first { it.name == "baz" }
  println("bazMethod has ${bazMethod.parameters.size} parameter(s)")

The problem is, both return they have 1 parameter (and they are identical in all other public properties). But obviously, when called, bar will fail if provided only one parameter. If called as expected, it works.

  // println(barMethod.call(foo))        -- fails
  println(bazMethod.call(foo))

  with (Context(5)) {
    println(barMethod.call(foo))
  }
  println(barMethod.call(foo, Context(6))

When debugging, I found, there actually is an internal property descriptor on barMethod (KCallableImpl) which contains contextReceiverParameters property correctly showing 1 for barMethod and 0 for bazMethod. But it's not accessible.

Any idea, if there's any supported way of retrieving information about context receivers via reflection, or it simply hasn't been implemented yet?





Aucun commentaire:

Enregistrer un commentaire