jeudi 2 mars 2017

How do I use Scala reflection to get a property from a method annotation

So I currently have the following:

import com.wordnik.swagger.annotations.ApiModel
import reflect.runtime.universe._

case class Model(
                  name: String,
                  qualifiedType: String,
                  properties: Map[String, ModelProperty],
                  description: Option[String] = None)

case class ModelProperty(
                          `type`: String,
                          qualifiedType: String,
                          description: Option[String] = None,
                          items: Option[ModelRef] = None)

case class ModelRef(
                     `type`: String,
                     ref: Option[String] = None,
                     qualifiedType: Option[String] = None)


class ModelHelper {

  def apply(models: Seq[Class[_]]) : Seq[Model] = {
    models.map(m =>
      Model(m.getSimpleName, m.getName, null, annotations(m))
    )
  }

  private def annotations(c : Class[_]) : Option[String] =
    c.getAnnotations.toSeq.map {
      case api : ApiModel => api.description
    }.headOption

  private def getType[T](clazz: Class[T]): Type = {
    val r = runtimeMirror(clazz.getClassLoader)
    r.classSymbol(clazz).toType
  }

  private def properties(c : Class[_]) = {
     getType(c).members.view.filter{!_.isMethod}.map { m =>
       m.annotations.map { a =>
         if(a.tree.tpe =:= typeOf[ApiModelProperty]) {
           a.tree.tpe.??? //<--stuck here
         }
       }
     }
  }
}

In def annotations(), I can grab all the description fields from the ApiModel class annotation. In properties, I want to grab fields from ApiModelProperty.

In an SBT console, I can clearly see the annotation I want if I drill down:

...
scala> t.members.view.filter{!_.isMethod}map{ m => { m.annotations }}).head.head.tree
q: reflect.runtime.universe.Tree = new com.wordnik.swagger.annotations.ApiModelProperty @scala.annotation.meta.field(value = "A list of errors on the asset in five different containers.")

..but I'm not sure what to match on to actually get an instance of that specific Annotation like I did in annotations() above. I guess I don't really need the instance persay, I just need to get value. How would I go about doing that? I'm on Scala 2.11.8.





Aucun commentaire:

Enregistrer un commentaire