diff --git a/tests/src/main/scala/shove.scala b/tests/src/main/scala/shove.scala new file mode 100644 index 00000000..d456bdc6 --- /dev/null +++ b/tests/src/main/scala/shove.scala @@ -0,0 +1,48 @@ +import scala.reflect.macros.Context +import scala.language.experimental.macros +import scala.annotation.StaticAnnotation + +object shoveMacro { + def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { + import c.universe._ + import Flag._ + val Apply(Select(New(AppliedTypeTree(_, List(victim))), _), _) = c.prefix.tree + val result = { + annottees.map(_.tree).toList match { + case ValDef(mods, name, tpt, rhs) :: Nil => + ClassDef( + Modifiers(IMPLICIT), + newTypeName(s"${victim}With$name"), + List(), + Template( + List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), + emptyValDef, + List( + ValDef(Modifiers(PRIVATE | LOCAL), newTermName("x"), victim, EmptyTree), + DefDef( + Modifiers(), + nme.CONSTRUCTOR, + List(), + List(List(ValDef(Modifiers(PARAM), newTermName("x"), victim, EmptyTree))), + TypeTree(), + Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), c.literalUnit.tree) + ), + ValDef(mods, name, tpt, rhs) + ) + ) + ) + } + } + c.Expr[Any](result) + } +} + +class shove[A] extends StaticAnnotation { + def macroTransform(annottees: Any*) = macro shoveMacro.impl +} + +package pkg { + class shove extends StaticAnnotation { + def macroTransform(annottees: Any*) = macro shoveMacro.impl + } +} \ No newline at end of file diff --git a/tests/src/test/scala/annotations/run/TypeArgs.scala b/tests/src/test/scala/annotations/run/TypeArgs.scala new file mode 100644 index 00000000..f4b14846 --- /dev/null +++ b/tests/src/test/scala/annotations/run/TypeArgs.scala @@ -0,0 +1,11 @@ +import org.scalatest.FunSuite +import scala.reflect.runtime.universe._ + +class TypeArgs extends FunSuite { + test("macro annotations with type args expand") { + @shove[Int] val description = "I’m an Int!" + @shove[String] val bar = "I’m a String!" + assert(5.description === "I’m an Int!") + assert("foo".bar === "I’m a String!") + } +} \ No newline at end of file