java.lang.NoSuchMethodException:scala.collection.immutable.$ colon $ colon [英] java.lang.NoSuchMethodException: scala.collection.immutable.$colon$colon

查看:64
本文介绍了java.lang.NoSuchMethodException:scala.collection.immutable.$ colon $ colon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态地使用使用变量作为String类型的函数,即变量将包含函数名称作为String.因此,我需要使用该变量来调用函数.

I am trying to call a function using a variable as String type dynamically i.e. variable will contain a function name as String. So, I need to call a function using that variable.

因此,我正在使用Scala反射.如果函数接受数据类型为Sting,则可以正常工作,但是会抛出错误List [Map [String,Double]]

So, I am using Scala Reflection. It is working if the function is accepting datatype as Sting, But it is throwing error List[Map[String, Double]]

我在下面的链接中引用了代码

I used below link to refer a code

和下面是我要测试的测试程序

and below is a test program which I am trying to test

package test

import scala.collection.immutable.Map

class DynamicaVariable {
 def cat(s1: List[Map[String, Double]]) = {
  println(s1)
 }
}

object DynamicaVariable{
 def main(args: Array[String]): Unit = {

 val obj = new DynamicaVariable
 val data = List(
  Map("a" -> 1.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0), Map("a" -> 1.0, "b" -> 2678.0, "c" -> 40.0, "d" -> 2.0), Map("a" -> 4.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0), Map("a" -> 1.0, "b" -> 2678.0, "c" -> 90.0, "d" -> 17.0)
)
 val functionName = "cat"
 val method = obj.getClass.getMethod(functionName,data.getClass)
 method.invoke(obj,data)
}
}

我的scala版本是2.11.6,我正在使用IntelliJ.我也检查了SDK版本.并取消选中scala下的在编译过程中运行工作表"

My scala version is 2.11.6 and I am using IntelliJ. I have checked the SDK version also. And unchecked the "Run worksheet in the compile process" under scala

但是仍然没有运气!任何想法都会有所帮助.谢谢!

But still no luck! Any idea will be helpful. Thanks!

推荐答案

如此处所述

在Scala类上使用Java反射可能会返回令人惊讶或错误的结果

using Java reflection on Scala classes might return surprising or incorrect results

在您的情况下,由于 data 的类型为 scala.collection.immutable.List ,因此您需要使用Scala反射来获取 data val.这是一个例子

It seems for your case, as data is of type scala.collection.immutable.List, you need to use Scala reflection to get the type of the data val at runtime. Here is an example

import scala.reflect.ClassTag
import scala.reflect.classTag

class DynamicaVariable {
    def cat(s1: List[Map[String, Double]]) = {
        println(s1)
    }
}

object DynamicaVariable{
    def main(args: Array[String]): Unit = {
        val obj = new DynamicaVariable
        val data = List(Map("a" -> 1.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0)
                        , Map("a" -> 1.0, "b" -> 2678.0, "c" -> 40.0, "d" -> 2.0)
                        , Map("a" -> 4.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0)
                        , Map("a" -> 1.0, "b" -> 2678.0, "c" -> 90.0, "d" -> 17.0))
        val functionName = "cat"
        val method = obj.getClass.getMethod(functionName, getClassOf(data).runtimeClass)

        method.invoke(obj,data)
    }

    def getClassOf[T: ClassTag](obj: T) = classTag[T]
}

这篇关于java.lang.NoSuchMethodException:scala.collection.immutable.$ colon $ colon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆