Skip to content

Commit

Permalink
GROOVY-10378: coerce iterable to array or collection
Browse files Browse the repository at this point in the history
4_0_X backport
  • Loading branch information
eric-milles committed May 16, 2024
1 parent e54a452 commit c4615ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@
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;
Expand Down Expand Up @@ -272,12 +263,6 @@ private static Object continueCastOnCollection(final Object object, final Class
}
};

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
Expand All @@ -288,6 +273,14 @@ private static Object continueCastOnCollection(final Object object, final Class
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);
}

Expand Down Expand Up @@ -485,22 +478,24 @@ public static Collection asCollection(final Object value) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4615ce

Please sign in to comment.