This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new d54ca19ac4 GROOVY-10378: coerce iterable to array or collection
d54ca19ac4 is described below
commit d54ca19ac474c5b2d9861c4287051cde58daaded
Author: Eric Milles <[email protected]>
AuthorDate: Thu May 16 13:52:46 2024 -0500
GROOVY-10378: coerce iterable to array or collection
---
.../typehandling/DefaultTypeTransformation.java | 7 +++++-
.../DefaultTypeTransformationTest.groovy | 27 +++++++++++++++++++++-
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
index a44bd80d3b..5707158350 100644
---
a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
+++
b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
@@ -274,7 +274,10 @@ public class DefaultTypeTransformation {
return answer;
}
- if (object instanceof BaseStream || object instanceof Optional) {
+ if (object instanceof BaseStream // GROOVY-10028
+ || object instanceof Optional // GROOVY-10223
+ || (object instanceof Iterable // GROOVY-11378
+ && !(object instanceof Collection))) { // GROOVY-7867
Collection answer = newCollection.get();
answer.addAll(asCollection(object));
return answer;
@@ -479,6 +482,8 @@ public class DefaultTypeTransformation {
return StreamGroovyMethods.toList((BaseStream) value);
} else if (value instanceof String || value instanceof GString) {
return StringGroovyMethods.toList((CharSequence) value);
+ } else if (value instanceof Iterable) { // GROOVY-10378
+ return DefaultGroovyMethods.toList((Iterable<?>) value);
} else if (value instanceof Optional) { // GROOVY-10223
return ((Optional<?>)
value).map(Collections::singleton).orElseGet(Collections::emptySet);
} else if (value instanceof Class && ((Class) value).isEnum()) {
diff --git
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
index f27b7c603a..fa547527f0 100644
---
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
+++
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
@@ -119,8 +119,33 @@ final class DefaultTypeTransformationTest {
assert result[1] == 1
}
- @Test // GROOVY-10223
+ @Test // GROOVY-11378
void testCastToType5() {
+ def input = new org.codehaus.groovy.util.ArrayIterable<Integer>(0,1),
result
+
+ result = DefaultTypeTransformation.castToType(input, Number[])
+ assert result instanceof Number[]
+ assert result[0] == 0
+ assert result[1] == 1
+
+ result = DefaultTypeTransformation.castToType(input, int[])
+ assert result instanceof int[]
+ assert result[0] == 0
+ assert result[1] == 1
+
+ result = DefaultTypeTransformation.castToType(input, List)
+ assert result instanceof List
+ assert result[0] == 0
+ assert result[1] == 1
+
+ result = DefaultTypeTransformation.castToType(input, Set)
+ assert result instanceof Set
+ assert result[0] == 0
+ assert result[1] == 1
+ }
+
+ @Test // GROOVY-10223
+ void testCastToType6() {
def err = shouldFail GroovyCastException, {
DefaultTypeTransformation.castToType(Optional.of('123'), Number[])
}