Skip to content

Instantly share code, notes, and snippets.

@gkossakowski
Created June 6, 2013 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkossakowski/5724506 to your computer and use it in GitHub Desktop.
Save gkossakowski/5724506 to your computer and use it in GitHub Desktop.
Test case for SI-7561.
package cons
import mac._
class Consumer {
val f: Foo = new Foo
Rewrite.rewrite(f.k)
}
class Foo {
@fancy val k: Int = 5
def k$fancy: Int = ???
}
package mac
@scala.annotation.meta.getter
class fancy extends scala.annotation.StaticAnnotation
object Rewrite {
import scala.language.experimental.macros
import scala.reflect.macros.Context
def rewrite[A](a: A) = macro rewriteImpl[A]
def rewriteImpl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A]): c.Expr[A] = {
import c.universe._
//c.error(a.tree.pos, s"annotations: ${a.tree.symbol}")
//c.error(a.tree.pos, s"annotations: ${a.tree.symbol.annotations}")
def forceAnnotations(s: Symbol) = {
println(s.owner.typeSignature)
println(s.annotations)
s.annotations.foreach { ann =>
val annInternal: scala.reflect.internal.AnnotationInfos#AnnotationInfo = ann.asInstanceOf[scala.reflect.internal.AnnotationInfos#AnnotationInfo]
annInternal.completeInfo
}
}
val t = a.tree match {
case sel @ Select(qual, name) if { forceAnnotations(sel.symbol); sel.symbol.annotations.exists(_.tpe =:= weakTypeOf[fancy]) } =>
c.error(sel.pos, "rewriting $sel")
Select(qual, newTermName(name + "$fancy"))
case s => s
}
c.Expr[A](t)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment