我有以下 Scala 代码,虽然调试它,并检查方法createObject证明一切正常,但我得到一个异常:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
在行constructor.newInstance(convertedArguments).asInstanceOf[T]
object ObjectFactory {
def runTest() = {
val container = createContainer("n1 | 1 | true")
container.created
}
def createContainer(s: String): Container = {
createObject(classOf[Container], s)
}
def createObject[T](cls: Class[T], line: String): T = {
// we assume the classes first non-zero
// argument constructor is the one we need :)
val constructor = cls.getConstructors.filter(_.getParameterTypes.length > 0)(0)
var ptypes = constructor.getParameterTypes
val tokens: Array[String] = line.split("\\|", -1)
val convertedArguments = tokens.zip(ptypes).collect {
case Tuple2(value: String, propType: Class[_]) => convert(value.trim, propType)
}.collect {
case Some(o) => o
case None => null
}
constructor.newInstance(convertedArguments).asInstanceOf[T]
}
private def convert[T](input: String, outputClass: Class[T]): Option[T] = {
if (outputClass == classOf[Date]) {
Some(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S").parse(input).asInstanceOf[T])
} else if (outputClass == classOf[BigDecimal]) {
Some(BigDecimal.apply(input).asInstanceOf[T])
}
else {
if (input == null || ("" == input) || ("null" == input))
None
else
Some(outputClass.getConstructor(classOf[String]).newInstance(input))
}
}
}
class Container(val name: String, val score: Integer, val created: String)
这里发生了什么?如何将代码更正为更类似于 scala - 特别是类型转换部分。