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?

1
  • 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: stackoverflow.com/questions/11702184/… Commented Jun 22, 2015 at 10:22

2 Answers 2

1

A call would look something like this:

(.foo (MyClass.) 
      "first argument" 
      (into-array String  ["second" "and third"]))
Sign up to request clarification or add additional context in comments.

2 Comments

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
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).
-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

1 Comment

Thank you for your answer, though user3833320 is asking for an answer in Clojure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.