kotlinpoet

How to use FunSpec.overriding? in KotlinPoet

夙愿已清 提交于 2021-02-11 13:34:36
问题 Submitted by Fleshgrinder on GitHub. The FunSpec class has the very handy overriding method, however, it is unclear how this can be used while generating code. Minimal example: FileSpec.builder("com.fleshgrinder", "KotlinPoet").apply { val className = ClassName("com.fleshgrinder", "KotlinPoet") addType(TypeSpec.classBuilder(className).apply { addFunction(FunSpec.builder("toString").apply { addModifiers(KModifier.OVERRIDE) addStatement("""return "KotlinPoet"""") }.build()) }.build()) }.build()

KotlinPoet: Add function to existing class

自闭症网瘾萝莉.ら 提交于 2019-12-11 17:13:30
问题 I want to build an annotation processor that generates a public "non-mutable class" getter function of a private "mutable class" field (e.g. returning a LiveData version of a MutableLiveData field). What I want to write: class MyClass { @WithGetNonMutable private val popup: MutableLiveData<PopupTO?> = MutableLiveData() } What I want to generate class MyClass { private val popup: MutableLiveData<PopupTO?> = MutableLiveData() fun getPopup(): LiveData<PopupTO?> = popup } Generating the function