groovy
  1. groovy
  2. GROOVY-3547

Calling varargs method from Groovy creates varargs array of different type compared to Java

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6.3
    • Fix Version/s: 1.9-beta-4, 1.8.4, 1.7.11
    • Component/s: Compiler
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      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;
        }
      }
      

        Activity

        blackdrag blackdrag made changes -
        Field Original Value New Value
        Status Open [ 1 ] Closed [ 6 ]
        Assignee Jochen Theodorou [ blackdrag ]
        Fix Version/s 1.7.11 [ 17244 ]
        Fix Version/s 1.8.4 [ 17852 ]
        Fix Version/s 1.9-beta-4 [ 17656 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Peter Niederwieser
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: