[ 
https://issues.apache.org/jira/browse/GROOVY-11182?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-11182:
-------------------------------
    Description: 
Groovy is not parsing primitive arrays sent into a varargs parameter the same 
way it is being parsed in Java 17.0.7

If the input value is a byte array of the string "test"...

Groovy is slicing it up into a 2d array of dimensions [4][1]
an array of size 4 each containing an array of size 1

In java, the value is being slide up into a 2d array of dimensions [1][4]
an array of size 1 containing an array of size 4.

This was detected when trying to pass in a byte[] value into a varargs of 
(byte[]... args)
in a Spring library.

Here are the test cases for Groovy vs Java

{code:groovy}
class VarArgsTestGroovy {
  static void main(String[] args) {
    byte[] bytes = "test".bytes
    test(bytes)
  }

  static test(byte[]... byteArrays) {
    assert byteArrays.length == 4
    (0..3).each {
      assert byteArrays[it].length == 1
    }
}
{code}

{code:java}
public class VarArgsTestJava {

  public static void main(String[] args) {
    byte[] bytes = "test".getBytes();
    test(bytes);
    }

  static void test(byte[]... byteArrays) {
    assert byteArrays.length == 1;
    assert byteArrays[0].length == 4;
  }
}
{code}

My current workaround is to pre-create a 2 dimensional array of size[1][value]
and pass that into the varargs method instead of the original byte array

  was:
Groovy is not parsing primitive arrays sent into a varargs parameter the same 
way it is being parsed in Java 17.0.7

If the input value is a byte array of the string "test"...

Groovy is slicing it up into a 2d array of dimensions [4][1]
an array of size 4 each containing an array of size 1

In java, the value is being slide up into a 2d array of dimensions [1][4]
an array of size 1 containing an array of size 4.

This was detected when trying to pass in a byte[] value into a varargs of 
(byte[]... args)
in a Spring library.

Here are the test cases for Groovy vs Java

 
{{class VarArgsTestGroovy {}}
{{  static void main(String[] args) {}}
{{    byte[] bytes = "test".bytes}}
{{    test(bytes)}}
    }

{{  static test(byte[]... byteArrays){}}
{{    assert byteArrays.length == 4}}
{{    (0..3).each {}} assert byteArrays[it].length == 1 }
     }
{{}}}

{{public class VarArgsTestJava {}}

{{  public static void main(String[] args) {}}
{{    byte[] bytes = "test".getBytes();}}
{{    test(bytes);}}
    }

{{  static void test(byte[]... byteArrays){}}
{{    assert byteArrays.length == 1;}}
{{    assert byteArrays[0].length == 4;}}
    }
{{}}}

My current workaround is to pre-create a 2 dimensional array of size[1][value]
and pass that into the varargs method instead of the original byte array


> Primitive array Varargs Java incompatibility
> --------------------------------------------
>
>                 Key: GROOVY-11182
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11182
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.15
>            Reporter: Steve Eady
>            Priority: Major
>
> Groovy is not parsing primitive arrays sent into a varargs parameter the same 
> way it is being parsed in Java 17.0.7
> If the input value is a byte array of the string "test"...
> Groovy is slicing it up into a 2d array of dimensions [4][1]
> an array of size 4 each containing an array of size 1
> In java, the value is being slide up into a 2d array of dimensions [1][4]
> an array of size 1 containing an array of size 4.
> This was detected when trying to pass in a byte[] value into a varargs of 
> (byte[]... args)
> in a Spring library.
> Here are the test cases for Groovy vs Java
> {code:groovy}
> class VarArgsTestGroovy {
>   static void main(String[] args) {
>     byte[] bytes = "test".bytes
>     test(bytes)
>   }
>   static test(byte[]... byteArrays) {
>     assert byteArrays.length == 4
>     (0..3).each {
>       assert byteArrays[it].length == 1
>     }
> }
> {code}
> {code:java}
> public class VarArgsTestJava {
>   public static void main(String[] args) {
>     byte[] bytes = "test".getBytes();
>     test(bytes);
>     }
>   static void test(byte[]... byteArrays) {
>     assert byteArrays.length == 1;
>     assert byteArrays[0].length == 4;
>   }
> }
> {code}
> My current workaround is to pre-create a 2 dimensional array of size[1][value]
> and pass that into the varargs method instead of the original byte array



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to