groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thierry Hanser <Thierry.Han...@lhasalimited.org>
Subject GroovyShell : Can't modify (reassign) a variable within a script compiled with GroovyShell
Date Thu, 11 Feb 2016 19:13:52 GMT
Dear fellow Groovy users,

I stumbled over this unexpected behaviour. I was trying to access and modify a variable declared
in a base script class using the GroovyShell parser. Below is a minimal code illustrating
the issue. Although I can access the variable from the compiled script, I can't seem to change
its value!. This was unexpected. I tried all combinations of setters/getters/visibility and
can at the best read but never modify the variable 'x'. The same behaviour occurs with using
the GroovyScriptEngine or GroovyClassLoader


1.   Am I doing something wrong (stupid?) or is there a bug/limitation in the current parsing/compilation
process?
2. Which is as today the preferred compiler class between GroovyScriptEngine, GroovyShell
and GroovyClassLoader?

Merci/Thank you very much in advance for your help.

Thierry
-

import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;

import groovy.lang.Binding;
import groovy.lang.Closure
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.transform.CompileStatic;;

abstract class GroovyDemoScript extends Script
{
    public String x;

    public GroovyDemoScript()
    {
           x='one';
           System.out.println("init x: " + x);
    }

     /**
     * Demo main
     * @param args
     * @throws Throwable
     */
    public static void main(String...args) throws Throwable
    {
           ////
           // Compilation configuration
           CompilerConfiguration configuration = new CompilerConfiguration();
           configuration.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic.class));
           configuration.setScriptBaseClass(GroovyDemoScript.class.name);
           GroovyShell shell = new GroovyShell(this.getClass().getClassLoader(), new Binding(),
configuration);

           // source code
           String scriptSource= "println '1 = ' + x; x = 'two'; println '2 = ' + x\n";

           // compile the source code and run the compiled script
           GroovyDemoScript compiledScript = shell.parse(scriptSource);
          compiledScript.run();
    }
}

Output:

init x: one  <- initial value assignement OK :)
1 = one      <- successfully accessing 'x' from within the compiled script OK :)
2 = one      <- should be 'two' as instructed but is unchanged ??? :(



________________________________
Switchboard: +44 (0)113 394 6020
Technical Support: +44 (0)113 394 6030
________________________________
Lhasa Limited, a not-for-profit organisation, promotes scientific knowledge & understanding
through the development of computer-aided reasoning & information systems in chemistry
& the life sciences. Registered Charity Number 290866. Registered Office: Granary Wharf
House, 2 Canal Wharf, Leeds, LS11 5PS. Company Registration Number 01765239. Registered in
England and Wales.
This communication, including any associated attachments, is intended for the use of the addressee
only and may contain confidential, privileged or copyright material. If you are not the intended
recipient, you must not copy this message or attachment or disclose the contents to any other
person. If you have received this transmission in error, please notify the sender immediately
and delete the message and any attachment from your system. Except where specifically stated,
any views expressed in this message are those of the individual sender and do not represent
the views of Lhasa Limited. Lhasa Limited cannot accept liability for any statements made
which are the sender's own. Lhasa Limited does not guarantee that electronic communications,
including any attachments, are free of viruses. Virus scanning is recommended and is the responsibility
of the recipient.

Mime
View raw message