Groovy:构造函数哈希冲突



我有以下时髦的代码:

def    script
String credentials_id
String repository_path
String relative_directory
String repository_url
CredentialsWrapper(script, credentials_id, repository_name, repository_group, relative_directory=null) {
this(script, credentials_id, 'git@gitlab.foo.com:' + repository_group +'/' + repository_name + '.git', relative_directory);             
}
CredentialsWrapper(script, credentials_id, repository_url, relative_directory=null) {
this.script         = script;
this.credentials_id = credentials_id;
this.repository_url = repository_url;
if (null == relative_directory) {
int lastSeparatorIndex  = repository_url.lastIndexOf("/");
int indexOfExt          = repository_url.indexOf(".git"); 
this.relative_directory = repository_url.substring(lastSeparatorIndex+1, indexOfExt);
}
}

詹金斯给了我以下内容:

无法编译类 com.foo.CredentialsWrapper,因为构造函数 @ 第 30 行第 7 列中的哈希冲突。

我不明白为什么,构造函数是不同的,它们没有相同数量的参数。

另外,"脚本"是"WorkflowScript"的一个实例,但我不知道我应该导入什么来访问这个类,这将允许我显式声明脚本而不是使用"def">

知道吗?

当您使用四个参数调用构造函数时,是要调用第一个还是第二个参数?

如果你用默认值编写一个构造函数/方法,groovy 实际上会生成两个或多个版本。 所以

Test(String x, String y ="test")

将导致

Test(String x, String y) {...}

Test(String x) {new Test(x, "test")}

所以你的代码想编译为 4 个构造函数,但它包含带有签名的构造函数CredentialsWrapper(def, def, def, def)两次。 如果我正确理解您的代码,您可以省略一个或两个=null.结果将是相同的,但您只会得到两三个签名。然后,可以通过调用使用正确的参数计数调用它们来在两个版本之间进行选择。

最新更新