Call overloaded Java method in Clojure with string array

537 views Asked by At

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?

2

There are 2 answers

1
Karan Dani On

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

2
Arthur Ulfeldt On

A call would look something like this:

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