title: KotlinPoet: Add function to existing class
body: <p>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 <code>LiveData</code> version of a <code>MutableLiveData</code> field). </p>
  
  <p>What I want to write:</p>
  
  <pre><code>class MyClass {  
      @WithGetNonMutable  
      private val popup: MutableLiveData&lt;PopupTO?&gt; = MutableLiveData()  
  }
  </code></pre>
  
  <p>What I want to generate  </p>
  
  <pre><code>class MyClass {  
      private val popup: MutableLiveData&lt;PopupTO?&gt; = MutableLiveData()  
      fun getPopup(): LiveData&lt;PopupTO?&gt; = popup  
  }
  </code></pre>
  
  <p>Generating the function with the correct return type is no problem:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>The problem is that the variable (<code>popup</code>) 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?</p>