0

我正在使用GroovyShell(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;
};

我实际上已将其存储为字符串变量:

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\".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; };

当我运行时:

shell.evaluate(code);

我得到以下异常:

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 循环的开始......但对我来说,这似乎是非常好的代码。我在任何地方都忘记了右括号吗?其他一些语法错误?我哪里出错了?提前致谢!

4

1 回答 1

0

你有:

if(widget.getBuzz().equalsIgnoreCase(\"BIMDER\")) { toReturn.setMode(widget.getMode()); } for(String property : properties)

for在...之前需要一个分号

为什么不使用多行字符串?

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()
于 2013-11-07T14:23:19.140 回答