Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object CaseClassMacro {
- def implementation(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
- import c.universe._
- // lekérjük az annotált objektumokat egyenként
- annottees.map(_.tree).toList match {
- // mintaillesztéssel kiválogatjuk azokat az eseteket,
- // amikor egy osztályt jelöltünk meg vele
- case q"class $name(..$params) extends ..$parents { ..$body }" :: Nil => {
- val termName : TermName = name.toTermName
- val parameterNames = params.map(param => param.name)
- val parameterTypes = params.map((param : ValDef) => param.tpt)
- val selections = parameterNames.map((param: TermName) => Select(Ident(newTermName("obj")), param))
- val tree = q"""
- class $name protected (..$params) extends ..$parents { ..$body }
- object $termName {
- def apply(..$params) = new $name(..$parameterNames)
- def unapply(obj: $name) : Option[(..$parameterTypes)] = Some(
- (..$selections)
- )
- }
- """
- println(show(tree))
- c.Expr[Any](tree)
- }
- case _ => {
- c.error(c.enclosingPosition, "Unsupported expression!")
- c.Expr[Any](EmptyTree)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement