我有一个
patient
以下类型的资源:
val p:Patient = new Patient
该软件包如下:
import org.hl7.fhir.r4.model.Patient
现在,我想
set
有些价值,比如
ID
具有类似值的属性
example
当我尝试像
p.getId()
我应该能找回它。我在努力
scala reflection
通过引用其中一篇帖子设计了以下方法,但不确定如何在这里使用。以下是解决问题的方法
get
和
设置
:
object PatientInvoker {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder().appName("Patient").master("local[1]").getOrCreate()
val patientOutput = "C:\\Users\\siddheshk2\\IdeaProjects\\fhir\\mapper\\src\\main\\resources\\patientOutput.json"
val idValue = spark.read.option("multiline", "true").json(patientOutput).select(col("id")).first.getString(0)
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])
}
val p: Patient = new Patient
p.setV("id", idValue)
println("id:" + p.getV("id"))
}
}
我的错误如下:
Exception in thread "main" java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:347)
at scala.None$.get(Option.scala:345)
at com.fhir.mapper.io.PatientInvoker$$anon$1.setV(StudentInvoker.scala:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
无法设置的值
idValue
采用反射镜法。请引导我通过它