2013-06-19 40 views
1

我在嵌套列表和Scala反射方面遇到了一些麻煩。Scala嵌套列表反射

如何反思List[List[something]]類型的案例類別字段?

這裏有一些代碼。它被剝離了 - 在現實生活中,它構建了關於反射類的靜態數據。有趣的部分是inspectField

import reflect.runtime.currentMirror 
import reflect.runtime.universe._ 

case class Pet(val name: String, val legs: Int) 
case class ListList2(val name: String, val stuff: List[List[Pet]]) 

object Boom extends App { 
    // Introspect class and find all its members (constructor fields) 
    val symbol = currentMirror.classSymbol(Class.forName("com.br.ListList2")) 
    val constructor = symbol.toType.members.collectFirst { 
     case method: MethodSymbol if method.isPrimaryConstructor && method.isPublic && !method.paramss.isEmpty && !method.paramss.head.isEmpty => method 
    }.getOrElse(throw new IllegalArgumentException("Case class must have at least 1 public constructor having more than 1 parameters.")) 

    // Loop through each field 
    constructor.paramss.head.map(c => inspectField(c)) 

    private def inspectField[T](sym:Symbol) : String = {  
     val cname = sym.name.toString 
     println("Field: "+cname) 
     val cType = sym.typeSignature 
     if(cType.typeSymbol.fullName.toString == "scala.collection.immutable.List") { 
      println("C: "+cType) 
      val subtype = cType.asInstanceOf[TypeRef].args(0) // Goes boom here on first recursive call 
      println("Sub:"+subtype) 
      inspectField(subtype.typeSymbol) 
     } 
     "Hi" 
    } 
} 

我的案例類指定了一個List[List[Animal]]類型的字段。我希望我的inspectField代碼被遞歸調用。第一次沒有問題。它打印:

Field: name 
Field: stuff 
C: scala.List[scala.List[com.br.Pet]] 
Sub:scala.List[com.br.Pet] 

到目前爲止,這是我的預期。現在遞歸調用inspectField這次通過subtype的第一個呼叫(List[Pet])。我希望這樣的輸出:

Field: stuff 
C: scala.List[com.br.Pet] 
Sub:com.br.Pet 

相反這正好熱潮與地方注意到了一個錯誤:

Exception in thread "main" java.lang.ClassCastException: scala.reflect.internal.Types$PolyType cannot be cast to scala.reflect.api.Types$TypeRefApi 

回答

1

這個片段顯示符合拉開類型,以及兩次調用showType展示你」重做和你打算做什麼。

val cType = sym.typeSignature 
def showType(t: Type): Unit = { 
    if(t.typeSymbol.fullName.toString == "scala.collection.immutable.List") { 
    t match { 
     case PolyType(typeParams, resultType) => 
     println(s"poly $typeParams, $resultType") 
     case TypeRef(pre, sym, args) => 
     println(s"typeref $pre, $sym, $args") 
     val subtype = args(0) 
     println("Sub:"+subtype) 
     showType(subtype.typeSymbol.typeSignature) 
     showType(subtype) 
    } 
    } 
} 
showType(cType) 
+0

非常酷,謝謝! – Greg