I am attempting to call the Apache Commons StringUtils.join() method from within a Groovy class. I want to concatenate 3 strings (part1, part2, and part3).
Why doesn’t this work?
def path = StringUtils.join([part1, part2, part3]) //1
But the following line works:
def path = StringUtils.join([part1, part2, part3] as String[]) //2
A follow up question. Why does this work? I am using StringUtils v 2.6, so it does not have a varargs method. Does groovy always convert method parameters to an array?
def path = StringUtils.join(part1, part2, part3) //3
This is largely a curiosity question. I am not going to use StringUtils because I posted a separate question yesterday and found a better solution. However, I would still like to understand why technique #1 does not work, yet #3 does work.
So, to show what is happening, let’s write a script and show what output we get:
This prints:
Basically, the only one of your method calls that matches a signature in the StringUtils class is
2), as it matches theObject[]method definition. For the other two, Groovy is picking the best match that is can find for your method call.For both
1)and3), it’s doing the same thing. Wrapping the parameters as anObject[]and calling the same method as2)For
3)this is fine, as you get anObject[]with 3 elements, but for1), you get anObject[]with one element; yourList.A way of getting it to work with the List parameter, would be to use the spread operator like so:
Which would then print:
As expected (it would put all the elements of the list in as separate parameters, and these would then be put into an
Object[]with three elements as in example3)