[ https://issues.apache.org/jira/browse/GROOVY-6146?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles resolved GROOVY-6146. --------------------------------- Fix Version/s: 5.0.0-alpha-1 Resolution: Fixed https://github.com/apache/groovy/commit/b96166acef59760c903ab88b3029aeb169f9df12 > Calling a Java vararg method from Groovy with a null argument cast to the > vararg type behaves differently than in Java > ---------------------------------------------------------------------------------------------------------------------- > > Key: GROOVY-6146 > URL: https://issues.apache.org/jira/browse/GROOVY-6146 > Project: Groovy > Issue Type: Bug > Components: groovy-runtime > Affects Versions: 2.1.2 > Reporter: David Tonhofer > Assignee: Eric Milles > Priority: Major > Labels: varargs > Fix For: 5.0.0-alpha-1 > > > We have this Java class: > {code:title=JavaReceiver.java|borderStyle=solid} > public class JavaReceiver { > public static String receive(String... x) { > String res = ((x == null) ? "null" : ("an array of size " + > x.length)); > return "received 'x' is " + res; > } > } > {code} > which is called from this Java class to verify various effect: > {code:title=JavaSender.java|borderStyle=solid} > import org.junit.Test; > public class JavaSender { > @Test > public void sendNothing() { > System.out.println("sendNothing(): " + JavaReceiver.receive()); > } > @Test > public void sendNullWithNoCast() { > System.out.println("sendNullWithNoCast(): " + > JavaReceiver.receive(null)); > } > @Test > public void sendNullWithCastToString() { > System.out.println("sendNullWithCastToString(): " + > JavaReceiver.receive((String)null)); > } > @Test > public void sendNullWithCastToArray() { > System.out.println("sendNullWithCastToArray(): " + > JavaReceiver.receive((String[])null)); > } > @Test > public void sendOneValue() { > System.out.println("sendOneValue(): " + JavaReceiver.receive("a")); > } > @Test > public void sendThreeValues() { > System.out.println("sendThreeValues(): " + JavaReceiver.receive("a", > "b", "c")); > } > > @Test > public void sendArray() { > System.out.println("sendArray(): " + JavaReceiver.receive(new > String[]{"a", "b", "c"})); > } > } > {code} > Running the test above yields this output: > sendNothing(): received 'x' is an array of size 0 > sendNullWithNoCast(): received 'x' is null > sendNullWithCastToString(): received 'x' is an array of size 1 > sendNullWithCastToArray(): received 'x' is null > sendOneValue(): received 'x' is an array of size 1 > sendThreeValues(): received 'x' is an array of size 3 > sendArray(): received 'x' is an array of size 3 > Using essentially similar code from Groovy: > {code:title=GroovySender.groovy|borderStyle=solid} > import org.junit.Test > class GroovySender { > @Test > void sendNothing() { > System.out << "sendNothing(): " << JavaReceiver.receive() << "\n" > } > @Test > void sendNullWithNoCast() { > System.out << "sendNullWithNoCast(): " << JavaReceiver.receive(null) > << "\n" > } > @Test > void sendNullWithCastToString() { > System.out << "sendNullWithCastToString(): " << > JavaReceiver.receive((String)null) << "\n" > } > @Test > void sendNullWithCastToArray() { > System.out << "sendNullWithCastToArray(): " << > JavaReceiver.receive((String[])null) << "\n" > } > @Test > void sendOneValue() { > System.out << "sendOneValue(): " + JavaReceiver.receive("a") << "\n" > } > @Test > void sendThreeValues() { > System.out << "sendThreeValues(): " + JavaReceiver.receive("a", "b", > "c") << "\n" > } > @Test > void sendArray() { > System.out << "sendArray(): " + JavaReceiver.receive( ["a", "b", "c"] > as String[] ) << "\n" > } > } > {code} > Yields the different output: > sendNothing(): received 'x' is an array of size 0 > sendNullWithNoCast(): received 'x' is null > *sendNullWithCastToString(): received 'x' is null* > sendNullWithCastToArray(): received 'x' is null > sendOneValue(): received 'x' is an array of size 1 > sendThreeValues(): received 'x' is an array of size 3 > sendArray(): received 'x' is an array of size 3 > So the "cast to a String" does not result in a call with the argument > "String[]{null}". > Maybe that is expected behaviour though. -- This message was sent by Atlassian Jira (v8.20.10#820010)