Question

I am using GroovyShell (2.1.7) to dynamically evaluate some Groovy code that I have stored off as a string.

GroovyShell shell = magicallyInstantiateAndBindGroovyShell();

The above method takes care of instantiating the shell, and binding all the required variables to it. Since I believe this is a syntax error, I won't clutter this question with all the variables the shell is being bound with, and what the code I'm trying to evaluate is actually doing. If it turns out that I need to add any more info to the question to help solve my problem, I'll happily oblige!

I then have a string of Groovy code that I am trying to evaluate:

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;
};

Which I actually have stored off as a string variable:

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; };

When I run:

shell.evaluate(code);

I get the following exception:

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"]}

Column 294 is the beginning of the for-loop... but to me, this seems like perfectly fine code. Am I forgetting a closing bracket anywhere? Some other syntax error? Where am I going awry? Thanks in advance!

Was it helpful?

Solution

You have:

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

You need a semicolon before the for...

Why not use a multi-line string?

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top