I am annotating a trait such as:
@ScreenModel
trait TestTrait {
.......
}
And then I have got the implementation of @ScreenModel with something like:
def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val output : List[Tree] = annottees.map(_.tree) match {
case(cd @ q"$mods trait $name[..$tparams] extends ..$parents { ..$body }") :: Nil =>
val compObjVar = s"""{
import models.presentation.blocks.BlockModel;
class AAAA {
class BBBB {
}
}
}""";
val rawTree=c.parse(compObjVar)
val merged = q"{..$rawTree}";
merged :: Nil
case _ => c.abort(c.enclosingPosition, "Invalid test target")
}
c.Expr[Any](q"..$output")
}
So, with that I get:
"top-level class without companion can only expand either into an eponymous class or into a block"
But, if I move the import just before the AAA class inside the AAA class, then it works:
val compObjVar = s"""{
class AAAA {
import models.presentation.blocks.BlockModel;
class BBBB {
}
}
}""";
Why?