Getting supertype information in an annotation macro

Hi,

I am trying to write an annotation macro which needs type information about the supertypes. Basically I need to get hold on a Type from a SymTree.

Here’s what it looks like:

def macroImpl(classDef: ClassDef, companionObjectDef: Option[ModuleDef]) = {
  val ClassDef(mods, className, typeParams, impl) = classDef
  impl.parents.foreach {
    //inspect fields and members of supertypes here
    //each parent beeing a SymTree (presumably since it's a reference to another definition)
  }
}

Theoretically I should do something similar to runtime reflection, where I can get a TypeTag and from there inspect the type. I could not figure out how to do it in a macro though.

Thanks,
Razvan

I found the solution, thanks to @SethTisue. Calling c.typecheck on the Tree (where c is the macro context) adds the missing type information. Here’s how the solution looks:

def macroImpl(classDef: ClassDef, companionObjectDef: Option[ModuleDef]) = {
  val ClassDef(mods, className, typeParams, impl) = classDef
  val parentTypes: Seq[Type] = impl.parents.map(tree => c.typecheck(tree, c.TYPEmode).tpe)
}

Razvan

1 Like