Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Second Groovy wrappers tutorial



In the first Groovy wrappers tutorial, we defined a Script interface with one method only. This method had no arguments and a return value. In this tutorial, we will add more methods in our in Script interface.

Overview

We will define a Script interface, and run a Groovy Script as a class which implements this interface from Java.

Modify the Script interface

We will add two method definitions for the Script interface:
  • One without a return value
  • Another with some arguments
      public interface Script {
        public void doSomething();

        public int compute(int value1, int value2);
      }

Use the existing Groovy script wrapper

We don't need to modify the GroovyScriptWrapper, because we only added methods to our Script interface. It still is:
      public class GroovyScriptWrapperImpl extends GroovyScriptWrapper<Script> {
      }

Define our new Groovy method wrappers

Now we need to define wrapper for our two new methods:
  • The doSomething() method with no return value
  • The compute(int value1, int value2) method with two int arguments

Define a method wrapper for the method with two arguments

As the method has a return value, we will still extend the AbstractScriptMethodProxy class:
      public class MethodWrapperImpl2 extends AbstractScriptMethodWrapper<Script, Integer> {

        public MethodWrapperImpl2(ScriptWrapper<Script> wrapper) {
          super(wrapper);
        }

        protected Integer executeScriptImpl(Object[] arguments) {
          Integer i1 = (Integer)arguments[0];
          Integer i2 = (Integer)arguments[1];
          return script.compute(i1, i2);
        }
      }

Define a method wrapper for the method with no return value

As it is a method with no return value, we will to extend the AbstractVoidScriptMethodProxy class:
      public class MethodWrapperImpl3 extends AbstractVoidScriptMethodWrapper<Script> {

        public MethodWrapperImpl3(ScriptWrapper<Script> wrapper) {
          super(wrapper);
        }

        protected void executeScriptImpl(Object[] arguments) {
          return script.doSomething();
        }
      }

Use the scripting runtime

Suppose we have the following Groovy script:
      public int execute() {
        return 10;
      }

      public void doSomething() {
        System.out.println("I am here");
      }

      public int compute(int i1, int i2) {
        return i1 + i2;
      }
We create the ScriptWrapper exactly as in the first tutorial:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapperImpl();
Then we create the wrappers around the compute(int value1, int value2) and doSomething() methods:
      ScriptMethodWrapper<Script, Integer> methodWrapper2 = new MethodWrapperImpl2(wrapper);
      VoidScriptMethodWrapper<Script> methodWrapper3 = new MethodWrapperImpl3(wrapper);
We can now install the Script:
      File file = new File(<our script file>);
      wrapper.installScript(file);
And now we can run all the three methods:
      int value = methodWrapper.executeMethod();
      // value is 10

      int value = methodWrapper2.executeMethod(1, 2);
      // value is 3

      methodWrapper3.executeMethod();
      // the message "I am here" is printed on the console
As you see, if we try do do:
      int value = methodWrapper2.executeMethod(1f, 2f);
The method will throw an exception, because the arguments are not identical to those defined in MethodWrapperImpl2. The next tutorial will explain how to cast automatically the arguments.

Categories: tutorials

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