0
すべて:スカラーのデフォルト値でアノテーションを取得する方法
コメントのデフォルト値でアノテーションオブジェクトを取得する方法はありますか?
import scala.tools.reflect.ToolBox
val tb = currentMirror.mkToolBox()
val result = tb.eval(tb.untypecheck(annotation.tree)).asInstanceOf[Table]
この:オーバーライド値が渡されます
import scala.annotation.StaticAnnotation
import scala.reflect.runtime._
import scala.reflect.runtime.universe._
// The annotation has default value
case class Table(idField: String = "") extends StaticAnnotation
@Table()
case class SomeEntity()
println(getClassAnnotation[Table](classOf[SomeEntity]).idField)
def getClassAnnotation[A: TypeTag](beanClazz: Class[_]): A = {
val typeAnnotation=currentMirror.typeOf[A]
currentMirror.classSymbol(beanClazz).toType.typeSymbol.asClass.annotations.find(a => a.tree.tpe == typeAnnotation).map {
annotation =>
val value = annotation.tree.children.tail.map(_.productElement(0).asInstanceOf[Constant].value)
currentMirror.reflectClass(typeAnnotation.typeSymbol.asClass).
reflectConstructor(typeAnnotation.decl(termNames.CONSTRUCTOR).asMethod)(value: _*)
}.get.asInstanceOf[A]
}
エラーは、この
@Table(idField="code")
case class SomeEntity()