-
-
Save chandu/42982d570f64e5f5df1c379f98456b3d to your computer and use it in GitHub Desktop.
Macro annotation to eliminate case class repetition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.{StaticAnnotation, compileTimeOnly} | |
import scala.language.experimental.macros | |
import scala.reflect.macros.whitebox.Context | |
@compileTimeOnly("use macro paradise") | |
class Extends[T](defaults: Any*) extends StaticAnnotation { | |
def macroTransform(annottees: Any*): Any = macro Extends.impl | |
} | |
object Extends { | |
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { | |
import c.universe._ | |
val targetTrait: Tree = c.prefix.tree match { | |
case q"new $name[$typ]" => typ | |
} | |
val tpe: Type = c.typecheck(q"0.asInstanceOf[$targetTrait]").tpe | |
val values: List[TermSymbol] = tpe.members.collect { case d if d.isTerm => d.asTerm }.filter(t => t.isStable && t.isMethod && t.isAbstract).toList.reverse | |
def extractClassNameAndFields(classDecl: ClassDef): (TypeName, Seq[Tree], Seq[Tree], Seq[Tree]) = { | |
try { | |
val q"case class $className(..$fields) extends ..$bases { ..$body }" = classDecl | |
(className, fields, bases, body) | |
} catch { | |
case _: MatchError => c.abort(c.enclosingPosition, "Annotation is only supported on case classes") | |
} | |
} | |
def modifiedFields(fields: List[ValDef]): List[ValDef] = { | |
val newParams = values.map { v => | |
ValDef(Modifiers(Flag.CASEACCESSOR), v.name, TypeTree(v.typeSignature.typeConstructor), EmptyTree) | |
} | |
newParams ++ fields | |
} | |
def modifiedClass(classDecl: ClassDef): Expr[_] = { | |
val (className, fields, bases, body) = extractClassNameAndFields(classDecl) | |
val ctor = modifiedFields(fields.asInstanceOf[List[ValDef]]) | |
c.Expr(q"case class $className(..$ctor) extends $targetTrait with ..$bases { ..$body }") | |
} | |
annottees.map(_.tree) match { | |
case (classDecl: ClassDef) :: Nil => modifiedClass(classDecl) | |
case _ => c.abort(c.enclosingPosition, "Invalid annottee") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment