This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new c4615cec66 GROOVY-10378: coerce iterable to array or collection
c4615cec66 is described below
commit c4615cec66ed07c7f0d9c8c73ee9a6dbda147952
Author: Eric Milles <[email protected]>
AuthorDate: Thu May 16 14:19:33 2024 -0500
GROOVY-10378: coerce iterable to array or collection
4_0_X backport
---
.../typehandling/DefaultTypeTransformation.java | 43 ++++++++++------------
.../DefaultTypeTransformationTest.groovy | 25 +++++++++++++
2 files changed, 44 insertions(+), 24 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 5c0b4a46d8..eff0957b1a 100644
---
a/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
+++
b/src/main/java/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
@@ -23,16 +23,7 @@ import groovy.lang.GString;
import groovy.lang.GroovyRuntimeException;
import org.codehaus.groovy.classgen.asm.util.TypeUtil;
import org.codehaus.groovy.reflection.stdclasses.CachedSAMClass;
-import org.codehaus.groovy.runtime.DefaultGroovyMethods;
-import org.codehaus.groovy.runtime.FormatHelper;
-import org.codehaus.groovy.runtime.InvokerHelper;
-import org.codehaus.groovy.runtime.InvokerInvocationException;
-import org.codehaus.groovy.runtime.IteratorClosureAdapter;
-import org.codehaus.groovy.runtime.MethodClosure;
-import org.codehaus.groovy.runtime.NullObject;
-import org.codehaus.groovy.runtime.ResourceGroovyMethods;
-import org.codehaus.groovy.runtime.StreamGroovyMethods;
-import org.codehaus.groovy.runtime.StringGroovyMethods;
+import org.codehaus.groovy.runtime.*;
import java.io.File;
import java.io.IOException;
@@ -272,12 +263,6 @@ public class DefaultTypeTransformation {
}
};
- if (object instanceof BaseStream) {
- Collection answer = newCollection.get();
- answer.addAll(asCollection(object));
- return answer;
- }
-
if (object.getClass().isArray()) {
Collection answer = newCollection.get();
// we cannot just wrap in a List as we support primitive type
arrays
@@ -288,6 +273,14 @@ public class DefaultTypeTransformation {
return answer;
}
+ if (object instanceof BaseStream // GROOVY-10028
+ || (object instanceof Iterable // GROOVY-11378
+ && !(object instanceof Collection))) { // GROOVY-7867
+ Collection answer = newCollection.get();
+ answer.addAll(asCollection(object));
+ return answer;
+ }
+
return continueCastOnNumber(object, type);
}
@@ -485,22 +478,24 @@ public class DefaultTypeTransformation {
return arrayAsCollection(value);
} else if (value instanceof BaseStream) {
return StreamGroovyMethods.toList((BaseStream) value);
- } else if (value instanceof MethodClosure) {
- MethodClosure method = (MethodClosure) value;
- IteratorClosureAdapter adapter = new
IteratorClosureAdapter(method.getDelegate());
- method.call(adapter);
- return adapter.asList();
} 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 Class && ((Class) value).isEnum()) {
+ Object[] values = (Object[]) InvokerHelper.invokeMethod(value,
"values", EMPTY_OBJECT_ARRAY);
+ return Arrays.asList(values);
} else if (value instanceof File) {
try {
return ResourceGroovyMethods.readLines((File) value);
} catch (IOException e) {
throw new GroovyRuntimeException("Error reading file: " +
value, e);
}
- } else if (value instanceof Class && ((Class) value).isEnum()) {
- Object[] values = (Object[]) InvokerHelper.invokeMethod(value,
"values", EMPTY_OBJECT_ARRAY);
- return Arrays.asList(values);
+ } else if (value instanceof MethodClosure) {
+ MethodClosure method = (MethodClosure) value;
+ IteratorClosureAdapter<?> adapter = new
IteratorClosureAdapter<>(method.getDelegate());
+ method.call(adapter);
+ return adapter.asList();
} else {
// let's assume it's a collection of 1
return Collections.singletonList(value);
diff --git
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
index a889287f71..69a99c162f 100644
---
a/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
+++
b/src/test/org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformationTest.groovy
@@ -118,6 +118,31 @@ final class DefaultTypeTransformationTest {
assert result[1] == 1
}
+ @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
void testCompareTo() {
// objects