I want to generate a very simply class that just contains extension functions, like e.g. following:
import com.my.app.SomeClass
fun SomeClass.function() {
}
What I try:
val packageName = ...
val annotatedClassName = annotatedElement.simpleName
val fileName = "${annotatedClassName}_KotArgsExtensions"
val extensionBuilder = FileSpec.builder(packageName, fileName)
val funSpecBuilder= FunSpec.builder("${SomeClass::class.java.simpleName}.function").build()
extensionBuilder.addImport(SomeClass::class.java.`package`.name, SomeClass::class.java.simpleName)
extensionBuilder.addFunction(funSpecBuilder)
What I get:
import com.my.app.SomeClass
fun `SomeClass.function`() {
}
Question
How I can I correctly create an extension function like fun SomeClass.function()
?
Use FunSpec.Builder.receiver()
:
val builder = FunSpec.builder("function")
.receiver(SomeClass::class)
.build()
println(builder.build().toString())
Will give you:
fun com.my.app.SomeClass.function() {
}