Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Groovy scripts



Groovy scripts are a type of scripts which can be used in the library.

Script syntax creation

By default you must implement all the methods of the script interface.

  • You don't need to implement any method to get the Script context. The framework will take care of it under the hood
  • If your script interface has only one method without arguments, you don't need to declare the method, but you can only define the method associated code

Basic example

Suppose the script interface:
      public interface Script {
        public int execute();
      }
and the script wrapper creation:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>(){
      };
      Script script = wrapper.getScript();

      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.execute();
wythe the folliowing script:
      public int execute() {
        return 10;
      }
The result will be 10.

Loading scripts inside a script

You can load other Groovy scripts inside the script you use. For example suppose you have the script interface:
      public interface Script {
        public int execute();
      }
And the following support.groovy Groovy script:
      public int give() {
        return 10;
      }
And finally the Groovy script implementing the interface:
      load 'support.groovy' as loaded

      public int execute() {
        return loaded.give();
      }
Executing this script will return 10.

Debugging scripts

Main Article: Debugging scripts

The framework allows to debug Groovy scripts.

Example

Suppose the following Script interface:
      public interface Script {
        public int execute();
      }
To create the wrapper, you must use a GroovyScriptWrapper:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>(){
      };
      Script script = wrapper.getScript();

      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.execute();
You can use for example the following script:
      public int execute() {
        return 10;
      }
or even:
      return 10;

Optimization level


The Groovy scripting language optimizes by default to "indy" invokeDynamic:
  • The "default", "maximum", and all values greater than 0 will set the compîler to "indy"
  • All other values will not set anything

See also


Categories: impls

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence