ian.shaun.thomas
02/06/2025, 6:12 PMdata class Test(val input:String) {
companion object {
val example = Test("Hello")
}
}
I can get as far as creating the data class and companion object but I'm not sure how to get the type Test
, I'm using Any
to get the code to generate but Any is the wrong type. It would be fine to just not have a type and maybe that is the solution but I'm not seeing a way to create a property without a type.
FileSpec.builder("", "Test")
.addType(
TypeSpec.classBuilder("Test")
.addModifiers(KModifier.DATA)
.primaryConstructor(
FunSpec.constructorBuilder()
.addParameter("input", String::class)
.build(),
)
.addProperty(
PropertySpec.builder("input", String::class)
.initializer("input")
.build(),
)
.addType(
TypeSpec.companionObjectBuilder()
.addProperty(
PropertySpec.builder("example", Any::class)
.initializer(CodeBlock.of("Test(\"Hello\")"))
.build(),
)
.build(),
)
.build(),
)
.build()
.writeTo(generatedOutputDir)
output:
public data class Test(
public val input: String,
) {
public companion object {
public val example: Any = Test("Hello")
}
}
jw
02/06/2025, 6:21 PMClassName
using the target package and type name.jw
02/06/2025, 6:21 PMClassName
first, and then use that to create the FileSpec
and TypeSpec
which both will accept `ClassName`sian.shaun.thomas
02/06/2025, 6:22 PMjw
02/06/2025, 6:22 PMian.shaun.thomas
02/06/2025, 6:26 PM