0
からプロパティを取得するためにScalaのリフレクションを使用してください:は、どのように私は、メソッドのアノテーション
def annotations()
で
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
}
}
}
}
}
、私はApiModel
クラスのアノテーションからすべての説明フィールドをつかむことができます。 properties
には、ApiModelProperty
のフィールドを取得したいと考えています。私はドリルダウンした場合にSBTコンソールで
...
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.")
私が上記のannotations()
で行ったように。私は本当にインスタンスpersayを必要としないと思います、私はちょうどvalue
を取得する必要があります。それをどうやってやりますか?私はScala 2.11.8です。