Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3547

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6.3
    • 1.9-beta-4, 1.8.4, 1.7.11
    • Compiler
    • 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;
        }
      }
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            pniederw Peter Niederwieser
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: