Ich möchte einige Scala-Klasse zur Laufzeit laden, die Klasse sind in meinem Scala-Projekt. Genauer gesagt, im Stammordner von meinem Projekt habe ich die TmpCaseClass.scala:Laden Sie eine Klasse zur Laufzeit in einem Scala-Projekt
class TmpHBaseCaseClass(val adresse:String,val age:Int,val nom:String,val id:Int,val salaire:Float)
ich es kompilieren möchte und es während der Laufzeit unter Verwendung von ToolBox wie folgt zu verwenden:
val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()
val clazz = tb.compile(tb.parse("./TmpCaseClass.scala"))().asInstanceOf[Class[_]]
val ctor = clazz.getDeclaredConstructors()(0)
val instance = ctor.newInstance()
aber ich begegne den follwing Fehler:
Exception in thread "main" scala.tools.reflect.ToolBoxError: reflective compilation has failed:
illegal start of definition at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.throwIfErrors(ToolBoxFactory.scala:316) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.parse(ToolBoxFactory.scala:291) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$parse$1.apply(ToolBoxFactory.scala:417) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$parse$1.apply(ToolBoxFactory.scala:414) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.liftedTree2$1(ToolBoxFactory.scala:355) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:355) at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.parse(ToolBoxFactory.scala:414) at Main$.delayedEndpoint$Main$1(Main.scala:17) at Main$delayedInit$body.apply(Main.scala:13) at scala.Function0$class.apply$mcV$sp(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.collection.immutable.List.foreach(List.scala:381) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35) at scala.App$class.main(App.scala:76) at Main$.main(Main.scala:13) at Main.main(Main.scala)
die Linie 17 (wo der Fehler auftritt) entspricht:
val clazz = tb.compile(tb.parse("./TmpCaseClass.scala"))().asInstanceOf[Class[_]]
Wie könnte ich diesen Fehler loswerden?
Danke, ich habe den Fehler nicht mehr, aber jetzt weiß ich nicht, was ich tun soll. Ich habe instance.getClass.getFields.foreach (x => println (x.getName)) aber nichts wird gedruckt. Ich weiß nicht, wie ich das Objekt benutzen soll, das ich gerade gebaut habe. –