Kotlin: Get superclass from class object - object inference error
<p>The following code should iterate through the chain of superclasses of the passed object's class until the top-level is reached. </p>

<pre><code>protected fun getBestMethodFor(o: Any): Method? {
    val nodeClass = o.javaClass
    val objectClass = Any::class.java
    ...

    // Go through superclasses.
    var c = nodeClass
    while (c != objectClass &amp;&amp; ans == null) {
        debugMsg("Looking for class match for " + c.name)
        ...
        c = c.superclass
    }
    ...
}
</code></pre>

<p>This results in 2 errors:</p>

<pre><code>Error:(57, 17) Kotlin: Type mismatch: inferred type is Class&lt;in Any!&gt;! but Class&lt;Any&gt; was expected
Error:(57, 19) Kotlin: Type inference failed. Expected type mismatch: inferred type is Class&lt;in Any!&gt;! but Class&lt;Any&gt; was expected
</code></pre>

<p>Why is this failing, and what do I need to change to fix this?</p>

<p>Note: The code was automatically converted from Java using IntelliJ's converter.</p>