0

Given (Scala 2.10.3),

package models
@MyAnnotation
case class MyClass() 

How do I get the package name in the impl of the macro?

I've tried:

1) A typeCheck like was suggested here, but that results in a stack overflow (although I can see it spitting out the correct full name).

val result = {
  annottees.map(_.tree).toList match {

    case classDef @ q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents { $self => ..$body }" :: Nil => {

    val full = c.typeCheck(q"??? : $name").tpe.typeSymbol.fullName
...

2) Collecting the ClassDef and calling .symbol reveals that it has none.

I'd like to avoid:

3) Passing in the value as an argument to the annotation.

4) Annotating the package and storing the name for use when expanding the class.

5) Parsing the context's .enclosingPosition with the hopes that package and directory structure corresponds.

Did I botch the typeCheck? Should I settle for #3 or #4? Any suggestions how to accomplish my goal?

Thanks very much for any input,

-Julian

Community
  • 1
  • 1
Julian Peeters
  • 853
  • 1
  • 6
  • 19

1 Answers1

0

I would typecheck a dummy definition, e.g. c.typeCheck(q"class Dummy${newTypeName(c.fresh())}") and then look into its symbol. Walking the Symbol.owner chain of that symbol would reveal the enclosing package.

Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59
  • Thanks for the suggestion, that would be great, but the compiler complained: `[error] Error occurred in an application involving default arguments`. So I added `Modifiers()` like so: `c.typeCheck(q"class Dummy ${newTypeName(c.fresh()); Modifiers() }") `, but that resulted in a typer error: `[error] last tree to typer: type Dummy [error] symbol: (flags: ) [error] symbol definition: [error] symbol owners: [error] context owners: ` -- Any ideas? – Julian Peeters Apr 11 '14 at 06:33
  • Ah, here's on the right track: `val freshName = c.fresh(newTypeName("Probe$")) val probe = c.typeCheck(q""" {class $freshName; ()} """) val owner = probe match { case Block(List(t), r) => t.symbol.owner }` Much obliged. – Julian Peeters Apr 11 '14 at 09:38