2

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?

ntalbs
  • 28,700
  • 8
  • 66
  • 83
hdinh
  • 49
  • 1
  • 4
  • Can you add the exact code that triggers this, please? There are several possibilities for such an exception. Anyway, you might want to take a look at this question: http://stackoverflow.com/questions/11702184/how-to-handle-java-variable-length-arguments-in-clojure – fsb Jun 22 '15 at 10:22

2 Answers2

1

A call would look something like this:

(.foo (MyClass.) 
      "first argument" 
      (into-array String  ["second" "and third"]))
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • This seems to result in this exception. Am I missing something? IllegalArgumentException array element type mismatch java.lang.reflect.Array.set (Array.java:-2) pp.webservice.server=> (.printStackTrace *e) java.lang.IllegalArgumentException: array element type mismatch at java.lang.reflect.Array.set(Native Method) at clojure.lang.RT.seqToTypedArray(RT.java:1638) at clojure.core$into_array.invoke(core.clj:3097) at test$testme.invoke(test.clj:31 – hdinh Jun 22 '15 at 17:57
  • 2
    It's tough to know without running your exact code, but you need to make sure that all the types in the clojure seq are the type you've declared in the into-array call. For example: (Paths/get "/usr/" (into-array String ["tim"])) works. And (Paths/get "/usr/" (into-array String ["tim" 1])) throws IllegalArgumentException array element type mismatch java.lang.reflect.Array.set (Array.java:-2). – Tim Brooks Jun 22 '15 at 18:41
  • Yes, that was it. Thanks! – hdinh Jun 22 '15 at 19:10
-1

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