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
    • 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

      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

        Peter Niederwieser created issue -
        Jochen Theodorou made changes -
        Field Original Value New Value
        Status Open [ 1 ] Closed [ 6 ]
        Fix Version/s 1.9-beta-4 [ 17656 ]
        Fix Version/s 1.8.4 [ 17852 ]
        Assignee Jochen Theodorou [ blackdrag ]
        Fix Version/s 1.7.11 [ 17244 ]
        Resolution Fixed [ 1 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12732539 ] Default workflow, editable Closed status [ 12744412 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12970367 ] Default workflow, editable Closed status [ 12978123 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development