@@ -144,7 +144,7 @@ protected Message parse(Object json) {
144144
145145 if (array .get (INDEX_MESSAGEID ).getAsInt () == TYPENUMBER_CALL ) {
146146 message = new CallMessage ();
147- (( CallMessage ) message ) .setAction (array .get (INDEX_CALL_ACTION ).getAsString ());
147+ message .setAction (array .get (INDEX_CALL_ACTION ).getAsString ());
148148 message .setPayload (array .get (INDEX_CALL_PAYLOAD ).toString ());
149149 } else if (array .get (INDEX_MESSAGEID ).getAsInt () == TYPENUMBER_CALLRESULT ) {
150150 message = new CallResultMessage ();
@@ -160,131 +160,4 @@ protected Message parse(Object json) {
160160 return message ;
161161 }
162162
163- /*
164- private <T> T parseJSON(JSONObject json, Class<T> type) throws Exception {
165- T object = type.newInstance();
166-
167- for (Method method: type.getDeclaredMethods()) {
168- String key;
169- if ((key = extractKey(method)) == null)
170- continue; // Skip non-setter method
171-
172- if (json.has(key)) {
173- method.invoke(object, parseValue(key, json, method));
174- }
175- }
176- return object;
177- }
178-
179- private Object parseValue(String key, JSONObject json, Method method) throws Exception
180- {
181- Class<?> type = setterParameterType(method);
182- Type genericType = setterGenericParameterType(method);
183-
184- return parseValue(key, json, type, genericType);
185- }
186-
187- private Object parseValue(String key, JSONObject json, Class<?> type, Type genericType) throws Exception {
188- Object output;
189-
190- if (type.isArray()) {
191- output = parseArray(json.getJSONArray(key), type.getComponentType());
192- }
193- else if (type == String.class) {
194- output = json.getString(key);
195- }
196- else if (type == Calendar.class) {
197- output = DatatypeConverter.parseDateTime(json.getString(key));
198- }
199- else if (type == Integer.class || genericType == Integer.TYPE) {
200- output = json.getInt(key);
201- }
202- else if (type == Long.class || genericType == Long.TYPE) {
203- output = json.getLong(key);
204- }
205- else if (type == Double.class || genericType == Double.TYPE) {
206- output = json.getDouble(key);
207- }
208- else if (type == Boolean.class || genericType == Boolean.TYPE) {
209- output = json.getBoolean(key);
210- } else if (type.isEnum()) {
211- output = Enum.valueOf((Class<Enum>) type, json.getString(key));
212- }
213- else {
214- output = parseJSON(json.optJSONObject(key), type);
215- }
216-
217- return output;
218- }
219-
220- private <T> T[] parseArray(JSONArray array, Class<?> type) throws Exception {
221- T[] output = (T[]) Array.newInstance(type, array.length());
222- for(int i = 0; i < array.length(); i++)
223- output[i] = (T)parseArrayItem(array, i, type);
224- return output;
225- }
226-
227- private Object parseArrayItem(JSONArray array, int index, Class<?> type) throws Exception {
228- Object output;
229-
230- if (type == String.class) {
231- output = array.getString(index);
232- }
233- else if (type == Calendar.class) {
234- output = DatatypeConverter.parseDateTime(array.getString(index));
235- }
236- else if (type == Integer.class || type == Integer.TYPE) {
237- output = array.getInt(index);
238- }
239- else if (type == Long.class || type == Long.TYPE) {
240- output = array.getLong(index);
241- }
242- else if (type == Double.class || type == Double.TYPE) {
243- output = array.getDouble(index);
244- }
245- else if (type == Boolean.class || type == Boolean.TYPE) {
246- output = array.getBoolean(index);
247- }
248- else {
249- output = parseJSON(array.optJSONObject(index), type);
250- }
251-
252- return output;
253- }
254-
255- private Type setterGenericParameterType(Method method) {
256- Type[] types = method.getGenericParameterTypes();
257- return types.length > 0 ? types[0]: null;
258- }
259-
260- private Class<?> setterParameterType(Method method) {
261- Class<?>[] types = method.getParameterTypes();
262- return types.length > 0 ? types[0]: null;
263- }
264-
265- private String extractKey(Method method) {
266- String key = null;
267- if (methodIsSetter(method)) {
268- key = method.getName().substring(3);
269- key = key.substring(0, 1).toLowerCase() + key.substring(1);
270- }
271- return key;
272- }
273-
274- private boolean methodIsSetter(Method method) {
275- boolean isSetter;
276- String methodName = method.getName();
277-
278- // Setter must take one parameter and no return type
279- isSetter = method.getParameterCount() == 1;
280- isSetter &= method.getReturnType().equals(Void.TYPE);
281-
282- // Name convention must be set<ValueName>
283- isSetter &= methodName.length() > 3;
284- isSetter &= methodName.startsWith("set");
285- isSetter &= Character.isUpperCase(methodName.charAt(3));
286-
287- return isSetter;
288- }
289- */
290163}
0 commit comments