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