calling wrong overloaded method with Object[] args

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

calling wrong overloaded method with Object[] args

Aled Sage
Hi,

I am trying to call a Java method from groovy code - the Java method is
overloaded in the way shown in the class below.

I expected it to write out foo2, but instead it writes out:
     foo1: [[a], [b]]

Is there any way in groovy to call the foo2 method? I presume this is
happening because it's treating the foo1 Object[] as a varargs that thus
accepts Object, Object, so that takes precedence?

Thanks, Aled

---
class ArrayParamsBug {
     public static void main(String[] args) {
         foo(["a"], ["b"])
     }

     public static void foo(Object[] o1) {
         println("foo1: $o1")
     }

     public static void foo(Object[] o1, Object[] o2) {
         println("foo2: $o1; o2")
     }
}


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: calling wrong overloaded method with Object[] args

Dinko Srkoc
On 22 February 2012 23:53, Aled Sage <[hidden email]> wrote:

> Hi,
>
> I am trying to call a Java method from groovy code - the Java method is
> overloaded in the way shown in the class below.
>
> I expected it to write out foo2, but instead it writes out:
>    foo1: [[a], [b]]
>
> Is there any way in groovy to call the foo2 method? I presume this is
> happening because it's treating the foo1 Object[] as a varargs that thus
> accepts Object, Object, so that takes precedence?

This should work:

    foo(["a"] as Object[], ["b"] as Object[])


Cheers,
Dinko

>
> Thanks, Aled
>
> ---
> class ArrayParamsBug {
>    public static void main(String[] args) {
>        foo(["a"], ["b"])
>    }
>
>    public static void foo(Object[] o1) {
>        println("foo1: $o1")
>    }
>
>    public static void foo(Object[] o1, Object[] o2) {
>        println("foo2: $o1; o2")
>    }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: calling wrong overloaded method with Object[] args

Jochen Theodorou
In reply to this post by Aled Sage
Am 22.02.2012 23:53, schrieb Aled Sage:
[...]
> class ArrayParamsBug {
> public static void main(String[] args) {
> foo(["a"], ["b"])
> }

the call is with two lists, not two arrays, thus

> public static void foo(Object[] o1) {
> println("foo1: $o1")
> }

this method can be used to store the two lists, but this method

> public static void foo(Object[] o1, Object[] o2) {
> println("foo2: $o1; o2")
> }
> }

not. The last parameter works in both cases as a vargs part, being able
to collect all additional arguments. But only the last one. Since the
first parameter of this method is an oridnary array, it is not
compatible with the list and will not be considered for the method
selection process anymore.

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...