gpt4 book ai didi

java - 实例化参数类型的空对象

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:25 24 4
gpt4 key购买 nike

我正在尝试制作一个 CSV 阅读器,将带有标题的 CSV 文件解析为“行对象”列表。这些“线对象”的类型由函数的调用者给出。

这个想法是能够像这样使用 CSV 阅读器:

case class PlayerData(btag: String, team: String, status: String, role: String)
//...
val p = CSV.from_filename("all-team.test.csv").extract[PlayerData]
for(PlayerData(b, t, _, r) <- p) {
println(s"btag: $b, team: $t, role: $r")
}

我在 extract 函数中实例化对象时遇到一些问题。目前,该功能如下:

class CSV(data: List[List[String]]) {

def make_instance[T: ClassTag]: Option[T] = classTag[T].runtimeClass.newInstance match {
case v: T => Some(v)
case _ => None
}

def get_term[T: TypeTag](term: String) = ru.typeOf[T].decl(ru.TermName(term)).asTerm

def get_mirror[T: ClassTag] = ru.runtimeMirror(classTag[T].runtimeClass.getClassLoader)

def extract[T:ClassTag:TypeTag](): List[T] = {
val header = data.head
val datas = data.tail
data.map(row => {
val res: T = make_instance[T].get
for(i <- 0 to row.length) {
val data_symb = get_term[T](header(i))
val m = get_mirror[T]
val im = m.reflect(res)
val data_mirror = im.reflectField(data_symb)
data_mirror.set(datas(i))
}
res
})
}
}

代码可以编译,但在执行过程中出现以下异常:

[error] (run-main-0) java.lang.InstantiationException: CLI$PlayerData
[error] java.lang.InstantiationException: CLI$PlayerData
[error] at java.base/java.lang.Class.newInstance(Class.java:547)
[error] at CSV.make_instance(csv.scala:63)
[error] eam-statat CSV.$anonfun$extract$1(csv.scala:79)
[error] at scala.collection.immutable.List.map(List.scala:286)
[error] at CSV.extract(csv.scala:78)
[error] at CLI$.main(test.scala:61)
[error] at CLI.main(test.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:564)
[error] Caused by: java.lang.NoSuchMethodException: CLI$PlayerData.<init>()
[error] at java.base/java.lang.Class.getConstructor0(Class.java:3302)
[error] at java.base/java.lang.Class.newInstance(Class.java:532)
[error] at CSV.make_instance(csv.scala:63)
[error] at CSV.$anonfun$extract$1(csv.scala:79)
[error] at scala.collection.immutable.List.map(List.scala:286)
[error] at CSV.extract(csv.scala:78)
[error] at CLI$.main(test.scala:61)
[error] at CLI.main(test.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:564)

如何创建我尝试使用 CSV 行填充的对象?

最佳答案

错误出现在 make_instance 中。 classTag[T].runtimeClass.newInstance 尝试调用 T(即 PlayerData)的零参数构造函数。而 PlayerData 没有这样的构造函数。因此,要么将这样的构造函数添加到 PlayerData

case class PlayerData(btag: String, team: String, status: String, role: String) {
def this() = {
this("", "", "", "")
}
}

或修复 make_instance 调用正确的构造函数

def make_instance[T: ClassTag]: Option[T] = classTag[T].runtimeClass.getConstructor(classOf[String],classOf[String],classOf[String],classOf[String]).newInstance("", "", "", "") match {
case v: T => Some(v)
case _ => None
}

(这当然是针对 PlayerData 的,而不是针对任意 T)

<小时/>

对于任意T,您应该修改make_instance

class CSV(data: List[List[String]]) {

//def make_instance[T: ClassTag]: Option[T] =
def make_instance[T: ClassTag](args: Seq[String]): Option[T] =
// classTag[T].runtimeClass.newInstance match {
classTag[T].runtimeClass.getConstructor(Seq.fill(args.length)(classOf[String]): _*).newInstance(args: _*) match {
case v: T => Some(v)
case _ => None
}

def get_term[T: TypeTag](term: String) = ru.typeOf[T].decl(ru.TermName(term)).asTerm

def get_mirror[T: ClassTag] = ru.runtimeMirror(classTag[T].runtimeClass.getClassLoader)

def extract[T:ClassTag:TypeTag](): List[T] = {
val header = data.head
val datas = data.tail
/*data*/datas.map(row => {
val res: T = /*make_instance[T].get*/make_instance[T](row).get
for(i <- 0 /*to*/until row.length) {
val data_symb = get_term[T](header(i))
val m = get_mirror[T]
val im = m.reflect(res)
val data_mirror = im.reflectField(data_symb)
data_mirror.set(/*datas*/row(i))
}
res
})
}
}

case class PlayerData(btag: String, team: String, status: String, role: String)

val p = new CSV(List(
List("btag", "team", "status", "role"),
List("a", "b", "c", "d"),
List("a1", "b1", "c1", "d1")
)).extract[PlayerData]

for(PlayerData(b, t, _, r) <- p) {
println(s"btag: $b, team: $t, role: $r")
}
//btag: a, team: b, role: d
//btag: a1, team: b1, role: d1

关于java - 实例化参数类型的空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com