I searched StackOverflow before posting this question but I wasn’t able to find the right answer. Sorry if this is a repost. My problem is this:
I have a method that is overloaded in my java class:
class ABC{
public boolean doSomething(String path, ArrayList<SomeObject> objList){
// calling the method below in a loop
}
public boolean doSomething(String path, SomeObject obj){
// Some code here
}
}
I’m calling the above class from my groovy code like this:
void performDoSomething(ABC abc, String path, String[] strList){
def list = []
for (str in strList) {
SomeObject sObj = new SomeObject(str) // EDIT: sorry I missed this line before
list.add(sObj)
}
if (abc) abc.doSomething(path, list)
}
The problem is that the groovy compiler is calling the abc.doSomething(String str, SomeObject obj) method instead of abc.doSomething(String str, ArrayList objList) method.
I’m using eclipse IDE with grails plugin (STS) and this is part of a bigger grails project (our company has a lot of legacy code in java).
Any ideas on how to fix this?
Thank you for your time!
EDIT: I have tried
abc.doSomething(path, list as ArrayList)
too but I’m getting the same result..
Can you reproduce this with a simple example using the classes you mention? I tried, but I got the expected behavior.
SomeObject.java::
ABC.java:
test.groovy:
I run this doing (in the same directory as the files):
And the output I get is:
Which means that the correct overloaded method,
doSomething(String, List<SomeObject>), is being called by Groovy. This is what is expected, as Groovy uses the runtime type information on the methods to decide which overloaded version to call (an example of this can be found here). I would not recommend changing the signature ofdoSomthingas suggested, as it will pollute the class’ interface unnecessarily; the problem is probably somewhere else.