vendredi 8 avril 2016

Scala reflection: access and perform operation on ListBuffer object

In Parameter's method which is called in Model, I need to access cmdParameterList (created in Command). Any suggestions?

class Model{   
  Command1.param1.set("tab")
}

abstract class Command {
  var cmdParameterList=ListBuffer[Parameter]()
  val param1 = new Parameter()
}


case class Parameter() {  
  var paramValue: String = ""  

  def set(newValue: String): Unit = {       
  this.paramValue = newValue
  cmdParameterList.append(this)  //I cannot access this List which is declared in Command
   }
}

I tried to access the list using reflection. The object is found but I can't perform any operation.

  val paramList=classOf[Command].getDeclaredField("cmdParameterList")
  val paramList.append(this)  // doesnt work

Only Parameters for which value has been set can be added to the ListBuffer. In Model class should be seen as little operations as possible. That's why Parameter can't be appended there.

Any ideas?





Aucun commentaire:

Enregistrer un commentaire