| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.impl.converter; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.TypeConverter; |
| 20 |
|
|
| 21 |
|
import java.lang.reflect.Array; |
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Collection; |
| 25 |
|
import java.util.List; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
58 |
public class ArrayTypeConverter implements TypeConverter { |
| 34 |
|
public <T> T convertTo(Class<T> type, Object value) { |
| 35 |
14 |
if (type.isArray()) { |
| 36 |
4 |
if (value instanceof Collection) { |
| 37 |
3 |
Collection collection = (Collection) value; |
| 38 |
3 |
Object array = Array.newInstance(type.getComponentType(), collection.size()); |
| 39 |
3 |
if (array instanceof Object[]) { |
| 40 |
2 |
collection.toArray((Object[]) array); |
| 41 |
2 |
} |
| 42 |
|
else { |
| 43 |
1 |
int index = 0; |
| 44 |
1 |
for (Object element : collection) { |
| 45 |
2 |
Array.set(array, index++, element); |
| 46 |
2 |
} |
| 47 |
|
} |
| 48 |
3 |
return (T) array; |
| 49 |
|
} |
| 50 |
1 |
else if (value != null && value.getClass().isArray()) { |
| 51 |
1 |
int size = Array.getLength(value); |
| 52 |
1 |
Object answer = Array.newInstance(type.getComponentType(), size); |
| 53 |
3 |
for (int i = 0; i < size; i++) { |
| 54 |
2 |
Array.set(answer, i, Array.get(value, i)); |
| 55 |
|
} |
| 56 |
1 |
return (T) answer; |
| 57 |
|
} |
| 58 |
|
} |
| 59 |
10 |
else if (Collection.class.isAssignableFrom(type)) { |
| 60 |
1 |
if (value != null) { |
| 61 |
1 |
if (value instanceof Object[]) { |
| 62 |
0 |
return (T) Arrays.asList((Object[]) value); |
| 63 |
|
} |
| 64 |
1 |
else if (value.getClass().isArray()) { |
| 65 |
1 |
int size = Array.getLength(value); |
| 66 |
1 |
List answer = new ArrayList(size); |
| 67 |
3 |
for (int i = 0; i < size; i++) { |
| 68 |
2 |
answer.add(Array.get(value, i)); |
| 69 |
|
} |
| 70 |
1 |
return (T) answer; |
| 71 |
|
} |
| 72 |
|
} |
| 73 |
|
} |
| 74 |
9 |
return null; |
| 75 |
|
} |
| 76 |
|
} |