1 package org.andromda.cartridges.support.webservice.client;
2
3 import java.lang.reflect.Method;
4 import org.apache.axis2.databinding.typemapping.SimpleTypeMapper;
5
6
7
8
9
10
11 public class DefaultTypeMapper
12 implements TypeMapper
13 {
14
15
16
17 public Object getObject(Class type)
18 {
19 Object object = null;
20 if (type != null)
21 {
22 try
23 {
24 object = type.newInstance();
25 }
26 catch (Exception exception)
27 {
28 throw new TypeMapperException(exception);
29 }
30 }
31 return object;
32 }
33
34 private static final String VALUE_OF = "valueOf";
35
36
37
38
39 @SuppressWarnings("unchecked")
40 public Object getObject(Class type, String value)
41 {
42 Object object = null;
43 if (type != null && value != null)
44 {
45 try
46 {
47 if (type.isEnum())
48 {
49 object = type.getMethod(
50 VALUE_OF, new Class[]{String.class}).invoke(
51 type, value);
52 }
53 else
54 {
55 final Method fromMethod = getEnumerationFromMethod(type);
56 if (fromMethod != null)
57 {
58 object = fromMethod.invoke(type, value);
59 }
60 }
61 }
62 catch (Exception exception)
63 {
64 throw new TypeMapperException(exception);
65 }
66 }
67 return object;
68 }
69
70
71
72
73 public String getStringValue(Object object)
74 {
75 return SimpleTypeMapper.getStringValue(object);
76 }
77
78
79
80
81 public boolean isSimpleType(Class type)
82 {
83 return java.util.Calendar.class.isAssignableFrom(type) ||
84 java.util.Date.class.isAssignableFrom(type) ||
85 SimpleTypeMapper.isSimpleType(type) ||
86 isEnumeration(type);
87 }
88
89
90
91
92
93
94
95 private boolean isEnumeration(final Class type)
96 {
97 return isEnumeration(type, getEnumerationFromMethod(type));
98 }
99
100
101
102
103
104
105
106
107
108 private boolean isEnumeration(final Class type, final Method fromMethod)
109 {
110 boolean enumeration = false;
111 if (type != null)
112 {
113 enumeration = type.isEnum();
114 if (!enumeration)
115 {
116 enumeration = fromMethod != null;
117 }
118 }
119 return enumeration;
120 }
121
122 private static final String FROM = "from";
123
124
125
126
127
128
129
130 private Method getEnumerationFromMethod(final Class type)
131 {
132 Method fromMethod = null;
133 if (type != null)
134 {
135
136 for (final Method method : type.getMethods())
137 {
138 if (method.getName().startsWith(FROM))
139 {
140 final Class[] parameterTypes = method.getParameterTypes();
141 if (parameterTypes.length == 1)
142 {
143 final Class parameterType = parameterTypes[0];
144 if (method.getName().equals(FROM + parameterType.getSimpleName()))
145 {
146 fromMethod = method;
147 break;
148 }
149 }
150 }
151 }
152 }
153 return fromMethod;
154 }
155
156 }