我正在尝试在类中生成对象定义。这是一个提炼的版本:
class SomeClass {
// need to figure out how to generate this
companion object {
// and this
object Constants {
val SOME_CONSTANT = "CONSTANT VALUE"
}
}
}
我正在尝试在类中生成对象定义。这是一个提炼的版本:
class SomeClass {
// need to figure out how to generate this
companion object {
// and this
object Constants {
val SOME_CONSTANT = "CONSTANT VALUE"
}
}
}
您可以创建object
withTypeSpec.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()