title: How can I set a property of a companion object in Kotlin via reflection?
body: <p>When I have a class that has a companion object, is it possible to set  a property in this companion object using reflection? I can do it with normal properties, but fail on companion objects:</p>
  
  <pre><code>import kotlin.reflect.KMutableProperty
  import kotlin.reflect.full.companionObject
  import kotlin.reflect.full.memberProperties
  
  class WithProperty {
      lateinit var prop: String
  
      companion object {
          lateinit var companionProp: String
      }
  
      fun test() = "$companionProp $prop"
  }
  
  fun main(args: Array&lt;String&gt;) {
      val obj = WithProperty()
  
      val prop = obj::class.memberProperties.filter { it.name == "prop" }.first()
      if (prop is KMutableProperty&lt;*&gt;) {
          prop.setter.call(obj, "world")
      }
  
      val companion = obj::class.companionObject
      if (companion != null) {
          val companionProp = companion.memberProperties.filter { it.name == "companionProp" }.first()
          if (companionProp is KMutableProperty&lt;*&gt;) {
              companionProp.setter.call(companionProp, "hello") // &lt;-- what must go here as first argument?
          }
      }
  
      println(obj.test())
  }
  </code></pre>
  
  <p>Calling the setter for the <em>normal</em> property works as it should, but when I call <code>companionProp.setter.call(companionProp, "hello")</code> I get </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class</p>
  </blockquote>
  
  <p>What do I have to pass as first argument to <code>call()</code> to succeed?</p>
  
  <p><strong>Edit:</strong> I wrote <code>companionProp</code>as first argument, but that definitely is wrong, I actually tried with the <code>companion</code> object, but that is not working as well.</p>