How to debug scalajs linker error: non-existent method java.lang.Class.getDeclaredFields

How to debug scalajs linker error: non-existent method java.lang.Class.getDeclaredFields

将以下方法添加到 map case 类 到 Mapjs.Dictionary 之后 - 我已经尝试了五六个以下变体 - 我的代码编译正常且没有警告,但随后在 fastOptJS sjs 链接阶段遇到错误。

方法

  def ccToMap(cc: AnyRef) =
    (Map[String, Any]() /: cc.getClass.getDeclaredFields) {
      (a, f) =>
        f.setAccessible(true)
        a + (f.getName -> f.get(cc))
    }

请注意,我尝试过的所有变体都以略有不同的方式做同样的事情。

错误

 [info] Fast optimizing /Users/justin/Desktop/arete/jt/client/target/scala-2.11/client-fastopt.js
 [error] Referring to non-existent class java.lang.reflect.Field
 [error]   called from com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$.ccToMap(java.lang.Object)scala.collection.immutable.Map
 [error]   called from com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$.<init>()
 [error]   called from com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun.apply(japgolly.scalajs.react.extra.router.RouterCtl)japgolly.scalajs.react.ReactElement
 [error]   called from com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun.apply(java.lang.Object)java.lang.Object
 [error]   called from scala.collection.LinearSeqOptimized$class.foreach(scala.collection.LinearSeqOptimized,scala.Function1)scala.Unit
 [error]   called from scala.collection.mutable.MutableList.foreach(scala.Function1)scala.Unit
 [error]   called from scala.collection.TraversableLike$WithFilter.map(scala.Function1,scala.collection.generic.CanBuildFrom)java.lang.Object
 [error]   called from scala.collection.immutable.Stream$StreamWithFilter.map(scala.Function1,scala.collection.generic.CanBuildFrom)java.lang.Object
 [error]   called from org.scalajs.testinterface.internal.Slave.org$scalajs$testinterface$internal$Slave$$execute(scala.scalajs.js.Dynamic)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.Slave.handleMsgImpl(java.lang.String,scala.Function0)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.handleMsg(java.lang.String)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.$$anonfun(java.lang.String)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.init()scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.$$js$exported$meth$init()java.lang.Object
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.init
 [error]   exported to JavaScript with @JSExport
 [error] involving instantiated classes:
 [error]   com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$
 [error]   com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun
 [error]   scala.collection.mutable.Queue
 [error]   scala.collection.mutable.MutableList
 [error]   scala.collection.TraversableLike$WithFilter
 [error]   scala.collection.immutable.Stream$StreamWithFilter
 [error]   org.scalajs.testinterface.internal.Slave
 [error]   org.scalajs.testinterface.internal.Master
 [error] Referring to non-existent method java.lang.Class.getDeclaredFields()     [java.lang.reflect.Field
 [error]   called from com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$.ccToMap(java.lang.Object)scala.collection.immutable.Map
 [error]   called from com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$.<init>()
 [error]   called from com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun.apply(japgolly.scalajs.react.extra.router.RouterCtl)japgolly.scalajs.react.ReactElement
 [error]   called from com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun.apply(java.lang.Object)java.lang.Object
 [error]   called from scala.collection.LinearSeqOptimized$class.foreach(scala.collection.LinearSeqOptimized,scala.Function1)scala.Unit
 [error]   called from scala.collection.mutable.MutableList.foreach(scala.Function1)scala.Unit
 [error]   called from scala.collection.TraversableLike$WithFilter.map(scala.Function1,scala.collection.generic.CanBuildFrom)java.lang.Object
 [error]   called from scala.collection.immutable.Stream$StreamWithFilter.map(scala.Function1,scala.collection.generic.CanBuildFrom)java.lang.Object
 [error]   called from org.scalajs.testinterface.internal.Slave.org$scalajs$testinterface$internal$Slave$$execute(scala.scalajs.js.Dynamic)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.Slave.handleMsgImpl(java.lang.String,scala.Function0)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.handleMsg(java.lang.String)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.$$anonfun(java.lang.String)scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.init()scala.Unit
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.$$js$exported$meth$init()java.lang.Object
 [error]   called from org.scalajs.testinterface.internal.BridgeBase.init
 [error]   exported to JavaScript with @JSExport
 [error] involving instantiated classes:
 [error]   com.jshin47.jtdc.client.module.visualization.DiodeStateVizC$
 [error]   com.jshin47.jtdc.client.module.landing.LandingLocC$$anonfun
 [error]   scala.collection.mutable.Queue
 [error]   scala.collection.mutable.MutableList
 [error]   scala.collection.TraversableLike$WithFilter
 [error]   scala.collection.immutable.Stream$StreamWithFilter
 [error]   org.scalajs.testinterface.internal.Slave
 [error]   org.scalajs.testinterface.internal.Master
 [trace] Stack trace suppressed: run last client/compile:fastOptJS for the full output.
 [error] (client/compile:fastOptJS) There were linking errors
 [error] Total time: 36 s, completed May 10, 2016 2:01:07 AM

我试过的

由于不熟悉链接器的细节,我只能尝试一些 "obvious" 诊断:

如有关于如何进行、调试等的任何提示,我们将不胜感激。

或者,关于如何在没有上述样式(基于反射的)函数的情况下将 case class 转换为 Map 的任何提示,请告诉我

您根本无法在 Scala.js 中使用 Java 反射:

Java reflection and, a fortiori, Scala reflection, are not supported. There is limited support for java.lang.Class, e.g., obj.getClass.getName will work for any Scala.js object (not for objects that come from JavaScript interop).

改用宏。参见例如Scala Macros: Making a Map out of fields of a class in Scala