parent
791f2b3539
commit
5b0f2dce80
@ -505,18 +505,18 @@ public final class Convert {
|
||||
else if (fromClass.isArray()) {
|
||||
Object[] fromArray = (Object[]) from;
|
||||
|
||||
// [#3062] Default collections if no specific collection type was requested
|
||||
// [#3062] [#5796] Default collections if no specific collection type was requested
|
||||
if (Collection.class.isAssignableFrom(toClass) &&
|
||||
toClass.isAssignableFrom(ArrayList.class)) {
|
||||
return (U) new ArrayList(Arrays.asList(fromArray));
|
||||
return (U) new ArrayList<Object>(Arrays.asList(fromArray));
|
||||
}
|
||||
else if (Collection.class.isAssignableFrom(toClass) &&
|
||||
toClass.isAssignableFrom(LinkedHashSet.class)) {
|
||||
return (U) new LinkedHashSet(Arrays.asList(fromArray));
|
||||
return (U) new LinkedHashSet<Object>(Arrays.asList(fromArray));
|
||||
}
|
||||
|
||||
// [#3443] Conversion from Object[] to JDBC Array
|
||||
if (toClass == java.sql.Array.class) {
|
||||
else if (toClass == java.sql.Array.class) {
|
||||
return (U) new MockArray(null, fromArray, fromClass);
|
||||
}
|
||||
else {
|
||||
@ -524,23 +524,12 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
|
||||
else if (toClass.isArray()
|
||||
&& Collection.class.isAssignableFrom(fromClass)){
|
||||
Collection f = (Collection) from;
|
||||
Class componentType = toClass.getComponentType();
|
||||
|
||||
Object[] dest = (Object[]) Array.newInstance(componentType, f.size());
|
||||
Object[] list = f.stream()
|
||||
.map(e -> {
|
||||
if (!componentType.isAssignableFrom(e.getClass()))
|
||||
return convert(e, componentType);
|
||||
return e;
|
||||
}).toArray();
|
||||
System.arraycopy(list, 0, dest, 0, dest.length);
|
||||
|
||||
return (U) dest;
|
||||
// [#3062] Default collections if no specific collection type was requested
|
||||
else if (Collection.class.isAssignableFrom(fromClass)){
|
||||
return (U) convertArray(((Collection<?>) from).toArray(), toClass);
|
||||
}
|
||||
|
||||
|
||||
else if (toClass == Optional.class) {
|
||||
return (U) Optional.of(from);
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@ Authors and contributors of jOOQ or parts of jOOQ in alphabetical order:
|
||||
- Matti Tahvonen
|
||||
- Michael Doberenz
|
||||
- Michał Kołodziejski
|
||||
- Nathaniel Fischer
|
||||
- Oliver Flege
|
||||
- Peter Ertl
|
||||
- Robin Stocker
|
||||
|
||||
@ -1,42 +0,0 @@
|
||||
package org.jooq.tools;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by nfischer on 9/24/2016.
|
||||
*/
|
||||
public class ConvertTest {
|
||||
|
||||
@Test
|
||||
public void testFromArray(){
|
||||
String[] arr = new String[]{"Hello", "World", "!"};
|
||||
List convertedList = Convert.convert(arr, List.class);
|
||||
assertEquals(ArrayList.class, convertedList.getClass());
|
||||
assertTrue(Arrays.equals(arr, convertedList.toArray()));
|
||||
|
||||
Set convertedSet = Convert.convert(arr, Set.class);
|
||||
assertEquals(LinkedHashSet.class, convertedSet.getClass());
|
||||
assertTrue(Arrays.equals(arr, convertedSet.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromCollection(){
|
||||
List<String> list = asList("Hello", "world", "!");
|
||||
String[] arr = Convert.convertCollection(list, String[].class);
|
||||
assertEquals(list, asList(arr));
|
||||
|
||||
String[] numStrings = new String[]{"1", "2", "3"};
|
||||
List<Integer> integerList = asList(1, 2, 3);
|
||||
|
||||
String[] convertedNumString = Convert.convertCollection(integerList, String[].class);
|
||||
assertTrue(Arrays.equals(numStrings, convertedNumString));
|
||||
|
||||
assertTrue(Arrays.equals(new String[0], Convert.convertCollection(new LinkedList<Integer>(), String[].class)));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user