title: Read Kotlin function annotation value using reflection?
body: <p>Given an interface method like this (Android Retrofit), how do I read the URL path specified in the annotation argument from Kotlin code at runtime?</p>
  
  <p>ApiDefinition interface:</p>
  
  <pre><code>@GET("/api/somepath/objects/")
  fun getObjects(...)
  </code></pre>
  
  <p>Read the annotation value:</p>
  
  <pre><code>val method = ApiDefinition::getObjects.javaMethod
  val verb = method!!.annotations[0].annotationClass.simpleName ?: ""
  // verb contains "GET" as expected
  // But how to get the path specified in the annotation?
  val path = method!!.annotations[0].????????
  </code></pre>
  
  <p>UPDATE 1</p>
  
  <p>Thanks for answers. I'm still struggling as I can't see what type to use to do the following:</p>
  
  <pre><code>val apiMethod = ApiDefinition::getObjects
  </code></pre>
  
  <p>.... then to pass that function reference into  a method like this (it's reused)</p>
  
  <pre><code>private fun getHttpPathFromAnnotation(method: Method?) : String {
      val a = method!!.annotations[0].message
  }
  </code></pre>
  
  <p>IntelliJ IDE is suggesting I use KFunction5&lt;> as a function parameter type (it doesn't exist as far as I can see) and seems to be requiring I specify all the parameter types for the method too, which makes a generic call to get the annotation attribute impossible. Isn't there a Kotlin equivalent of "Method"?, a type that will accept any method? I tried KFunction, without success.</p>
  
  <p>UPDATE 2</p>
  
  <p>Thanks for clarifying things. I've got to this point:</p>
  
  <p>ApiDefinition (Retrofit interface)</p>
  
  <pre><code>@GET(API_ENDPOINT_LOCATIONS)
  fun getLocations(@Header(API_HEADER_TIMESTAMP) timestamp: String,
                   @Header(API_HEADER_SIGNATURE) encryptedSignature: String,
                   @Header(API_HEADER_TOKEN) token: String,
                   @Header(API_HEADER_USERNAME) username: String
                   ): Call&lt;List&lt;Location&gt;&gt;
  </code></pre>
  
  <p>Method to retrieve annotation argument:</p>
  
  <pre><code>private fun &lt;T&gt; getHttpPathFromAnnotation(method: KFunction&lt;T&gt;) : String {
      return method.annotations.filterIsInstance&lt;GET&gt;().get(0).value
  }
  </code></pre>
  
  <p>Call to get the path argument for a specific method:</p>
  
  <pre><code>    val path = getHttpPathFromAnnotation&lt;ApiDefinition&gt;(ApiDefinition::getLocations as KFunction&lt;ApiDefinition&gt;)
  </code></pre>
  
  <p>The implicit cast seems to be necessary or the type parameter demands I provide a KFunction5 type.</p>
  
  <p>This code works, but it has the GET annotation hard-coded, is there a way to make it more generic? I suspect I might need to look for GET, POST and PUT and return the first match.</p>