I created the following code snippet for use as an encoding generator for a scala type of type java.
object Macros {
def encode[A <: Product, B](value:A):B = macro MacrosImpl.encode_impl[A, B]
}
class MacrosImpl(val c:Context) {
import c.universe._
def encode_impl[ScalaType: c.WeakTypeTag, JavaType: c.WeakTypeTag](value:c.Expr[ScalaType]) = {
val scalaType: WeakTypeTag[ScalaType] = implicitly[WeakTypeTag[ScalaType]]
val fields = scalaType.tpe.typeSymbol.companion.typeSignature.members.collectFirst {
case method if method.name.toString == "apply" => method
}.toList.flatMap(_.asMethod.paramLists.flatten).
map{
case s if s.name.toString == "id" => q"underlying.setId($value.$s.orNull)"
case s => q"underlying.${c.universe.newTermName("set" + s.name.toString.capitalize) }($value.$s)"
}
val javaType: WeakTypeTag[JavaType] = implicitly[WeakTypeTag[JavaType]]
q"""
val underlying = new ${javaType.tpe}()
..$fields
underlying
"""
}
}
This compiles during macro compilation just fine when I try to use it. It throws an exception when using compilation of a library project.
private val x: IpDataEntry = IpDataEntry(None, "a", "a")
println(Macros.encode[IpDataEntry, Underlying](x)) //not comp
[error] Unknown type: <error>, <error> [class scala.reflect.internal.Types$ErrorType$, class scala.reflect.internal.Types$ErrorType$] TypeRef? false
[trace] Stack trace suppressed: run 'last web/compile:compile' for the full output.
[error] (web/compile:compile) scala.reflect.internal.FatalError: Unknown type: <error>, <error> [class scala.reflect.internal.Types$ErrorType$, class scala.reflect.internal.Types$ErrorType$] TypeRef? false
[error] Total time: 12 s, completed Jun 19, 2014 11:53:42 AM
I am stuck here and I cannot find something wrong in my code.
Scala version 2.11.1.
tiran source
share