title: kotlin.time.Duration and java reflection
body: <p>It seems I've encountered a weird problem with interaction of java reflection API (particularly, <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Proxy.html" rel="noreferrer">java.lang.reflect.Proxy</a> and <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/-duration/" rel="noreferrer">kotlin.time.Duration</a>. It looks like Java Reflection fails to determine method return type for <code>kotlin.time.Duration</code>. Consider the following example:</p>
  
  <pre><code>@file:JvmName("Main")
  package org.test.kotlin.time.duration
  
  import java.lang.reflect.Proxy
  import kotlin.time.Duration
  import kotlin.time.ExperimentalTime
  import kotlin.time.seconds
  
  @ExperimentalTime
  interface I {
      fun getNullableDuration(): Duration? = 1.seconds
      fun getRange(): IntRange = 1..3
      fun getDuration(): Duration = 1.seconds
  }
  
  class IC: I
  
  inline fun &lt;reified T&gt; T.sampleProxy(): T = sampleProxy(T::class.java)
  
  fun &lt;T&gt; sampleProxy(c: Class&lt;T&gt;): T {
      return c.cast(Proxy.newProxyInstance(c.classLoader, arrayOf(c)) { _, method, _ -&gt;
          println("[proxy] ${method.declaringClass.name}::${method.name} return type is ${method.returnType}")
          when (method.name) {
              "getNullableDuration", "getDuration" -&gt; 1.seconds
              "getRange"                           -&gt; 0..1
              else                                 -&gt; TODO("method ${method.name} isn't handled yet")
          }
      })
  }
  
  fun main() {
      val v: I = IC()
      println("I::getNullableDuration() return type is ${v::getNullableDuration.returnType}")
      v.sampleProxy().getNullableDuration()
      println("I::getRange() return type is ${v::getRange.returnType}")
      v.sampleProxy().getRange()
      println("I::getDuration() return type is ${v::getDuration.returnType}")
      v.sampleProxy().getDuration()
  }
  </code></pre>
  
  <p>This code produces the following output:</p>
  
  <pre><code>I::getNullableDuration() return type is kotlin.time.Duration?
  [proxy] org.test.kotlin.time.duration.I::getNullableDuration return type is class kotlin.time.Duration
  I::getRange() return type is kotlin.ranges.IntRange
  [proxy] org.test.kotlin.time.duration.I::getRange return type is class kotlin.ranges.IntRange
  I::getDuration() return type is kotlin.time.Duration
  [proxy] org.test.kotlin.time.duration.I::getDuration return type is double
  Exception in thread "main" java.lang.ClassCastException: class kotlin.time.Duration cannot be cast to class java.lang.Double (kotlin.time.Duration is in unnamed module of loader 'app'; java.lang.Double is in module java.base of loader 'bootstrap')
      at com.sun.proxy.$Proxy2.getDuration(Unknown Source)
      at org.test.kotlin.time.duration.Main.main(main.kt:38)
      at org.test.kotlin.time.duration.Main.main(main.kt)
  
  Process finished with exit code 1
  </code></pre>
  
  <p>As one can see, inside <code>sampleProxy</code> return types for <code>getNullableDuration()</code> and <code>getRange()</code> are determined correctly (<code>kotlin.time.Duration?</code> expectedly becomes <code>kotlin.time.Duration</code>), while <code>getDuration()</code> suddenly becomes a method returning <code>double</code> and the cast from kotlin.time.Duration to double on return from the method fails with the exception.</p>
  
  <p>What could be a reason for the problem? How can I workaround it?</p>
  
  <p>My environment:</p>
  
  <ul>
  <li>OpenJDK 11.0.6+10</li>
  <li>Kotlin 1.3.71</li>
  <li>Linux/x86-64</li>
  </ul>