title: Getting Scala 2.10 annotation values at runtime
body: <p>So I have a documentation annotation that looks like this:</p>
  
  <pre class="lang-scala prettyprint-override"><code>case class DocProp(name: String = "", dataType: Class[_] = classOf[Object])
  </code></pre>
  
  <p>The idea is that by default we'll introspect the field and look at the ID and return type, but in some cases that needs overriding.</p>
  
  <p>I want to put it on the fields of my model case classes, so I redeclare it as a <code>type</code> with <code>@field</code>:</p>
  
  <pre class="lang-scala prettyprint-override"><code>type DocProperty = DocProp @field
  </code></pre>
  
  <p>And then use it in the following different scenarios:</p>
  
  <pre class="lang-scala prettyprint-override"><code>case class MyModel(
      // 1. use reflection to inspect the property
      @DocProperty
      prop1: Int,
  
      // 2. override the name
      @DocProperty(name = "myProp")
      prop2: String,
  
      // 3. override the return type
      @DocProperty(dataType = classOf[String])
      prop3: Option[String],
  
      // 4. override everything
      @DocProperty("myOtherProp", classOf[Number])
      prop4: Float,
  
      // 5. don't document this one at all
      hiddenProp: String
  )
  </code></pre>
  
  <p>I've finally managed to dig through layers of <code>runtimeMirrors</code> and whatnot to get down to the <code>Symbols</code> for the values and their associated annotations, and dig out just the <code>DocProp</code> annotations:</p>
  
  <pre class="lang-scala prettyprint-override"><code>// val mc = classOf[MyModel] // passed in
  import scala.reflect.runtime.{universe =&gt; ru}
  val mirror = ru.runtimeMirror(mc.getClassLoader)
  val members = mirror.classSymbol(mc).asType.typeSignature.members
  
  val allProps = (for (m &lt;- members) yield {
    val a8ns = m.annotations
    val a8n = a8ns.find(a =&gt; a.tpe &lt;:&lt; ru.typeOf[DocProp])
    a8n match {
      case Some(found) =&gt; Some((m, found))
      case _ =&gt; None
    }
  }).flatten
  </code></pre>
  
  <p>This gets me an iterable of <code>(Symbol, Annotation)</code> for just the annotated properties (I still need the Symbols for the reflective cases).</p>
  
  <p>But I'm now deep in a thicket of <code>reflect.runtime.universe.Trees</code>, and it's not obvious to me how to get out. How do I get from</p>
  
  <pre class="lang-scala prettyprint-override"><code>reflect.runtime.universe.Tree = classOf[java.lang.Number]
  </code></pre>
  
  <p>to an actual <code>Class[Number]</code> value? How do I get from</p>
  
  <pre class="lang-scala prettyprint-override"><code>reflect.runtime.universe.Tree = doc.this.DocProp.&lt;init&gt;$default$1
  </code></pre>
  
  <p>to the empty string, or even to <code>1</code>?</p>