package controllers.services
import scala.reflect.runtime.{universe => ru}

case class CastBean() {
  
  def stringToBean(it: String , clsName: String):Any = {
    val runtimeMirror =ru.runtimeMirror(Thread.currentThread.getContextClassLoader)
    val beanClassSymbol = runtimeMirror.staticClass(clsName).toType
    val bcm = Class.forName(clsName).newInstance()
    val bim = runtimeMirror.reflect(bcm)
    var i:Int=0
    if(it!="") {
      for(f<-Class.forName(clsName).getDeclaredFields()){
        val fieldSymbol = beanClassSymbol.declaration(ru.newTermName(f.getName())).asTerm
        val fm =bim.reflectField(fieldSymbol)
        fm.set(
            f.getType().getName() match {
              case "long" =>
                it.split(",").apply(i) match{
                  case "" => 0
                  case _ => it.split(",").apply(i).toInt
                  }
              case "java.lang.String" =>
                  it.split(",").apply(i) match{
                    case "" => " "
                    case _ => it.split(",").apply(i)
                  }
              case _ =>
                it.split(",").apply(i) match{
                  case _ => null
                  }
              }
         )
        i += 1
       }
     }
    bcm
  }
}