Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8011065

Problems when script implements an interface with variadic methods

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 8
    • None
    • core-libs
    • None
    • b89
    • generic
    • generic

      John Keeping noted this issue and posted to nashorn-dev alias:

      File: MyInterface.java

      public interface MyInterface {
          void test(int i, String... strings);
      }


      File: Main.java

      import javax.script.*;
      import java.io.*;

      public class Main {
         public static void main(String[] args) throws Exception {
             ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
             engine.eval(new FileReader(args[0]));
             Invocable inv = (Invocable) engine;
             MyInterface obj = inv.getInterface(MyInterface.class);
             obj.test(42, "hello", "world");
         }
      }

      File: file.js

      function test(i, strings) {
          print('i = ' + i);
          print('strings = ' + strings);
      }


      File: file2.js

      function test(i, strings) {
          for (var index = 0; index < arguments.length; index++)
              print('arg ' + index + ' = ' + arguments[index]);
      }

      When running the following commands:

          javac Main.java
          java Main file.js

      we get the following output:

          i = 42
         strings = hello

      We don't get strings to be an array and no way for script to access "world" argument passed.

      When we run this command:

          java Main file2.js

      (file2.js uses arguments in script function), we get the following error:

      Exception in thread "main" java.lang.invoke.WrongMethodTypeException: Parameter counts differ: (Object[])Object vs. (int,String[])void
      at jdk.internal.dynalink.support.TypeConverterFactory.asType(TypeConverterFactory.java:184)
      at jdk.internal.dynalink.support.LinkerServicesImpl.asType(LinkerServicesImpl.java:123)
      at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.adaptHandle(JavaAdapterFactory.java:751)
      at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getHandle(JavaAdapterFactory.java:742)
      at jdk.nashorn.internal.javaadapters.MyInterface.<init>(Unknown Source)
      at jdk.nashorn.api.scripting.NashornScriptEngine.getInterfaceInner(NashornScriptEngine.java:202)
      at jdk.nashorn.api.scripting.NashornScriptEngine.getInterface(NashornScriptEngine.java:217)
      at Main.main(Main.java:9)


            attila Attila Szegedi
            sundar Sundararajan Athijegannathan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: