0

How do I use Kotlinpoet to generate the following code?

data class Test (
  @Id
  var id: Long
) : Interface {
  override fun primaryKey() : Serializable = this
}
eahau
  • 1
  • 1
  • 1
    What have you tried so far? Are you stuck somewhere? – Bartek Lipinski Dec 16 '19 at 10:17
  • Unit tests are a great source of KotlinPoet recipes: https://github.com/square/kotlinpoet/blob/f3a35362f7bac6cf01372ece4372390882a41eed/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt#L247 – Egor Dec 17 '19 at 01:17

2 Answers2

0

You can try

FileSpec
                .builder(packageName, actionName)
                .addType(TypeSpec.classBuilder(actionName)
                        .primaryConstructor(FunSpec.constructorBuilder()
                                .addParameter(ParameterSpec.builder("aName", String::class.java)
                                        .addAnnotation(Id::class.java)
                                        .build())
                                .build()).build())
Yrii Borodkin
  • 772
  • 5
  • 7
0

with Yrii‘s answer, you also need call

addProperty(PropertySpec.builder("id",Long::class).initializer("id").build())
kevin liu
  • 23
  • 3