Groovy脚本上的语法错误?

Groovy脚本上的语法错误?,groovy,syntax-error,evaluation,groovyshell,Groovy,Syntax Error,Evaluation,Groovyshell,我正在使用(2.1.7)动态评估一些作为字符串存储的Groovy代码 GroovyShell shell = magicallyInstantiateAndBindGroovyShell(); 上面的方法负责实例化shell,并将所有必需的变量绑定到它。因为我相信这是一个语法错误,所以我不会把这个问题和shell绑定的所有变量以及我试图计算的代码实际在做什么混为一谈。如果事实证明我需要在问题中添加更多信息来帮助解决我的问题,我会很乐意的 然后,我将尝试评估一组Groovy代码: com.me.

我正在使用(2.1.7)动态评估一些作为字符串存储的Groovy代码

GroovyShell shell = magicallyInstantiateAndBindGroovyShell();
上面的方法负责实例化shell,并将所有必需的变量绑定到它。因为我相信这是一个语法错误,所以我不会把这个问题和shell绑定的所有变量以及我试图计算的代码实际在做什么混为一谈。如果事实证明我需要在问题中添加更多信息来帮助解决我的问题,我会很乐意的

然后,我将尝试评估一组Groovy代码:

com.me.myorg.myapp.ExpressionUtils.metaClass.filterMetadata = {
        com.me.myorg.myapp.model.WidgetVO widget, List<String> properties ->
    WidgetVO toReturn = new WidgetVO();

    toReturn.setFizz(widget.getFizz());

    if(widget.getBuzz().equalsIgnoreCase("BIMDER")) {
        toReturn.setMode(widget.getMode());
    }

    for(String property : properties) {
        if("some.prop".equals(property)) {
            Preconditions.checkNotNull(widget.getDescriptions());
            toReturn.setDescriptions(new ArrayList<DescriptionVO>());
            DescriptionVO description = widget.getDescriptions().get(0);
            toReturn.getDescriptions().add(description);
        } else if("another.prop".equals(property)) {
            Preconditions.checkNotNull(widget.getTitles().get(0));
            toReturn.setTitles(new ArrayList<TitleVO>());
            TitleVO title = widget.getTitles().get(0);
            toReturn.getTitles().add(title);
        }
    }

    return toReturn;
};
我得到以下例外情况:

startup failed, Script1.groovy: 1: unexpected token: for @ line 1, column 294.
1 error

No signature of method: com.me.myorg.myapp.ExpressionUtils.metaClass.filterMetadata() is applicable for argument types: (com.me.myorg.myapp.model.WidgetVO, java.util.ArrayList) values: {com.me.myorg.myapp.model.WidgetVO@9427908c, ["some.prop", "another.prop"]}
第294列是for循环的开始。。。但对我来说,这似乎是完美的代码。我是否忘记了一个结束括号?其他语法错误?我哪里出错了?提前谢谢

您有:

if(widget.getBuzz().equalsIgnoreCase(\"BIMDER\")) { toReturn.setMode(widget.getMode()); } for(String property : properties)
在的
前面需要一个分号

为什么不使用多行字符串

String code = """com.me.myorg.myapp.ExpressionUtils.metaClass.filterMetadata = { com.me.myorg.myapp.model.WidgetVO widget, List<String> properties ->
                |    WidgetVO toReturn = new WidgetVO()
                |    toReturn.setFizz(widget.getFizz())
                |    if( widget.getBuzz().equalsIgnoreCase( "BIMDER" ) ) {
                |        toReturn.setMode(widget.getMode())
                |    }
                |    for( String property : properties ) {
                |        if( "some.prop" == property ) { 
                |            Preconditions.checkNotNull( widget.descriptions )
                |            toReturn.descriptions = [ widget.descriptions[ 0 ] ]
                |        }
                |        else if( "another.prop" == property ) { 
                |            Preconditions.checkNotNull( widget.titles[ 0 ] )
                |            toReturn.titles = [ widget.titles[ 0 ] ]
                |        }
                |    }
                |    toReturn
                |}""".stripMargin()
String code=“””com.me.myorg.myapp.ExpressionUtils.metaClass.filterMetadata={com.me.myorg.myapp.model.WidgetVO小部件,列表属性->
|WidgetVO toReturn=新的WidgetVO()
|toReturn.setFizz(widget.getFizz())
|if(widget.getBuzz().equalsIgnoreCase(“BIMDER”)){
|toReturn.setMode(widget.getMode())
|    }
|用于(字符串属性:属性){
|如果(“some.prop”==属性){
|前提条件.checkNotNull(widget.descriptions)
|toReturn.descriptions=[widget.descriptions[0]]
|        }
|else如果(“另一个.prop”==属性){
|前提条件.checkNotNull(widget.titles[0])
|toReturn.titles=[widget.titles[0]]
|        }
|    }
|返回
|}“”条带边缘()

谢谢@tim_-yates(+1)-但我认为您复制了我的字符串不正确。如果您注意到,在
前面有一个分号:
toreern.setMode(widget.getMode())
@TicketMonster是的,但这在
for
前面的
if
语句的大括号内。。。你有
if(x){y;}表示…
,而不是
if(x){y};对于…
Ahh,我又看到了(+1!),所以我在
if
语句的右大括号后面直接添加了一个分号,现在我得到了以下错误:
startup failed,Script1.groovy:1:expecting'},find'return'@第1行,第810列。1错误
。我刚才是不是弄坏了什么东西,或者我的代码中有两个语法错误?我是否需要在
if
else if
之后添加分号,如果
for
-循环中也有
?@TicketMonster您需要在
返回返回之前添加分号。。。将其作为多行字符串来执行,您将不会那么头疼;-)
if(widget.getBuzz().equalsIgnoreCase(\"BIMDER\")) { toReturn.setMode(widget.getMode()); } for(String property : properties)
String code = """com.me.myorg.myapp.ExpressionUtils.metaClass.filterMetadata = { com.me.myorg.myapp.model.WidgetVO widget, List<String> properties ->
                |    WidgetVO toReturn = new WidgetVO()
                |    toReturn.setFizz(widget.getFizz())
                |    if( widget.getBuzz().equalsIgnoreCase( "BIMDER" ) ) {
                |        toReturn.setMode(widget.getMode())
                |    }
                |    for( String property : properties ) {
                |        if( "some.prop" == property ) { 
                |            Preconditions.checkNotNull( widget.descriptions )
                |            toReturn.descriptions = [ widget.descriptions[ 0 ] ]
                |        }
                |        else if( "another.prop" == property ) { 
                |            Preconditions.checkNotNull( widget.titles[ 0 ] )
                |            toReturn.titles = [ widget.titles[ 0 ] ]
                |        }
                |    }
                |    toReturn
                |}""".stripMargin()