在groovy中的对象集合中查找具有属性值的对象

问题描述 投票:0回答:1

我有一个 ProjectComponent 类型的集合,我使用以下代码在我的集合中查找具有特定名称的对象。这是代码:

 if(newIssueproject.getComponents().stream().anyMatch { it.getName().equals(shortenedComponentName)  }){
                        newComponent=it
   }

我收到错误Jira自动化规则的脚本函数失败:UpdateExecutionSummary,文件:SuperFeature/rest_superFeatureGenerator.groovy,错误:groovy.lang.MissingPropertyException:没有这样的属性:它用于类:SuperFeature.rest_superFeatureGenerator

但是我查阅了教程,即使没有声明它也应该自动工作,正如您在此处看到的那样:

object variables groovy scope scriptrunner-for-jira
1个回答
0
投票

问题出在这一行:

newComponent=it
。 您在未定义的范围内使用
it
it
变量仅在
anyMatch {...}
闭包内“可见”。您需要做的是,首先找到元素,然后分配。这是草稿(使用 Java 流):

def found = newIssueproject.getComponents().stream().filter { it.getName().equals(shortenedComponentName) }.findAny()
newComponent = found.orElseGet(null)

不过,我建议使用 Groovy 语法:

def found = newIssueproject.getComponents().find { it.getName() == shortenedComponentName) }
if (found) {
    newComponent = found
}

希望对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.