0

Given a wrapper case class which has a set of values of which the types will be typedefs with annotations on them. I want to inspect the types to see whether the annotation is present and what the value is. This is so that I can create a macro that will construct an instance of my case class.

This should be enough code to see how far I've gotten:

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
}

I am using Scala 2.12

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • Do not map to keep only the `.typeSignature` of each param of apply, if wanting to get the param `.annotations` the param itself must be used: https://github.com/ReactiveMongo/ReactiveMongo/blob/master/macros/src/main/scala-2.12/MacroImpl.scala – cchantep Aug 30 '17 at 07:44
  • I'm not looking to get the parameter annotations, I am looking for the parameter type annotations – Cheetah Aug 30 '17 at 11:22
  • `.typeSignature` is `Type` (or `TypeApi`), it doesn't expose any annotation, try `.typeSymbol` from there – cchantep Aug 30 '17 at 11:57
  • I got there using a `TypeRef` on `typeSignature` – Cheetah Aug 30 '17 at 19:20

0 Answers0