mardi 23 juin 2020

Setting and Getting of Values using Scala Reflection

I have a patient resource of below type:

val p:Patient = new Patient  

which comes under below package:

import org.hl7.fhir.r4.model.Patient

Now, I want to set some value for it like one ID attribute with value like example and when I try something like p.getId() I should be able to retrieve it. I was trying scala reflection and desgined below methods by referring one of the posts but not sure how to use it over here. Below are the methods for get and set:

object ReflectionDryRun {
  def main(args: Array[String]): Unit = {    
    val p:Patient=new Patient
    reflector(p.setV(idValue))
    
  }
  implicit def reflector(ref: AnyRef) = new {
    def getV(name: String): Any = ref.getClass.getMethods.find(_.getName == name).get.invoke(ref)
    def setV(name: String, value: Any): Unit = ref.getClass.getMethods.find(_.getName == name + "_$eq").get.invoke(ref, value.asInstanceOf[AnyRef])
  }   

Unable to set value of idValue using reflector method. Please guide me through it





Aucun commentaire:

Enregistrer un commentaire