Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Arguments conversion tutorial



In the Second Groovy wrappers tutorial, we defined a compute(int value1, int value2) method in our Script interface with two int values as arguments. However if we used the script with other numbers, the method will throw an exception.

Overview

In this tutorial, wil will explain how to add an automatic conversion of the arguments if it is possible. We have two areas where we will have to change the code:

Change the MethodWrapper code

We will change the code of our MethodWrapeprImpl2 class:
      public class MethodWrapperImpl2 extends AbstractScriptMethodWrapper<Script, Integer> {

        public MethodWrapperImpl2(ScriptWrapper<Script> wrapper) {
          super(wrapper);
          checkDeclaredArguments("compute", Integer.class, Integer.class);
        }

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

Usage

Now, if we try do do:
      int value = methodWrapper2.executeMethod(1f, 2f);
The method will perform correctly because the float arguments will be converted to int values.

See also


Categories: tutorials

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