0

我正在尝试在类中生成对象定义。这是一个提炼的版本:

class SomeClass {

   // need to figure out how to generate this
   companion object {

      // and this
      object Constants {
         val SOME_CONSTANT = "CONSTANT VALUE"
      }
   }
}
4

1 回答 1

1

您可以创建objectwithTypeSpec.objecBuilder然后将其嵌套在一个类中 with addType,例如:

val constants = TypeSpec.objectBuilder("Constants")
        .addProperty(PropertySpec.builder("SOME_CONSTANT", String::class)
                .mutable(false)
                .initializer("CONSTANT VALUE")
                .build())
        .build()

val someClass = TypeSpec.classBuilder("SomeClass")
        .addType(constants)
        .build()
于 2018-09-06T20:26:22.023 回答