Reputation: 315
I want to generate a kotlin class definition, this class implements an customized interface, the target class definition as below:
data class TemplateState(val data: String) : ContractState {
}
I used below poet code to generate it except the interface part, anyone can help?
val file = FileSpec.builder("com.template", "StatesAndContracts")
.addType(TypeSpec.classBuilder("TemplateState")
.addModifiers(KModifier.DATA)
.primaryConstructor(FunSpec.constructorBuilder()
.addParameter("data", String::class)
.build())
.addProperty(PropertySpec.builder("data", String::class)
.initializer("data")
.build())
.build())
.build()
Upvotes: 3
Views: 1850
Reputation: 89578
I think you're looking for the addSuperInterface
method, which you can chain to the TypeSpec
builder:
TypeSpec.classBuilder("TemplateState")
.addSuperinterface(ClassName("", "ContractState"))
...
If you have the package name for the ContractState
class, you can add it as the first parameter of the ClassName
constructor. Or if you can reference the ContractState
type directly, you can create the TypeName
instance with a ParameterizedTypeName.get(...)
call.
Upvotes: 6