Files
kotlin/compiler/testData/codegen/boxWithStdlib/reflection/lambdaClasses/parameterNamesAndNullability.kt
Alexander Udalov df935f5bb7 Support reflection on lambdas and function expressions
Write a special annotation containing the bytes for the Callable protobuf
message and deserialize it at runtime properly

 #KT-9005 Fixed
2015-09-03 21:43:58 +03:00

51 lines
1.2 KiB
Kotlin
Vendored

import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
import kotlin.test.assertNull
fun lambda() {
val f = { x: Int, y: String? -> }
val g = f.reflect()!!
// TODO: maybe change this name
assertEquals("<anonymous>", g.name)
assertEquals(listOf("x", "y"), g.parameters.map { it.name })
assertEquals(listOf(false, true), g.parameters.map { it.type.isMarkedNullable })
}
fun funExpr() {
val f = fun(x: Int, y: String?) {}
val g = f.reflect()!!
// TODO: maybe change this name
assertEquals("<no name provided>", g.name)
assertEquals(listOf("x", "y"), g.parameters.map { it.name })
assertEquals(listOf(false, true), g.parameters.map { it.type.isMarkedNullable })
}
fun extensionFunExpr() {
val f = fun String.(): String = this
val g = f.reflect()!!
assertEquals(KParameter.Kind.EXTENSION_RECEIVER, g.parameters.single().kind)
assertEquals(null, g.parameters.single().name)
}
fun customFunction() {
val f = object : Function<String> {}
assertNull(f.reflect())
}
fun box(): String {
lambda()
funExpr()
extensionFunExpr()
customFunction()
return "OK"
}