diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-06-10 14:06:35 +0200 |
---|---|---|
committer | Jon Pretty <jon.pretty@propensive.com> | 2017-06-10 14:06:35 +0200 |
commit | 756d8350567c8993396d21b8d0c82902812321be (patch) | |
tree | 9b13c91cdc5ffee1f99a601aff14694a79f6277c /core/src/main | |
parent | f3a9707a2f3528ff11543c903158ecdf12d40c3a (diff) | |
download | magnolia-756d8350567c8993396d21b8d0c82902812321be.tar.gz magnolia-756d8350567c8993396d21b8d0c82902812321be.tar.bz2 magnolia-756d8350567c8993396d21b8d0c82902812321be.zip |
Avoid nasty asInstanceOf tree-rewriting hack
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/scala/generic.scala | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/core/src/main/scala/generic.scala b/core/src/main/scala/generic.scala index a353677..5f60ff2 100644 --- a/core/src/main/scala/generic.scala +++ b/core/src/main/scala/generic.scala @@ -15,6 +15,10 @@ case class Pos(pos: api.Position) { case class DirectlyReentrantException() extends Exception("attempt to recurse directly") +object Lazy { + def apply[T](method: String): T = ??? +} + object GlobalMutableState { def log(c: whitebox.Context)(msg: String) = msg.split("/n").foreach { ln => @@ -60,7 +64,6 @@ object GlobalMutableState { @bundle class Macros(val context: whitebox.Context) extends GenericMacro(context) { - protected def classBody(context: whitebox.Context)(genericType: context.Type, implementation: context.Tree): context.Tree = { import context.universe._ q"""def extract(src: _root_.magnolia.Thing): $genericType = $implementation""" @@ -89,10 +92,8 @@ abstract class GenericMacro(whiteboxContext: whitebox.Context) { import c.universe._ val transformer = new Transformer { override def transform(tree: Tree): Tree = tree match { - case ta@TypeApply(Select(Literal(Constant(method: String)), TermName("asInstanceOf")), List(tpe)) => - log(c)(s"FOUND TYPEAPPLY: ${ta}") - val m = TermName(method) - q"$m" + case q"_root_.magnolia.Lazy[$returnType](${Literal(Constant(method: String))})" => + q"${TermName(method)}" case _ => super.transform(tree) } } @@ -107,6 +108,7 @@ abstract class GenericMacro(whiteboxContext: whitebox.Context) { val str = nm.encodedName.toString val searchType = appliedType(typeConstructor, genericType) q"$str.asInstanceOf[${searchType}]" + q"_root_.magnolia.Lazy[$searchType]($str)" }.orElse { val searchType = appliedType(typeConstructor, genericType) if(GlobalMutableState.find(c)(genericType).isEmpty) { |