While upgrading Kotlin I noticed a difference in behaviour on ‘javaClass.enclosingClass’. This no longer seems to work from an extension. See the test below, it fails from version 2.0.0:
class EnclosingClassTest {
@Test
fun test() {
val enclosingClass = someExtension {}
// works in 1.9.25, but not in 2.0.0 or newer
assertEquals(EnclosingClassTest::class.java, enclosingClass)
}
}
fun <T> someExtension(call: () -> T): Class<*>? {
return call.javaClass.enclosingClass
}
My guess would be there were never guarantees that it behaves as you expected. I don’t think Kotlin specifies where exactly is the lambda located in the resulting bytecode.