给定一个包装器案例类,它具有一组值,其类型将是带有注释的typedef。我想检查类型以查看注释是否存在以及值是什么。这样我就可以创建一个宏来构建我的case类的实例。
这应该是足够的代码,看看我已经走了多远:
class MyAnnotation(val value: String)
@MyAnnotation("some value")
type MyType = String
case class MyWrapper(myType: MyType)
// macro impl
def impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[T] = {
import c.universe._
val tpe = weakTypeOf[T]
val sym = tpe.typeSymbol.asClass
val cSym = sym.companion
val cTpe = cSym.typeSignature
val apply = cTpe.member(TermName("apply")).asMethod
val applyParamTypes = apply.paramLists.flatten.map(p => p.typeSignature)
// now I have the types of the parameters to the case class companion object apply method, I want to look at the types (of which they will be typedefs), see if the MyAnnotation annotation is present, and pull out the MyAnnotation.value
}
我正在使用Scala 2.12