I am trying to generate one kotlin class using kotlin poet library which should have one function and one inner static class as given below.
class SampleClass{
class TestClass{
lateinit var id: String
lateinit var name: String
}
fun function1(init: TestClass.() -> Unit) {
val trackPhoneNumberClicked = TestClass().apply(init)
val event = Event.Builder.from(testData.getTestDataById("testdataid")!!)
.apply {
addProperty("id", trackPhoneNumberClicked.id)
addProperty("name", trackPhoneNumberClicked.name)
}
.build()
}
}
I can generate SampleClass and inner class TestClass but I am not able to create function1 with this argument and body.
github like of kotlin poet library. https://github.com/square/kotlinpoet
Can anyone provide any solution for this?
I got the answer that how we can generate function1(init: TestClass.() -> Unit)
this type of argument in function. We need to use LambdaTypeName class of kotlin poet lib.
val buildParameter = ParameterSpec.builder(FUNCTION_PARAMETER_NAME, LambdaTypeName.get(ClassName("", CLASS_TYPE_NAME), returnType = Unit::class.asTypeName())).build()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.