Nashorn bug when calling overloaded method with varargs parameter

前端 未结 3 1204
借酒劲吻你
借酒劲吻你 2020-12-15 19:27

Assume the following API:

package nashorn.test;

public class API {

    public static void test(String string) {
        throw new RuntimeException(\"Don\'t         


        
3条回答
  •  眼角桃花
    2020-12-15 19:59

    These are valid workarounds:

    Explicitly calling the test(Integer[]) method using an array argument:

    var API = Java.type("nashorn.test.API");
    API.test([1]);
    

    Removing the overload:

    public class AlternativeAPI1 {
        public static void test(Integer... args) {
            System.out.println("OK");
        }
    }
    

    Removing the varargs:

    public class AlternativeAPI3 {
        public static void test(String string) {
            throw new RuntimeException("Don't call this");
        }
    
        public static void test(Integer args) {
            System.out.println("OK");
        }
    }
    

    Replacing String by CharSequence (or any other "similar type"):

    public class AlternativeAPI2 {
        public static void test(CharSequence string) {
            throw new RuntimeException("Don't call this");
        }
    
        public static void test(Integer args) {
            System.out.println("OK");
        }
    }
    

提交回复
热议问题