[
https://issues.apache.org/jira/browse/GROOVY-12139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18094456#comment-18094456
]
ASF GitHub Bot commented on GROOVY-12139:
-----------------------------------------
paulk-asert opened a new pull request, #2675:
URL: https://github.com/apache/groovy/pull/2675
…compilation
GROOVY-11182's reproducer (a single byte[] passed to a byte[]... parameter)
still failed with IllegalArgumentException when dispatched through
MetaMethod#doMethodInvoke — the classic call-site path, and any other route
through the reflective MOP invocation. The original fix only added a test; the
invokedynamic chain's own varargs adaptation happened to handle the case,
masking the gap under the default compilation mode.
Root cause: ParameterTypes#fitToVargs treated any trailing array argument as
the pre-packed varargs array. Being an array is not sufficient — a byte[]
passed to byte[]... is a single element of the byte[][] varargs array. The
argument now passes through only when it is assignable to the varargs parameter
type or is an array of the same dimension (whose elements the downstream
argument coercion converts, e.g. Integer[] for int...); otherwise it is wrapped
as an element.
Adds a classic-compilation (indy=false) variant of the GROOVY-11182 test.
Also reproducible on GROOVY_5_0_X, so a candidate for backport.
> Primitive array Varargs Java incompatibility (fix needed also for classic
> bytecode)
> -----------------------------------------------------------------------------------
>
> Key: GROOVY-12139
> URL: https://issues.apache.org/jira/browse/GROOVY-12139
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 6.0.0-alpha-2, 5.0.7
> Reporter: Steve Eady
> Assignee: Eric Milles
> 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)