compiler crash with macro

48 views
Skip to first unread message

Mohit Jaggi

unread,
Oct 17, 2016, 6:07:47 PM10/17/16
to scala-user

I wrote a macro that parses JSON into a matching case class. 


def parse(jsonTree: JsValue): BaseType = macro parserImpl

def parserImpl(c: blackbox.Context)(jsonTree: c.Tree) = {
import c.universe._
val q"$json" = jsonTree
val cases = List("X", "Y").map { caseClassName =>
val caseClass = c.parse(caseClassName)
val reader = c.parse(s"JSONHelp.${caseClassName}_reads")
val y = cq"""$caseClassName => (($json \ "config").validate[$caseClass]($reader)).get"""
println(showCode(y))
y
}.toList

val r =
q"""
import play.api.libs.json._
import JSONHelp._
println($json)
($json \ "type").as[String] match { case ..$cases }
"""
println(showCode(r))

r
}



The following is that code it generates (printed by the last println):


{

  import play.api.libs.json._;

  import JSONHelp._;

  println(NodeParser.this.json);

  NodeParser.this.json.\("type").as[String] match {

    case "X" => NodeParser.this.json.\("config").validate[X](JSONHelp.X_reads).get

    case "Y" => NodeParser.this.json.\("config").validate[Y](JSONHelp.Y_reads).get

  }

}


The compilation of the subproject containing the macro definition works fine. But when I compile the project(using sbt 0.13.11 and scala 2.11.8) using the macro, I get the following error:


java.lang.NullPointerException

at play.routes.compiler.RoutesCompiler$GeneratedSource$.unapply(RoutesCompiler.scala:37)

at play.sbt.routes.RoutesCompiler$$anonfun$11$$anonfun$apply$2.isDefinedAt(RoutesCompiler.scala:180)

at play.sbt.routes.RoutesCompiler$$anonfun$11$$anonfun$apply$2.isDefinedAt(RoutesCompiler.scala:179)

at scala.Option.collect(Option.scala:250)

at play.sbt.routes.RoutesCompiler$$anonfun$11.apply(RoutesCompiler.scala:179)

at play.sbt.routes.RoutesCompiler$$anonfun$11.apply(RoutesCompiler.scala:178)

Mohit Jaggi

unread,
Oct 18, 2016, 3:11:00 PM10/18/16
to scala-user
any idea what is going on here? build works w/o macro and also if i substitute the macro body with some simple(but not what i want) code
Reply all
Reply to author
Forward
0 new messages