Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.6.3
-
None
Description
Whereas Java creates an array whose runtime type matches the vararg's static type, Groovy creates an array whose runtime type matches the argument's type. Although it's not clear (to me) if the JLS is prescriptive in this area, Groovy should probably do the same as Java.
All of the following tests currently fail:
class VarArgsTest extends GroovyTestCase { void testCallObjectVarArgWithInt() { assert foo(1).getClass() == Object[] // actual: Integer[] } void testCallObjectVarArgWithStrings() { assert foo("one", "two").getClass() == Object[] // actual: String[] } void testCallObjectVarArgImplementedInJavaWithInt() { assert JavaVarArgsMethod.foo(1).getClass() == Object[] // actual: Integer[] } void testCallObjectVarArgImplementedInJavaWithStrings() { assert JavaVarArgsMethod.foo("one", "two").getClass() == Object[] // actual: String[] } void testCallSerializableVarArgWithString() { assert bar("").getClass() == Serializable[] // actual: String[] } def foo(Object... args) { args } def bar(Serializable... args) { args } }
JavaVarArgsMethod.java:
public class JavaVarArgsMethod { public static Object foo(Object... args) { return args; } }