|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.rblasch.convert.converters; |
|
17 |
| |
|
18 |
| import org.rblasch.convert.MetaConverter; |
|
19 |
| import org.rblasch.convert.type.Type; |
|
20 |
| import org.rblasch.convert.type.Types; |
|
21 |
| |
|
22 |
| import java.lang.reflect.Method; |
|
23 |
| import java.util.Arrays; |
|
24 |
| import java.util.HashSet; |
|
25 |
| import java.util.Iterator; |
|
26 |
| import java.util.Set; |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class ObjectToMetaConvertersConverter extends AbstractConverter { |
|
32 |
| private final int weight; |
|
33 |
| |
|
34 |
1
| public ObjectToMetaConvertersConverter(final int weight) {
|
|
35 |
1
| this.weight = weight;
|
|
36 |
| } |
|
37 |
| |
|
38 |
0
| public Type getSourceType() {
|
|
39 |
0
| return Types.findTypeByClass(Object.class);
|
|
40 |
| } |
|
41 |
| |
|
42 |
0
| public Type getDestinationType() {
|
|
43 |
0
| return Types.findPTypeByClass(Set.class, MetaConverter.class);
|
|
44 |
| } |
|
45 |
| |
|
46 |
0
| public int getWeight() {
|
|
47 |
0
| return 100;
|
|
48 |
| } |
|
49 |
| |
|
50 |
1
| public Object convert(final Object obj) throws Exception {
|
|
51 |
1
| final Set converters = new HashSet();
|
|
52 |
| |
|
53 |
1
| for (final Iterator i = Arrays.asList(obj.getClass().getMethods()).iterator(); i.hasNext();) {
|
|
54 |
14
| final Method m = (Method) i.next();
|
|
55 |
14
| if (m.getName().startsWith("convert")
|
|
56 |
| && m.getParameterTypes().length == 1 |
|
57 |
| && !m.getReturnType().equals(Void.TYPE)) { |
|
58 |
2
| converters.add(new AbstractClassConverter() {
|
|
59 |
2
| public Class getSourceClass() {
|
|
60 |
2
| return m.getParameterTypes()[0];
|
|
61 |
| } |
|
62 |
| |
|
63 |
2
| public Class getDestinationClass() {
|
|
64 |
2
| return m.getReturnType();
|
|
65 |
| } |
|
66 |
| |
|
67 |
0
| public int getWeight() {
|
|
68 |
0
| return weight;
|
|
69 |
| } |
|
70 |
| |
|
71 |
0
| public Object convert(final Object arg) throws Exception {
|
|
72 |
0
| return m.invoke(obj, new Object[]{arg});
|
|
73 |
| } |
|
74 |
| }); |
|
75 |
| } |
|
76 |
| } |
|
77 |
| |
|
78 |
1
| return converters;
|
|
79 |
| } |
|
80 |
| } |