Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/main/java/org/codehaus/groovy/reflection/ParameterTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.classgen.asm.util.TypeUtil;
import org.codehaus.groovy.runtime.ArrayTypeUtils;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import org.codehaus.groovy.runtime.wrappers.Wrapper;
Expand Down Expand Up @@ -271,18 +272,25 @@ private static Object[] fitToVargs(final Object[] arguments, final CachedClass[]
args[aCount] = Array.newInstance(vaType, 0);
return args;
} else if (aCount == pCount) {
// the number of arguments is correct, but if the last argument
// is no array we have to wrap it in an array; if last argument
// is null, then we don't have to do anything
// the number of arguments is correct, but unless the last argument
// is null or usable as the varargs array itself — assignable, or
// an array of the same dimension whose elements the argument
// coercion below can convert (e.g. Integer[] for int...) — it is a
// single element and has to be wrapped in an array; being an array
// is not sufficient: a byte[] passed to a byte[]... parameter is
// an element, not the byte[][] varargs array (GROOVY-11182, GROOVY-12139)
var lastArgument = getArgClass(arguments[aCount - 1]);
if (lastArgument != null && !lastArgument.isArray()) {
var varargsType = paramTypes[pCount - 1].getTheClass();
if (lastArgument == null || varargsType.isAssignableFrom(lastArgument)
|| (lastArgument.isArray()
&& ArrayTypeUtils.dimension(lastArgument) == ArrayTypeUtils.dimension(varargsType))) {
// we may have to box the argument!
return unwrappedArguments;
} else {
Object[] args = new Object[pCount];
System.arraycopy(unwrappedArguments, 0, args, 0, pCount - 1);
args[pCount-1] = makeCommonArray(unwrappedArguments, pCount - 1, vaType);
return args;
} else {
// we may have to box the argument!
return unwrappedArguments;
}
} else if (aCount > pCount) {
// wrap tail arguments in an array
Expand Down
28 changes: 21 additions & 7 deletions src/test/groovy/bugs/Groovy11182.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,35 @@
*/
package bugs

import org.codehaus.groovy.control.CompilerConfiguration
import org.junit.jupiter.api.Test

import static groovy.test.GroovyAssert.assertScript

final class Groovy11182 {

private static final String SCRIPT = '''
def foo(byte[]... byteArrays) {
assert byteArrays.length == 1
assert byteArrays[0].length == 4
}

foo("test".bytes)
'''

@Test
void testVariadicPrimitiveArray() {
assertScript '''
def foo(byte[]... byteArrays) {
assert byteArrays.length == 1
assert byteArrays[0].length == 4
}
assertScript SCRIPT
}

foo("test".bytes)
'''
// GROOVY-12139: the classic call-site path dispatches through
// MetaMethod#doMethodInvoke, whose varargs correction used to pass any
// trailing array through unwrapped instead of wrapping it when it is not
// assignable to the varargs parameter type (byte[] into byte[][])
@Test
void testVariadicPrimitiveArrayClassic() {
def config = new CompilerConfiguration()
config.optimizationOptions.indy = false
new GroovyShell(config).evaluate SCRIPT
}
}
Loading