groovy - Groovy 类工厂模式给出了奇怪的错误
问题描述
我是 groovy 的新手,我正在尝试一些 groovy 代码。我有这种情况。我有以下模块
package com.utils
abstract class Base {
static String data = ''
}
package com.utils
class A extends Base {
static String data = 'dummy'
}
package com.utils
class B extends Base {
static String data = 'dummy'
}
package com.utils
class ShapeFactory {
static Map <String,Object> shapes = [
"a": A,
"b": B
]
static Object get_shapes(String shape) {
return shapes.get(shape);
}
}
And in the main file I am using
在主文件中,这里是它失败并出现奇怪错误的地方。我无法确定原因,我将不胜感激。
import com.utils.ShapeFactory
def shapeA = ShapeFactory.get_shapes('a')
shapeA.data // here it fails with the below error
hudson.remoting.ProxyException: org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object '[]' with class 'java.util.ArrayList' to class 'java.util.Map'
due to: groovy.lang.GroovyRuntimeException:
Could not find matching constructor for: java.util.Map()
任何帮助将不胜感激,谢谢
解决方案
抱歉,我发现了问题,在 Base 类中,我还有一个 Map 变量,它被初始化为一个数组
abstract class Base {
static String data = ''
static Map mapper = [] // This has to be [:]
}
推荐阅读
- excel - 进行更改时如何突出显示 Exel 黄色中的一行和红色单元格(不止一次更改)
- mongodb - Union 2 表就像它是单个实体,然后排序、跳过和限制 (Mongoose)
- node.js - Axios - 等待所有响应运行 .then 块
- apache-spark - 派生 Spark SQL 中的分区数
- firebase - 限制谁可以在没有 Firestore 规则或 Firebase 身份验证的情况下获取实时文档更新
- powershell - 使用 Powershell 编辑本地组策略
- python - 从逗号分隔的单词序列中以排序形式打印唯一单词
- php - 使用 PDO 处理 PHP 中的 MySQL 错误 1104
- java - Java 从隐藏在用户点击后面的网站中抓取数据?
- python - 从一个接收数据列表的函数创建多个 matplotlib 图表