I have a macro annotation dealing with logging, and want to handle both SLF4J and Log4j2 logging. Unfortunately, this requires finding the type of the logger
field. I know how to do this when the annotation is on a class from other questions. But I haven't figured out the case when annotation is on the method. This is what I've tried:
val loggerType = {
val classImpl = (if (annottees.head.isInstanceOf[DefDefApi])
c.enclosingClass
else
c).asInstanceOf[ImplDef].impl
classImpl.body.collectFirst {
case vd: ValDef if vd.name == TermName("logger") =>
c.typecheck(vd.duplicate).symbol.info
}.getOrElse {
// TODO look at parents
c.abort(c.enclosingPosition, "Logger type not found")
}
}
This compiles (though enclosingClass
is deprecated), but compiling a class using this annotation results in illegal cyclic reference
. Typechecking the entire enclosing class doesn't work either.
Is there any way to do it?