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 with the correct return type is no problem:
val liveDataType = ClassName("android.arch.lifecycle", "LiveData")
val returnType = liveDataType.parameterizedBy(genericDataType)
val function = FunSpec.builder("get${element.simpleName}")
                .addModifiers(KModifier.PUBLIC)
                .addStatement("return ${element.simpleName}")
                .returns(returnType)
                .build()
The problem is that the variable (popup) is private - so to access it my generated function also needs to be part of that class (it can't be a simple extension function in a new file). The KotlinPoet example all write to new files - but there's no way to access the private field (or is there?) so I'd need to write the function in the actual class file? How can I achieve this?
 
                        
Annotation Processors cannot modify existing code, they can only generate new code.
That said, you could maybe modify your approach and generate an extension function instead of a member function.
MyClass(withprivatemodifier changed tointernal):If you completely can't modify the
MyClass(you can't changeprivatetointernal), you can (it's not that elegant, but you can do it):In the generated extension function use
Reflectionto access a private field.