简体   繁体   中英

Call overloaded Java method in Clojure with string array

Given this class and the overloaded method:

public class MyClass {
    public MyClass(){}
    public String foo(string a, boolean b) { return "bool: " + i; }
    public String foo(string a, String... values) { return "strarray: " + values; }
}

We want to call foo with the second parameter. We tried many iterations with type hints but I still can't get it to call the strarray method.

This is the array we get when we try into-array:

IllegalArgumentException No matching method found: setParam for class xxx  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

How would one do this in clojure?

A call would look something like this:

(.foo (MyClass.) 
      "first argument" 
      (into-array String  ["second" "and third"]))

You can concate the string first and return the same

public class Demo {
    public String foo(String... values) {
        String value = "";
        for (int i = 0; i < values.length; i++) {
            value += values[i]+" ";
        }
        return "strarray: " + value;
    }
    public static void main(String[] args) {
        Demo demo = new Demo();
        System.out.println(demo.foo("ABCD", "PQRS","XYZW"));
    }
}

OUTPUT: strarray: ABCD PQRS XYZW

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM