Files
kotlin-fork/compiler/testData/codegen/box/reflection/enclosing/lambdaInClassObject.kt
T
Mads Ager a8e2893494 JVM_IR: Output EnclosingMethod attribute.
This works in many cases, however, it is incomplete since there
are cases where classes are extracted to top-level and therefore
reparented. Therefore, we lose the information about the function
class are nested inside.
2019-04-10 20:50:03 +02:00

30 lines
905 B
Kotlin
Vendored

// TARGET_BACKEND: JVM
// WITH_REFLECT
class O {
companion object {
// Currently we consider <clinit> in class O as the enclosing method of this lambda,
// so we write outer class = O and enclosing method = null
val f = {}
}
}
fun box(): String {
val javaClass = O.f.javaClass
val enclosingMethod = javaClass.getEnclosingMethod()
if (enclosingMethod != null) return "method: $enclosingMethod"
val enclosingConstructor = javaClass.getEnclosingConstructor()
if (enclosingConstructor != null) return "constructor: $enclosingConstructor"
val enclosingClass = javaClass.getEnclosingClass()
if (enclosingClass?.getName() != "O") return "enclosing class: $enclosingClass"
val declaringClass = javaClass.getDeclaringClass()
if (declaringClass != null) return "anonymous function has a declaring class: $declaringClass"
return "OK"
}