lundi 31 août 2020

Facing issue while using reflection class concept in scala

I have one main class like this:

class Test {
  def exe(first:String, second:String, task:String):String = {
     task match {
       case "A" => {
          val obj = new A(first)
          obj.defineSecond(second)
       }
       case "B" => {
          val obj = new B(first)
          obj.defineSecond(second)
       }
       case "C" => {
          val obj = new C(first)
          obj.defineSecond(second)
       }
       ....so many cases
   }
 }
}

Instead of writing case in my Test class everytime a new class is added, I tried using the concept of reflection in scala. Below is what I trying:

val m = ru.runtimeMirror(getClass.getClassLoader)
val classTest = ru.typeOf[Test].typeSymbol.asClass
val cm = m.reflectClass(classTest)

But getting error as "class Test is inner class, use reflectClass on an InstaneMirror to obtain its classMirror".

Can anyone knows how can I can avoid adding cases to my main class everytime a new class is created, instead I can write my main class in a way it will work for every case.





Aucun commentaire:

Enregistrer un commentaire